Skip to content

Commit

Permalink
Metamodel #329: support obtaining case type values
Browse files Browse the repository at this point in the history
  • Loading branch information
FroMage committed Apr 24, 2014
1 parent 6112924 commit 966c97d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Expand Up @@ -2,9 +2,13 @@

import java.util.List;

import ceylon.language.ArraySequence;
import ceylon.language.Iterator;
import ceylon.language.Map;
import ceylon.language.Sequential;
import ceylon.language.empty_;
import ceylon.language.finished_;
import ceylon.language.meta.declaration.ValueDeclaration;
import ceylon.language.meta.model.ClassOrInterface$impl;
import ceylon.language.meta.model.IncompatibleTypeException;
import ceylon.language.meta.model.Member;
Expand Down Expand Up @@ -522,6 +526,31 @@ public boolean exactly(@TypeInfo("ceylon.language.meta.model::Type<ceylon.langua
return Metamodel.isExactly(producedType, type);
}

@Override
@TypeInfo("ceylon.language::Sequential<Type>")
public ceylon.language.Sequential<? extends Type> getCaseValues(){
Sequential<? extends ceylon.language.meta.declaration.OpenType> caseTypeDeclarations = getDeclaration().getCaseTypes();
Iterator<? extends ceylon.language.meta.declaration.OpenType> iterator = caseTypeDeclarations.iterator();
Object it;
@SuppressWarnings("unchecked")
Type[] ret = (Type[]) java.lang.reflect.Array.newInstance($reifiedType.getArrayElementClass(), (int) caseTypeDeclarations.getSize());
int count = 0;
while((it = iterator.next()) != finished_.get_()){
if(it instanceof ceylon.language.meta.declaration.OpenClassType == false)
continue;
ceylon.language.meta.declaration.OpenClassType caseClassType = (ceylon.language.meta.declaration.OpenClassType)it;
ceylon.language.meta.declaration.ClassDeclaration caseClass = caseClassType.getDeclaration();
if(!caseClass.getAnonymous())
continue;
ValueDeclaration valueDeclaration = caseClass.getContainingPackage().getValue(caseClass.getName());
ceylon.language.meta.model.Value<? extends Type,? super Object> valueModel =
valueDeclaration.<Type,Object>apply($reifiedType, TypeDescriptor.NothingType);
Type value = valueModel.get();
ret[count++] = value;
}
return new ArraySequence<>($reifiedType, ret, 0, count, false);
}

@Ignore
@Override
public TypeDescriptor $getType$() {
Expand Down
5 changes: 4 additions & 1 deletion src/ceylon/language/meta/model/ClassOrInterface.ceylon
Expand Up @@ -16,14 +16,17 @@ shared interface ClassOrInterface<out Type=Anything>

"The declaration for this class or interface."
shared formal actual ClassOrInterfaceDeclaration declaration;

"The extended closed type for this class or interface. Note that the [[Anything|ceylon.language::Anything]] type
has no extended type since it is the top of the type hierarchy."
shared formal ClassModel<Anything, Nothing>? extendedType;

"The list of closed types that this class or interface satisfies."
shared formal InterfaceModel<Anything>[] satisfiedTypes;

"The list of case values for this type. This omits any case type to only contain case values."
shared formal Type[] caseValues;

// FIXME: move all these to Type
// FIXME: introduce MemberClassOrInterface?
// if I do that I have to give up the enumerated type of ClassModel | InterfaceModel here, so let's not do that for now,
Expand Down

0 comments on commit 966c97d

Please sign in to comment.