Search in sources :

Example 1 with ClassOrInterfaceDeclaration

use of ceylon.language.meta.declaration.ClassOrInterfaceDeclaration in project ceylon by eclipse.

the class ClassOrInterfaceImpl method getCaseValues.

@Override
@TypeInfo("ceylon.language::Sequential<Type>")
public ceylon.language.Sequential<? extends Type> getCaseValues() {
    if (!((ClassOrInterface) declaration.declaration).isAbstract()) {
        // optimization: a concrete class cannot have cases
        return (Sequential) empty_.get_();
    }
    Sequential<? extends ceylon.language.meta.declaration.OpenType> caseTypeDeclarations = getDeclaration().getCaseTypes();
    Iterator<? extends ceylon.language.meta.declaration.OpenType> iterator = caseTypeDeclarations.iterator();
    Object it;
    Array<Type> ret = new Array<Type>($reifiedType, (int) caseTypeDeclarations.getSize(), (Type) null);
    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;
        Type value = null;
        Object container = caseClass.getContainer();
        while (true) {
            if (container instanceof ceylon.language.meta.declaration.Package) {
                ValueDeclaration valueDeclaration = ((ceylon.language.meta.declaration.Package) container).getValue(caseClass.getName());
                ceylon.language.meta.model.Value<? extends Type, ? super Object> valueModel = valueDeclaration.<Type, Object>apply($reifiedType, TypeDescriptor.NothingType);
                value = valueModel.get();
                break;
            } else {
                if (container instanceof ClassOrInterfaceDeclaration) {
                    ValueDeclaration valueDeclaration = ((ClassOrInterfaceDeclaration) container).getMemberDeclaration(ValueDeclaration.$TypeDescriptor$, caseClass.getName());
                    Attribute a = valueDeclaration.memberApply($reifiedType, $reifiedType, Nothing.NothingType, this);
                    value = (Type) a.bind(null).get();
                }
                // other nestable decls can't contain members, so keep looking up scopes
                container = ((NestableDeclaration) container).getContainer();
            }
            if (value != null) {
                break;
            }
        }
        if (value == null && !producedType.isNull()) {
            throw new AssertionError("case " + caseClassType + " of " + this + " not found");
        }
        ret.set(count++, value);
    }
    return ret.take(count).sequence();
}
Also used : ClassOrInterfaceDeclaration(ceylon.language.meta.declaration.ClassOrInterfaceDeclaration) Attribute(ceylon.language.meta.model.Attribute) ValueDeclaration(ceylon.language.meta.declaration.ValueDeclaration) ClassDeclaration(ceylon.language.meta.declaration.ClassDeclaration) Array(ceylon.language.Array) ReifiedType(org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType) Sequential(ceylon.language.Sequential) AssertionError(ceylon.language.AssertionError) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 2 with ClassOrInterfaceDeclaration

use of ceylon.language.meta.declaration.ClassOrInterfaceDeclaration in project ceylon by eclipse.

the class Util method javaClassForModel.

// Used by the code generator
// Copied from ceylon.interop.java
@SuppressWarnings("unchecked")
public static <T> java.lang.Class<? extends T> javaClassForModel(ClassOrInterface<? extends T> model) {
    ClassOrInterfaceDeclaration decl = model.getDeclaration();
    if (decl instanceof ClassOrInterfaceDeclarationImpl) {
        ClassOrInterfaceDeclarationImpl ci = (ClassOrInterfaceDeclarationImpl) decl;
        String prefix = null;
        while (ci.getJavaClass().equals(ObjectArray.class)) {
            model = ((ClassOrInterface) model.getTypeArgumentList().getFromFirst(0));
            decl = model.getDeclaration();
            ci = (ClassOrInterfaceDeclarationImpl) decl;
            prefix = prefix == null ? "[" : prefix + "[";
        }
        java.lang.Class result = (java.lang.Class<? extends T>) classErasure(ci.getJavaClass());
        if (prefix != null) {
            String name = result.isArray() && result.getComponentType().isPrimitive() ? prefix + result.getName() : prefix + "L" + result.getName() + ";";
            try {
                result = java.lang.Class.forName(name, false, result.getClassLoader());
            } catch (ClassNotFoundException e1) {
                rethrow(e1);
            }
        }
        return result;
    }
    throw new ceylon.language.AssertionError("Unsupported declaration type: " + decl);
}
Also used : ClassOrInterface(ceylon.language.meta.model.ClassOrInterface) ClassOrInterfaceDeclaration(ceylon.language.meta.declaration.ClassOrInterfaceDeclaration) AssertionError(ceylon.language.AssertionError) ClassOrInterfaceDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl) Class(org.eclipse.ceylon.compiler.java.metadata.Class)

Aggregations

AssertionError (ceylon.language.AssertionError)2 ClassOrInterfaceDeclaration (ceylon.language.meta.declaration.ClassOrInterfaceDeclaration)2 Array (ceylon.language.Array)1 Sequential (ceylon.language.Sequential)1 ClassDeclaration (ceylon.language.meta.declaration.ClassDeclaration)1 ValueDeclaration (ceylon.language.meta.declaration.ValueDeclaration)1 Attribute (ceylon.language.meta.model.Attribute)1 ClassOrInterface (ceylon.language.meta.model.ClassOrInterface)1 Class (org.eclipse.ceylon.compiler.java.metadata.Class)1 TypeInfo (org.eclipse.ceylon.compiler.java.metadata.TypeInfo)1 Metamodel (org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)1 ClassOrInterfaceDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl)1 ReifiedType (org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType)1