Search in sources :

Example 1 with ClassMirror

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

the class AnnotationLoader method setPrimaryFromAnnotationInvocationAnnotation.

private void setPrimaryFromAnnotationInvocationAnnotation(AnnotationMirror annotationInvocationAnnotation, AnnotationInvocation ai) {
    TypeMirror annotationType = (TypeMirror) annotationInvocationAnnotation.getValue(AbstractModelLoader.CEYLON_ANNOTATION_INSTANTIATION_ANNOTATION_MEMBER);
    ClassMirror annotationClassMirror = annotationType.getDeclaredClass();
    Module module = modelLoader.findModuleForClassMirror(annotationClassMirror);
    if (annotationClassMirror.getAnnotation(AbstractModelLoader.CEYLON_METHOD_ANNOTATION) != null) {
        ai.setPrimary((Function) modelLoader.convertToDeclaration(module, annotationClassMirror, DeclarationType.VALUE));
    } else {
        ai.setPrimary((Class) modelLoader.convertToDeclaration(module, annotationClassMirror, DeclarationType.TYPE));
    }
}
Also used : TypeMirror(org.eclipse.ceylon.model.loader.mirror.TypeMirror) Module(org.eclipse.ceylon.model.typechecker.model.Module) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror)

Example 2 with ClassMirror

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

the class CeylonModelLoader method loadPackage.

@Override
public boolean loadPackage(Module module, String packageName, boolean loadDeclarations) {
    synchronized (getLock()) {
        // abort if we already loaded it, but only record that we loaded it if we want
        // to load the declarations, because merely calling complete() on the package
        // is OK
        packageName = Util.quoteJavaKeywords(packageName);
        String cacheKey = cacheKeyByModule(module, packageName);
        if (loadDeclarations) {
            if (!loadedPackages.add(cacheKey)) {
                return true;
            }
        } else {
            Boolean exists = packageExistence.get(cacheKey);
            if (exists != null)
                return exists.booleanValue();
        }
        PackageSymbol ceylonPkg = packageName.equals("") ? syms().unnamedPackage : reader.enterPackage(names.fromString(packageName));
        if (loadDeclarations) {
            logVerbose("load package " + packageName + " full");
            ceylonPkg.complete();
            /*
                 * Eventually this will go away as we get a hook from the typechecker to load on demand, but
                 * for now the typechecker requires at least ceylon.language to be loaded 
                 */
            for (Symbol m : ceylonPkg.members().getElements()) {
                // skip things that are not classes (perhaps package-info?)
                if (!(m instanceof ClassSymbol))
                    continue;
                ClassSymbol enclosingClass = getEnclosing((ClassSymbol) m);
                if (enclosingClass == m && !Util.isLoadedFromSource(enclosingClass)) {
                    m.complete();
                    // avoid anonymous and local classes
                    if (isAnonymousOrLocal((ClassSymbol) m))
                        continue;
                    // avoid member classes
                    if (((ClassSymbol) m).getNestingKind() != NestingKind.TOP_LEVEL)
                        continue;
                    // skip module and package descriptors
                    if (isModuleOrPackageDescriptorName(m.name.toString()))
                        continue;
                    ClassMirror classMirror = lookupClassMirror(module, m.getQualifiedName().toString());
                    // So ATM we just avoid it, presumably we don't support what it does anyways
                    if (classMirror != null)
                        convertToDeclaration(module, classMirror, DeclarationType.VALUE);
                }
            }
            if (module.getNameAsString().equals(JAVA_BASE_MODULE_NAME) && packageName.equals("java.lang"))
                loadJavaBaseExtras();
            // might be too late
            return ceylonPkg.members().getElements().iterator().hasNext();
        } else {
            logVerbose("load package " + packageName + " light");
            try {
                // it is cheaper to verify that we have a class file somewhere than to complete the whole package
                // just to check for its existence
                Iterable<JavaFileObject> list = fileManager.list(PLATFORM_CLASS_PATH, packageName, EnumSet.of(JavaFileObject.Kind.CLASS), false);
                if (list.iterator().hasNext()) {
                    packageExistence.put(cacheKey, Boolean.TRUE);
                    return true;
                }
                list = fileManager.list(CLASS_PATH, packageName, EnumSet.of(JavaFileObject.Kind.CLASS), false);
                if (list.iterator().hasNext()) {
                    packageExistence.put(cacheKey, Boolean.TRUE);
                    return true;
                } else {
                    packageExistence.put(cacheKey, Boolean.FALSE);
                    return false;
                }
            } catch (IOException e) {
                return false;
            }
        }
    }
}
Also used : PackageSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.PackageSymbol) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) MethodSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.MethodSymbol) TypeSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.TypeSymbol) ClassSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.ClassSymbol) Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol) PackageSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.PackageSymbol) ClassSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.ClassSymbol) IOException(java.io.IOException) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror)

Example 3 with ClassMirror

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

the class CeylonModelLoader method lookupNewClassMirror.

@Override
public ClassMirror lookupNewClassMirror(Module module, String name) {
    synchronized (getLock()) {
        ClassMirror classMirror = lookupNewClassMirror(name);
        if (classMirror == null)
            return null;
        Module classMirrorModule = findModuleForClassMirror(classMirror);
        if (classMirrorModule == null) {
            logVerbose("Found a class mirror with no module");
            return null;
        }
        // make sure it's imported
        if (isImported(module, classMirrorModule)) {
            return classMirror;
        }
        logVerbose("Found a class mirror that is not imported: " + name);
        return null;
    }
}
Also used : Module(org.eclipse.ceylon.model.typechecker.model.Module) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror)

Example 4 with ClassMirror

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

the class AbstractModelLoader method addInnerClassesFromAnnotation.

private void addInnerClassesFromAnnotation(ClassOrInterface klass, AnnotationMirror membersAnnotation) {
    @SuppressWarnings("unchecked") List<AnnotationMirror> members = (List<AnnotationMirror>) membersAnnotation.getValue();
    for (AnnotationMirror member : members) {
        TypeMirror javaClassMirror = (TypeMirror) member.getValue("klass");
        String javaClassName;
        // void.class is the default value, I guess it's a primitive?
        if (javaClassMirror != null && !javaClassMirror.isPrimitive()) {
            javaClassName = javaClassMirror.getQualifiedName();
        } else {
            // we get the class name as a string
            String name = (String) member.getValue("javaClassName");
            ClassMirror container = null;
            if (klass instanceof LazyClass) {
                container = ((LazyClass) klass).classMirror;
            } else if (klass instanceof LazyInterface) {
                if (((LazyInterface) klass).isCeylon())
                    container = ((LazyInterface) klass).companionClass;
                else
                    container = ((LazyInterface) klass).classMirror;
            }
            if (container == null)
                throw new ModelResolutionException("Unknown container type: " + klass + " when trying to load inner class " + name);
            javaClassName = container.getQualifiedName() + "$" + name;
        }
        Declaration innerDecl = convertToDeclaration(ModelUtil.getModuleContainer(klass), klass, javaClassName, DeclarationType.TYPE);
        if (innerDecl == null)
            throw new ModelResolutionException("Failed to load inner type " + javaClassName + " for outer type " + klass.getQualifiedNameString());
        if (shouldLinkNatives(innerDecl)) {
            initNativeHeaderMember(innerDecl);
        }
    }
}
Also used : AnnotationMirror(org.eclipse.ceylon.model.loader.mirror.AnnotationMirror) TypeMirror(org.eclipse.ceylon.model.loader.mirror.TypeMirror) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface) List(java.util.List) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror)

Example 5 with ClassMirror

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

the class AbstractModelLoader method findLocalContainerFromAnnotationAndSetCompanionClass.

private Scope findLocalContainerFromAnnotationAndSetCompanionClass(Package pkg, Interface declaration, AnnotationMirror localContainerAnnotation) {
    @SuppressWarnings("unchecked") List<String> path = (List<String>) localContainerAnnotation.getValue("path");
    // we start at the package
    Scope scope = pkg;
    for (String name : path) {
        scope = (Scope) getDirectMember(scope, name);
    }
    String companionClassName = (String) localContainerAnnotation.getValue("companionClassName");
    if (companionClassName == null || companionClassName.isEmpty()) {
        declaration.setCompanionClassNeeded(false);
        return scope;
    }
    ClassMirror container;
    Scope javaClassScope;
    if (scope instanceof TypedDeclaration && ((TypedDeclaration) scope).isMember())
        javaClassScope = scope.getContainer();
    else
        javaClassScope = scope;
    if (javaClassScope instanceof LazyInterface) {
        container = ((LazyInterface) javaClassScope).companionClass;
    } else if (javaClassScope instanceof LazyClass) {
        container = ((LazyClass) javaClassScope).classMirror;
    } else if (javaClassScope instanceof LazyValue) {
        container = ((LazyValue) javaClassScope).classMirror;
    } else if (javaClassScope instanceof LazyFunction) {
        container = ((LazyFunction) javaClassScope).classMirror;
    } else if (javaClassScope instanceof SetterWithLocalDeclarations) {
        container = ((SetterWithLocalDeclarations) javaClassScope).classMirror;
    } else {
        throw new ModelResolutionException("Unknown scope class: " + javaClassScope);
    }
    String qualifiedCompanionClassName = container.getQualifiedName() + "$" + companionClassName;
    ClassMirror companionClassMirror = lookupClassMirror(pkg.getModule(), qualifiedCompanionClassName);
    if (companionClassMirror == null)
        throw new ModelResolutionException("Could not find companion class mirror: " + qualifiedCompanionClassName);
    ((LazyInterface) declaration).companionClass = companionClassMirror;
    return scope;
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) LazyValue(org.eclipse.ceylon.model.loader.model.LazyValue) LazyFunction(org.eclipse.ceylon.model.loader.model.LazyFunction) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface) SetterWithLocalDeclarations(org.eclipse.ceylon.model.loader.model.SetterWithLocalDeclarations) List(java.util.List) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass)

Aggregations

ClassMirror (org.eclipse.ceylon.model.loader.mirror.ClassMirror)16 Module (org.eclipse.ceylon.model.typechecker.model.Module)7 LazyModule (org.eclipse.ceylon.model.loader.model.LazyModule)5 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)5 LinkedList (java.util.LinkedList)4 LazyClass (org.eclipse.ceylon.model.loader.model.LazyClass)4 LazyInterface (org.eclipse.ceylon.model.loader.model.LazyInterface)4 LazyValue (org.eclipse.ceylon.model.loader.model.LazyValue)4 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)4 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AnnotationMirror (org.eclipse.ceylon.model.loader.mirror.AnnotationMirror)3 TypeMirror (org.eclipse.ceylon.model.loader.mirror.TypeMirror)3 LazyFunction (org.eclipse.ceylon.model.loader.model.LazyFunction)3 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)3 ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)3 IOException (java.io.IOException)2 MethodMirror (org.eclipse.ceylon.model.loader.mirror.MethodMirror)2 FieldValue (org.eclipse.ceylon.model.loader.model.FieldValue)2