Search in sources :

Example 11 with ClassMirror

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

the class RuntimeModelLoader method lazyLoadModule.

@Override
public void lazyLoadModule(Module module) {
    if (isDynamicMetamodel()) {
        if (loadCompiledModule(module)) {
            String pkgName = module.getNameAsString();
            ClassMirror moduleClass = findModuleClass(module, pkgName);
            Class<?> klass = ((ReflectionClass) moduleClass).klass;
            String path = klass.getProtectionDomain().getCodeSource().getLocation().getPath();
            Unit u = new Unit();
            // FIXME: find path name?
            u.setFilename(path);
            u.setFullPath(path);
            module.setUnit(u);
        }
    }
}
Also used : ReflectionClass(org.eclipse.ceylon.model.loader.impl.reflect.mirror.ReflectionClass) Unit(org.eclipse.ceylon.model.typechecker.model.Unit) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror)

Example 12 with ClassMirror

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

the class AbstractModelLoader method convertDeclaredTypeToDeclaration.

private Declaration convertDeclaredTypeToDeclaration(Module moduleScope, TypeMirror type, DeclarationType declarationType) {
    // SimpleReflType does not do declared class so we make an exception for it
    String typeName = type.getQualifiedName();
    if (type instanceof SimpleReflType) {
        Module module = null;
        switch(((SimpleReflType) type).getModule()) {
            case CEYLON:
                module = getLanguageModule();
                break;
            case JDK:
                module = getJDKBaseModule();
                break;
        }
        return convertToDeclaration(module, typeName, declarationType);
    }
    ClassMirror classMirror = type.getDeclaredClass();
    Module module = findModuleForClassMirror(classMirror);
    if (module != null && isImported(moduleScope, module)) {
        return convertToDeclaration(module, typeName, declarationType);
    } else {
        if (module != null && isFlatClasspath() && isMavenModule(moduleScope))
            return convertToDeclaration(module, typeName, declarationType);
        String error = "Declaration '" + typeName + "' could not be found in module '" + moduleScope.getNameAsString() + "' or its imported modules";
        if (module != null && !module.isDefaultModule())
            error += " but was found in the non-imported module '" + module.getNameAsString() + "'";
        return logModelResolutionException(null, moduleScope, error).getDeclaration();
    }
}
Also used : Module(org.eclipse.ceylon.model.typechecker.model.Module) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror)

Example 13 with ClassMirror

use of org.eclipse.ceylon.model.loader.mirror.ClassMirror 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 ClassMirror

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

the class AbstractModelLoader method loadPackageDescriptor.

private void loadPackageDescriptor(LazyPackage pkg) {
    if (!pkg.getModule().isAvailable())
        lazyLoadModule(pkg.getModule());
    // Consider the descriptor loaded, we're not going to change our mind
    pkg.setDescriptorLoaded(true);
    // if we're bootstrapping
    if (isBootstrap && pkg.getQualifiedNameString().startsWith(CEYLON_LANGUAGE)) {
        return;
    }
    // let's not load package descriptors for Java modules
    if (pkg.getModule() != null && ((LazyModule) pkg.getModule()).isJava()) {
        pkg.setShared(((LazyModule) pkg.getModule()).isExportedJavaPackage(pkg.getNameAsString()));
        return;
    }
    String quotedQualifiedName = JVMModuleUtil.quoteJavaKeywords(pkg.getQualifiedNameString());
    // FIXME: not sure the toplevel package can have a package declaration
    String className = quotedQualifiedName.isEmpty() ? NamingBase.PACKAGE_DESCRIPTOR_CLASS_NAME : quotedQualifiedName + "." + NamingBase.PACKAGE_DESCRIPTOR_CLASS_NAME;
    logVerbose("[Trying to look up package from " + className + "]");
    Module module = pkg.getModule();
    if (module == null)
        throw new RuntimeException("Assertion failed: module is null for package " + pkg.getNameAsString());
    ClassMirror packageClass = loadClass(module, quotedQualifiedName, className);
    if (packageClass == null) {
        logVerbose("[Failed to complete " + className + "]");
        // missing: leave it private
        return;
    }
    // did we compile it from source or class?
    if (packageClass.isLoadedFromSource()) {
        // must have come from source, in which case we walked it and
        // loaded its values already
        logVerbose("[We are compiling the package " + className + "]");
        return;
    }
    loadCompiledPackage(packageClass, pkg);
}
Also used : Module(org.eclipse.ceylon.model.typechecker.model.Module) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror)

Example 15 with ClassMirror

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

the class AbstractModelLoader method addInnerClassesFromMirror.

private void addInnerClassesFromMirror(ClassOrInterface klass, ClassMirror classMirror) {
    boolean isJDK = isFromJDK(classMirror);
    Module module = ModelUtil.getModule(klass);
    for (ClassMirror innerClass : classMirror.getDirectInnerClasses()) {
        // We skip members marked with @Ignore
        if (innerClass.getAnnotation(CEYLON_IGNORE_ANNOTATION) != null)
            continue;
        // We skip anonymous inner classes
        if (innerClass.isAnonymous())
            continue;
        // We skip private classes, otherwise the JDK has a ton of unresolved things
        if (isJDK && !innerClass.isPublic())
            continue;
        // convert it
        convertToDeclaration(module, klass, innerClass, DeclarationType.TYPE);
    // no need to set its container as that's now handled by convertToDeclaration
    }
}
Also used : Module(org.eclipse.ceylon.model.typechecker.model.Module) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror)

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