Search in sources :

Example 6 with ClassOrInterfaceDeclarationImpl

use of org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl in project ceylon by eclipse.

the class Metamodel method getMetamodel.

public static ceylon.language.meta.declaration.FunctionDeclaration getMetamodel(Function method) {
    // find its container
    Scope container = method.getContainer();
    if (container instanceof org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) {
        org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl classOrInterface = (ClassOrInterfaceDeclarationImpl) getOrCreateMetamodel((org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) container);
        // now find the method
        ceylon.language.meta.declaration.FunctionDeclaration ret = classOrInterface.findMethod(method.getName());
        if (ret == null)
            throw Metamodel.newModelError("Failed to find method " + method.getName() + " in " + container);
        return ret;
    }
    if (container instanceof org.eclipse.ceylon.model.typechecker.model.Package) {
        ceylon.language.meta.declaration.Package pkg = getOrCreateMetamodel((org.eclipse.ceylon.model.typechecker.model.Package) container);
        ceylon.language.meta.declaration.FunctionDeclaration ret = pkg.getFunction(method.getName());
        if (ret == null)
            throw Metamodel.newModelError("Failed to find method " + method.getName() + " in " + container);
        return ret;
    }
    throw Metamodel.newModelError("Unsupported method container for " + method.getName() + ": " + container);
}
Also used : ClassOrInterface(ceylon.language.meta.model.ClassOrInterface) ClassOrInterfaceDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ClassOrInterfaceDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) LazyPackage(org.eclipse.ceylon.model.loader.model.LazyPackage) Package(ceylon.language.meta.declaration.Package) Package(ceylon.language.meta.declaration.Package)

Example 7 with ClassOrInterfaceDeclarationImpl

use of org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl 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)

Example 8 with ClassOrInterfaceDeclarationImpl

use of org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl in project ceylon by eclipse.

the class Metamodel method parseEnumerationReference.

@SuppressWarnings("unchecked")
public static <T> T parseEnumerationReference(java.lang.Class<T> klass) {
    ClassOrInterfaceDeclarationImpl decl = (ClassOrInterfaceDeclarationImpl) getOrCreateMetamodel(klass);
    String getterName = NamingBase.getGetterName(decl.declaration);
    try {
        java.lang.reflect.Method method = klass.getDeclaredMethod(getterName);
        method.setAccessible(true);
        return (T) method.invoke(null);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        /* aka ReflectiveOperationException */
        throw Metamodel.newModelError(e.toString());
    }
}
Also used : ClassOrInterfaceDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl) Method(java.lang.reflect.Method) IncompatibleTypeException(ceylon.language.meta.model.IncompatibleTypeException) InvocationException(ceylon.language.meta.model.InvocationException) ModuleLoadException(org.eclipse.ceylon.model.runtime.CeylonModuleClassLoader.ModuleLoadException) TypeApplicationException(ceylon.language.meta.model.TypeApplicationException)

Example 9 with ClassOrInterfaceDeclarationImpl

use of org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl in project ceylon by eclipse.

the class ClassOrInterfaceImpl method getDeclaredInterface.

@SuppressWarnings({ "unchecked", "hiding" })
@Override
@TypeParameters({ @TypeParameter(value = "Container"), @TypeParameter(value = "Type") })
@TypeInfo("ceylon.language.meta.model::MemberInterface<Container,Type>|ceylon.language::Null")
public <Container, Type> ceylon.language.meta.model.MemberInterface<Container, Type> getDeclaredInterface(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedType, String name, @Name("types") @Sequenced Sequential<? extends ceylon.language.meta.model.Type<?>> types) {
    checkInit();
    // do not return the attribute if the container is not a subtype of this type
    org.eclipse.ceylon.model.typechecker.model.Type reifiedContainer = Metamodel.getProducedType($reifiedContainer);
    if (!reifiedContainer.isSubtypeOf(producedType))
        throw new IncompatibleTypeException("Specified container type '" + reifiedContainer.asString() + "' is not a valid subtype of this type");
    final ClassOrInterfaceDeclarationImpl type = declaration.findDeclaredType(name);
    if (type == null)
        return null;
    if (type instanceof InterfaceDeclarationImpl == false)
        throw new IncompatibleTypeException("Specified member is not an interface: " + name);
    return (ceylon.language.meta.model.MemberInterface<Container, Type>) type.memberApply($reifiedContainer, $reifiedType, (ceylon.language.meta.model.Type<Container>) this, types);
}
Also used : ReifiedType(org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType) ClassOrInterfaceDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) IncompatibleTypeException(ceylon.language.meta.model.IncompatibleTypeException) InterfaceDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.InterfaceDeclarationImpl) ClassOrInterfaceDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 10 with ClassOrInterfaceDeclarationImpl

use of org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl in project ceylon by eclipse.

the class ClassOrInterfaceImpl method getClassOrInterface.

@Override
@TypeParameters({ @TypeParameter(value = "Container"), @TypeParameter(value = "Kind", satisfies = "ceylon.language.meta.model::ClassOrInterface<ceylon.language::Anything,ceylon.language::Nothing>") })
@TypeInfo("ceylon.language.meta.model::Member<Container,Kind>|ceylon.language::Null")
public <Container, Kind extends ceylon.language.meta.model.ClassOrInterface<? extends java.lang.Object>> ceylon.language.meta.model.Member<Container, Kind> getClassOrInterface(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedKind, String name, @Name("types") @Sequenced Sequential<? extends ceylon.language.meta.model.Type<?>> types) {
    checkInit();
    final ClassOrInterfaceDeclarationImpl type = declaration.findType(name);
    if (type == null)
        return null;
    MemberLookup<ClassOrInterfaceDeclarationImpl, Container> lookup = lookupMember(ClassOrInterfaceDeclarationImpl.$TypeDescriptor$, $reifiedContainer, type);
    if (lookup == null)
        return null;
    return applyClassOrInterface($reifiedContainer, $reifiedKind, lookup.declaration, types);
}
Also used : ClassOrInterfaceDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Aggregations

ClassOrInterfaceDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl)10 IncompatibleTypeException (ceylon.language.meta.model.IncompatibleTypeException)6 TypeInfo (org.eclipse.ceylon.compiler.java.metadata.TypeInfo)6 TypeParameters (org.eclipse.ceylon.compiler.java.metadata.TypeParameters)6 Metamodel (org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)4 ClassOrInterface (ceylon.language.meta.model.ClassOrInterface)2 ClassDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassDeclarationImpl)2 InterfaceDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.InterfaceDeclarationImpl)2 AssertionError (ceylon.language.AssertionError)1 ClassOrInterfaceDeclaration (ceylon.language.meta.declaration.ClassOrInterfaceDeclaration)1 Package (ceylon.language.meta.declaration.Package)1 InvocationException (ceylon.language.meta.model.InvocationException)1 TypeApplicationException (ceylon.language.meta.model.TypeApplicationException)1 Method (java.lang.reflect.Method)1 Class (org.eclipse.ceylon.compiler.java.metadata.Class)1 ClassOrInterfaceImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassOrInterfaceImpl)1 ReifiedType (org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType)1 LazyPackage (org.eclipse.ceylon.model.loader.model.LazyPackage)1 ModuleLoadException (org.eclipse.ceylon.model.runtime.CeylonModuleClassLoader.ModuleLoadException)1 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)1