Search in sources :

Example 11 with TypeMirror

use of org.eclipse.ceylon.model.loader.mirror.TypeMirror in project ceylon by eclipse.

the class AnnotationLoader method readObjectValuesAnnotation.

private LiteralAnnotationTerm readObjectValuesAnnotation(Module moduleScope, AnnotationMirror valueAnnotation, boolean singleValue) {
    if (singleValue) {
        TypeMirror klass = getAnnotationClassValues(valueAnnotation, "value").get(0);
        Type type = modelLoader.obtainType(moduleScope, klass, null, null);
        ObjectLiteralAnnotationTerm term = new ObjectLiteralAnnotationTerm(type);
        return term;
    } else {
        CollectionLiteralAnnotationTerm result = new CollectionLiteralAnnotationTerm(ObjectLiteralAnnotationTerm.FACTORY);
        for (TypeMirror klass : getAnnotationClassValues(valueAnnotation, "value")) {
            Type type = modelLoader.obtainType(moduleScope, klass, null, null);
            result.addElement(new ObjectLiteralAnnotationTerm(type));
        }
        return result;
    }
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) DeclarationType(org.eclipse.ceylon.model.loader.ModelLoader.DeclarationType) ObjectLiteralAnnotationTerm(org.eclipse.ceylon.compiler.java.codegen.ObjectLiteralAnnotationTerm) TypeMirror(org.eclipse.ceylon.model.loader.mirror.TypeMirror) CollectionLiteralAnnotationTerm(org.eclipse.ceylon.compiler.java.codegen.CollectionLiteralAnnotationTerm)

Example 12 with TypeMirror

use of org.eclipse.ceylon.model.loader.mirror.TypeMirror in project ceylon by eclipse.

the class JavacTypeParameter method getBounds.

@Override
public List<TypeMirror> getBounds() {
    if (bounds == null) {
        org.eclipse.ceylon.langtools.tools.javac.util.List<Type> bnds = typeSymbol.getBounds();
        List<TypeMirror> ret = new ArrayList<TypeMirror>(bnds.size());
        for (Type type : bnds) ret.add(new JavacType(type));
        bounds = Collections.unmodifiableList(ret);
    }
    return bounds;
}
Also used : Type(org.eclipse.ceylon.langtools.tools.javac.code.Type) TypeMirror(org.eclipse.ceylon.model.loader.mirror.TypeMirror) ArrayList(java.util.ArrayList)

Example 13 with TypeMirror

use of org.eclipse.ceylon.model.loader.mirror.TypeMirror in project ceylon by eclipse.

the class AbstractModelLoader method getLocalContainer.

private Scope getLocalContainer(Package pkg, ClassMirror classMirror, Declaration declaration) {
    AnnotationMirror localContainerAnnotation = classMirror.getAnnotation(CEYLON_LOCAL_CONTAINER_ANNOTATION);
    String qualifier = getAnnotationStringValue(classMirror, CEYLON_LOCAL_DECLARATION_ANNOTATION, "qualifier");
    // deal with types local to functions in the body of toplevel non-lazy attributes, whose container is ultimately the package
    Boolean isPackageLocal = getAnnotationBooleanValue(classMirror, CEYLON_LOCAL_DECLARATION_ANNOTATION, "isPackageLocal");
    if (BooleanUtil.isTrue(isPackageLocal)) {
        // make sure it still knows it's a local
        declaration.setQualifier(qualifier);
        return null;
    }
    LocalDeclarationContainer methodDecl = null;
    // we get a @LocalContainer annotation for local interfaces
    if (localContainerAnnotation != null) {
        methodDecl = (LocalDeclarationContainer) findLocalContainerFromAnnotationAndSetCompanionClass(pkg, (Interface) declaration, localContainerAnnotation);
    } else {
        // all the other cases stay where they belong
        MethodMirror method = classMirror.getEnclosingMethod();
        if (method == null)
            return null;
        // see where that method belongs
        ClassMirror enclosingClass = method.getEnclosingClass();
        while (enclosingClass.isAnonymous()) {
            // this gives us the method in which the anonymous class is, which should be the one we're looking for
            method = enclosingClass.getEnclosingMethod();
            if (method == null)
                return null;
            // and the method's containing class
            enclosingClass = method.getEnclosingClass();
        }
        // if we are in a setter class, the attribute is declared in the getter class, so look for its declaration there
        TypeMirror getterClass = (TypeMirror) getAnnotationValue(enclosingClass, CEYLON_SETTER_ANNOTATION, "getterClass");
        boolean isSetter = false;
        // we use void.class as default value
        if (getterClass != null && !getterClass.isPrimitive()) {
            enclosingClass = getterClass.getDeclaredClass();
            isSetter = true;
        }
        String javaClassName = enclosingClass.getQualifiedName();
        // make sure we don't go looking in companion classes
        if (javaClassName.endsWith(NamingBase.Suffix.$impl.name()))
            javaClassName = javaClassName.substring(0, javaClassName.length() - 5);
        // find the enclosing declaration
        Declaration enclosingClassDeclaration = convertToDeclaration(pkg.getModule(), javaClassName, DeclarationType.TYPE);
        if (enclosingClassDeclaration instanceof ClassOrInterface) {
            ClassOrInterface containerDecl = (ClassOrInterface) enclosingClassDeclaration;
            // now find the method's declaration
            // FIXME: find the proper overload if any
            String name = method.getName();
            if (method.isConstructor() || name.startsWith(NamingBase.Prefix.$default$.toString())) {
                methodDecl = (LocalDeclarationContainer) containerDecl;
            } else {
                // this is only for error messages
                String type;
                // lots of special cases
                if (isStringAttribute(method)) {
                    name = "string";
                    type = "attribute";
                } else if (isHashAttribute(method)) {
                    name = "hash";
                    type = "attribute";
                } else if (isGetter(method)) {
                    // simple attribute
                    name = getJavaAttributeName(method);
                    type = "attribute";
                } else if (isSetter(method)) {
                    // simple attribute
                    name = getJavaAttributeName(method);
                    type = "attribute setter";
                    isSetter = true;
                } else {
                    type = "method";
                }
                // it can be foo$priv$canonical so get rid of that one first
                if (name.endsWith(NamingBase.Suffix.$canonical$.toString())) {
                    name = name.substring(0, name.length() - 11);
                }
                name = JvmBackendUtil.strip(name, true, method.isPublic() || method.isProtected() || method.isDefaultAccess());
                if (name.indexOf('$') > 0) {
                    // may be a default parameter expression? get the method name which is first
                    name = name.substring(0, name.indexOf('$'));
                }
                methodDecl = (LocalDeclarationContainer) containerDecl.getDirectMember(name, null, false);
                if (methodDecl == null)
                    throw new ModelResolutionException("Failed to load outer " + type + " " + name + " for local type " + classMirror.getQualifiedName().toString());
                // if it's a setter we wanted, let's get it
                if (isSetter) {
                    LocalDeclarationContainer setter = (LocalDeclarationContainer) ((Value) methodDecl).getSetter();
                    if (setter == null)
                        throw new ModelResolutionException("Failed to load outer " + type + " " + name + " for local type " + classMirror.getQualifiedName().toString());
                    methodDecl = setter;
                }
            }
        } else if (enclosingClassDeclaration instanceof LazyFunction) {
            // local and toplevel methods
            methodDecl = (LazyFunction) enclosingClassDeclaration;
        } else if (enclosingClassDeclaration instanceof LazyValue) {
            // local and toplevel attributes
            if (enclosingClassDeclaration.isToplevel() && method.getName().equals(NamingBase.Unfix.set_.name()))
                isSetter = true;
            if (isSetter) {
                LocalDeclarationContainer setter = (LocalDeclarationContainer) ((LazyValue) enclosingClassDeclaration).getSetter();
                if (setter == null)
                    throw new ModelResolutionException("Failed to toplevel attribute setter " + enclosingClassDeclaration.getName() + " for local type " + classMirror.getQualifiedName().toString());
                methodDecl = setter;
            } else
                methodDecl = (LazyValue) enclosingClassDeclaration;
        } else {
            throw new ModelResolutionException("Unknown container type " + enclosingClassDeclaration + " for local type " + classMirror.getQualifiedName().toString());
        }
    }
    // we have the method, now find the proper local qualifier if any
    if (qualifier == null)
        return null;
    declaration.setQualifier(qualifier);
    methodDecl.addLocalDeclaration(declaration);
    return methodDecl;
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) LazyValue(org.eclipse.ceylon.model.loader.model.LazyValue) LocalDeclarationContainer(org.eclipse.ceylon.model.loader.model.LocalDeclarationContainer) LazyFunction(org.eclipse.ceylon.model.loader.model.LazyFunction) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror) AnnotationMirror(org.eclipse.ceylon.model.loader.mirror.AnnotationMirror) MethodMirror(org.eclipse.ceylon.model.loader.mirror.MethodMirror) TypeMirror(org.eclipse.ceylon.model.loader.mirror.TypeMirror) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 14 with TypeMirror

use of org.eclipse.ceylon.model.loader.mirror.TypeMirror in project ceylon by eclipse.

the class AbstractModelLoader method getRepeatableContainer.

public Interface getRepeatableContainer(Class c) {
    if (c instanceof AnnotationProxyClass) {
        AnnotationMirror mirror = ((AnnotationProxyClass) c).iface.classMirror.getAnnotation("java.lang.annotation.Repeatable");
        if (mirror != null) {
            TypeMirror m = (TypeMirror) mirror.getValue();
            Module module = findModuleForClassMirror(m.getDeclaredClass());
            return (Interface) convertDeclaredTypeToDeclaration(module, m, DeclarationType.TYPE);
        }
    }
    return null;
}
Also used : AnnotationMirror(org.eclipse.ceylon.model.loader.mirror.AnnotationMirror) TypeMirror(org.eclipse.ceylon.model.loader.mirror.TypeMirror) AnnotationProxyClass(org.eclipse.ceylon.model.loader.model.AnnotationProxyClass) Module(org.eclipse.ceylon.model.typechecker.model.Module) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface)

Example 15 with TypeMirror

use of org.eclipse.ceylon.model.loader.mirror.TypeMirror in project ceylon by eclipse.

the class AbstractModelLoader method getFunctionalInterfaceAsCallable.

private Type getFunctionalInterfaceAsCallable(Module moduleScope, Scope scope, TypeMirror typeMirror) {
    try {
        FunctionalInterfaceType functionalInterfaceType = getFunctionalInterfaceType(typeMirror);
        Type returnType = obtainType(moduleScope, functionalInterfaceType.getReturnType(), scope, TypeLocation.TOPLEVEL);
        java.util.List<Type> modelParameterTypes = new ArrayList<Type>(functionalInterfaceType.getParameterTypes().size());
        for (TypeMirror parameterType : functionalInterfaceType.getParameterTypes()) {
            Type modelParameterType = obtainType(moduleScope, parameterType, scope, TypeLocation.TOPLEVEL);
            modelParameterTypes.add(modelParameterType);
        }
        org.eclipse.ceylon.model.typechecker.model.Type parameterTuple = typeFactory.getTupleType(modelParameterTypes, functionalInterfaceType.isVariadic(), false, -1);
        org.eclipse.ceylon.model.typechecker.model.Type callableType = typeFactory.getCallableDeclaration().appliedType(null, Arrays.asList(returnType, parameterTuple));
        return callableType;
    } catch (ModelResolutionException x) {
        return logModelResolutionException(x, scope, "Failure to turn functional interface to Callable type");
    }
}
Also used : FunctionalInterfaceType(org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) FunctionalInterfaceType(org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType) TypeMirror(org.eclipse.ceylon.model.loader.mirror.TypeMirror) ArrayList(java.util.ArrayList) Type(org.eclipse.ceylon.model.typechecker.model.Type)

Aggregations

TypeMirror (org.eclipse.ceylon.model.loader.mirror.TypeMirror)23 FunctionalInterfaceType (org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType)10 Type (org.eclipse.ceylon.model.typechecker.model.Type)10 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)10 ArrayList (java.util.ArrayList)7 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)6 AnnotationMirror (org.eclipse.ceylon.model.loader.mirror.AnnotationMirror)5 Type (org.eclipse.ceylon.langtools.tools.javac.code.Type)4 LazyFunction (org.eclipse.ceylon.model.loader.model.LazyFunction)4 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)4 Type (java.lang.reflect.Type)3 ClassMirror (org.eclipse.ceylon.model.loader.mirror.ClassMirror)3 TypeParameterMirror (org.eclipse.ceylon.model.loader.mirror.TypeParameterMirror)3 LazyClass (org.eclipse.ceylon.model.loader.model.LazyClass)3 LazyValue (org.eclipse.ceylon.model.loader.model.LazyValue)3 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)3 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)3 Function (org.eclipse.ceylon.model.typechecker.model.Function)3 Module (org.eclipse.ceylon.model.typechecker.model.Module)3 ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)3