Search in sources :

Example 56 with Module

use of org.eclipse.ceylon.model.typechecker.model.Module in project ceylon by eclipse.

the class AbstractModelLoader method addMethod.

private Function addMethod(ClassOrInterface klass, MethodMirror methodMirror, ClassMirror classMirror, boolean isCeylon, boolean isOverloaded, boolean isNativeHeader, boolean isCoercedMethod) {
    JavaMethod method = new JavaMethod(methodMirror);
    String methodName = methodMirror.getName();
    method.setCoercionPoint(isCoercedMethod);
    method.setContainer(klass);
    method.setScope(klass);
    method.setRealName(methodName);
    method.setUnit(klass.getUnit());
    method.setOverloaded(isOverloaded);
    method.setVariadic(methodMirror.isVariadic());
    Type type = null;
    try {
        setMethodOrValueFlags(klass, methodMirror, method, isCeylon);
    } catch (ModelResolutionException x) {
        // collect an error in its type
        type = logModelResolutionException(x, klass, "method '" + methodMirror.getName() + "' (checking if it is an overriding method)");
    }
    if (methodName.equals("hash") || methodName.equals("string"))
        method.setName(methodName + "_method");
    else {
        String ceylonName = JvmBackendUtil.strip(methodName, isCeylon, method.isShared());
        if (!isCeylon && !JvmBackendUtil.isInitialLowerCase(ceylonName))
            ceylonName = NamingBase.getJavaBeanName(ceylonName);
        method.setName(ceylonName);
    }
    method.setDefaultedAnnotation(methodMirror.isDefault());
    // type params first
    try {
        setTypeParameters(method, methodMirror, isCeylon);
    } catch (ModelResolutionException x) {
        if (type == null) {
            type = logModelResolutionException(x, klass, "method '" + methodMirror.getName() + "' (loading type parameters)");
        }
    }
    Module module = ModelUtil.getModuleContainer(method);
    // do not log an additional error if we had one from checking if it was overriding
    if (type == null)
        type = obtainType(methodMirror.getReturnType(), methodMirror, method, module, "method '" + methodMirror.getName() + "'", klass);
    NullStatus nullPolicy = getUncheckedNullPolicy(isCeylon, methodMirror.getReturnType(), methodMirror);
    switch(nullPolicy) {
        case Optional:
            if (!isCeylon) {
                type = makeOptionalTypePreserveUnderlyingType(type, module);
            }
            break;
        case UncheckedNull:
            method.setUncheckedNullType(true);
            break;
    }
    if (type.isCached()) {
        type = type.clone();
    }
    method.setType(type);
    // now its parameters
    if (isEqualsMethod(methodMirror))
        setEqualsParameters(method, methodMirror);
    else
        setParameters(method, classMirror, methodMirror, isCeylon, klass, isCoercedMethod);
    type.setRaw(isRaw(module, methodMirror.getReturnType()));
    markDeclaredVoid(method, methodMirror);
    markUnboxed(method, methodMirror, methodMirror.getReturnType());
    markSmall(method, methodMirror.getReturnType());
    markTypeErased(method, methodMirror, methodMirror.getReturnType());
    markUntrustedType(method, methodMirror, methodMirror.getReturnType());
    method.setDeprecated(isDeprecated(methodMirror));
    setAnnotations(method, methodMirror, isNativeHeader);
    klass.addMember(method);
    ModelUtil.setVisibleScope(method);
    addLocalDeclarations(method, classMirror, methodMirror);
    return method;
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) FunctionalInterfaceType(org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType) JavaMethod(org.eclipse.ceylon.model.loader.model.JavaMethod) Module(org.eclipse.ceylon.model.typechecker.model.Module) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule)

Example 57 with Module

use of org.eclipse.ceylon.model.typechecker.model.Module in project ceylon by eclipse.

the class AbstractModelLoader method setTypeParametersFromAnnotations.

// from our annotation
private void setTypeParametersFromAnnotations(Scope scope, List<TypeParameter> params, AnnotatedMirror mirror, List<AnnotationMirror> typeParameterAnnotations, List<TypeParameterMirror> typeParameterMirrors) {
    // We must first add every type param, before we resolve the bounds, which can
    // refer to type params.
    String selfTypeName = getSelfTypeFromAnnotations(mirror);
    int i = 0;
    for (AnnotationMirror typeParamAnnotation : typeParameterAnnotations) {
        TypeParameter param = new TypeParameter();
        param.setUnit(scope.getUnit());
        param.setContainer(scope);
        param.setScope(scope);
        ModelUtil.setVisibleScope(param);
        param.setDeclaration((Declaration) scope);
        // let's not trigger the lazy-loading if we're completing a LazyClass/LazyInterface
        if (scope instanceof LazyContainer)
            ((LazyContainer) scope).addMember(param);
        else
            // must be a method
            scope.addMember(param);
        param.setName((String) typeParamAnnotation.getValue("value"));
        param.setExtendedType(typeFactory.getAnythingType());
        if (i < typeParameterMirrors.size()) {
            TypeParameterMirror typeParameterMirror = typeParameterMirrors.get(i);
            param.setNonErasedBounds(hasNonErasedBounds(typeParameterMirror));
        }
        String varianceName = (String) typeParamAnnotation.getValue("variance");
        if (varianceName != null) {
            if (varianceName.equals("IN")) {
                param.setContravariant(true);
            } else if (varianceName.equals("OUT"))
                param.setCovariant(true);
        }
        // If this is a self type param then link it to its type's declaration
        if (param.getName().equals(selfTypeName)) {
            param.setSelfTypedDeclaration((TypeDeclaration) scope);
        }
        params.add(param);
        i++;
    }
    Module moduleScope = ModelUtil.getModuleContainer(scope);
    // Now all type params have been set, we can resolve the references parts
    Iterator<TypeParameter> paramsIterator = params.iterator();
    for (AnnotationMirror typeParamAnnotation : typeParameterAnnotations) {
        TypeParameter param = paramsIterator.next();
        @SuppressWarnings("unchecked") List<String> satisfiesAttribute = (List<String>) typeParamAnnotation.getValue("satisfies");
        setListOfTypes(param.getSatisfiedTypes(), satisfiesAttribute, scope, moduleScope, "type parameter '" + param.getName() + "' satisfied types");
        @SuppressWarnings("unchecked") List<String> caseTypesAttribute = (List<String>) typeParamAnnotation.getValue("caseTypes");
        if (caseTypesAttribute != null && !caseTypesAttribute.isEmpty())
            param.setCaseTypes(new LinkedList<Type>());
        setListOfTypes(param.getCaseTypes(), caseTypesAttribute, scope, moduleScope, "type parameter '" + param.getName() + "' case types");
        String defaultValueAttribute = (String) typeParamAnnotation.getValue("defaultValue");
        if (defaultValueAttribute != null && !defaultValueAttribute.isEmpty()) {
            Type decodedType = decodeType(defaultValueAttribute, scope, moduleScope, "type parameter '" + param.getName() + "' defaultValue");
            param.setDefaultTypeArgument(decodedType);
            param.setDefaulted(true);
        }
    }
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) LinkedList(java.util.LinkedList) AnnotationMirror(org.eclipse.ceylon.model.loader.mirror.AnnotationMirror) LazyContainer(org.eclipse.ceylon.model.loader.model.LazyContainer) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) FunctionalInterfaceType(org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType) TypeParameterMirror(org.eclipse.ceylon.model.loader.mirror.TypeParameterMirror) List(java.util.List) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Module(org.eclipse.ceylon.model.typechecker.model.Module) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule)

Example 58 with Module

use of org.eclipse.ceylon.model.typechecker.model.Module in project ceylon by eclipse.

the class RuntimeModelLoader method findModuleForClass.

public Module findModuleForClass(Class<?> klass) {
    ClassLoader cl = klass.getClassLoader();
    if (cl instanceof CeylonModuleClassLoader) {
        CeylonModuleClassLoader classLoader = (CeylonModuleClassLoader) cl;
        String name = classLoader.getModuleName();
        String version = classLoader.getModuleVersion();
        String cacheKey = cacheKeyByModule(name, version);
        Module ret = moduleCache.get(cacheKey);
        if (ret == null) {
            // there's a good chance we didn't get the module loaded in time, wait until that classloader is
            // registered then, but give it a nudge:
            // this can complete in another thread or this thread
            classLoader.registerInMetaModel();
            Object lock = getLock();
            synchronized (lock) {
                int tries = MAX_JBOSS_MODULES_WAITS;
                while (!classLoaders.containsValue(cl)) {
                    try {
                        lock.wait(JBOSS_MODULES_TIMEOUT);
                    } catch (InterruptedException e) {
                        throw new ModelResolutionException(e);
                    }
                    if (tries-- < 0)
                        throw new ModelResolutionException("Failed to find registered classloader for " + klass);
                }
                ret = moduleCache.get(cacheKey);
            }
        }
        return ret;
    } else {
        // revert to a single classloader version?
        // FIXME: perhaps we can have other one-classloader-to-one-module setups than jboss modules?
        String pkgName = ReflectionUtils.getPackageName(klass);
        // even on jboss modules, the jdk has no class loader, so jdk classes will have to be resolved
        // to the jdk modules by package
        String jdkModuleName = JDKUtils.getJDKModuleNameForPackage(pkgName);
        if (jdkModuleName != null)
            return findModule(jdkModuleName, JDKUtils.jdk.version);
        if (isDynamicMetamodel())
            return lazyLoadModuleForPackage(pkgName);
        return lookupModuleByPackageName(pkgName);
    }
}
Also used : ModelResolutionException(org.eclipse.ceylon.model.loader.ModelResolutionException) CeylonModuleClassLoader(org.eclipse.ceylon.model.runtime.CeylonModuleClassLoader) CeylonModuleClassLoader(org.eclipse.ceylon.model.runtime.CeylonModuleClassLoader) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule) Module(org.eclipse.ceylon.model.typechecker.model.Module)

Example 59 with Module

use of org.eclipse.ceylon.model.typechecker.model.Module in project ceylon by eclipse.

the class RuntimeModelLoader method lazyLoadModuleForPackage.

private Module lazyLoadModuleForPackage(String pkgName) {
    if (lazyLoadedModulesByPackage.containsKey(pkgName)) {
        return lazyLoadedModulesByPackage.get(pkgName);
    }
    Module module;
    if (pkgName.isEmpty()) {
        module = modules.getDefaultModule();
    } else {
        // pretend it's the default module trying to load a new module? not sure what
        // to do here
        Module loadByModule = modules.getDefaultModule();
        ClassMirror moduleClass = findModuleClass(loadByModule, pkgName);
        if (moduleClass != null) {
            String name = getAnnotationStringValue(moduleClass, CEYLON_MODULE_ANNOTATION, "name");
            String version = getAnnotationStringValue(moduleClass, CEYLON_MODULE_ANNOTATION, "version");
            module = findOrCreateModule(name, version);
            loadCompiledModule(module);
            // damnit, make it import the default module, otherwise it won't be able to see non-ceylon modules
            module.addImport(new ModuleImport(null, modules.getDefaultModule(), false, false));
        // FIXME: do other things like what RuntimeModuleManager does?
        } else {
            // try parent package
            int lastDot = pkgName.lastIndexOf('.');
            String parentPackage;
            if (lastDot == -1)
                parentPackage = "";
            else
                parentPackage = pkgName.substring(0, lastDot);
            module = lazyLoadModuleForPackage(parentPackage);
        }
    }
    // cache module and return
    lazyLoadedModulesByPackage.put(pkgName, module);
    return module;
}
Also used : ModuleImport(org.eclipse.ceylon.model.typechecker.model.ModuleImport) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule) Module(org.eclipse.ceylon.model.typechecker.model.Module) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror)

Example 60 with Module

use of org.eclipse.ceylon.model.typechecker.model.Module in project ceylon by eclipse.

the class RuntimeModelLoader method loadStandardModules.

@Override
public void loadStandardModules() {
    // set up the type factory and that's it: do not try to load the language module package before it's set up
    // by Metamodel.loadModule
    jdkProvider = new JdkProvider();
    Module languageModule = findOrCreateModule(CEYLON_LANGUAGE, null);
    addModuleToClassPath(languageModule, (ArtifactResult) null);
    Package languagePackage = findOrCreatePackage(languageModule, CEYLON_LANGUAGE);
    typeFactory.setPackage(languagePackage);
    // make sure the jdk modules are loaded because those are not initialised by jboss modules nor the IDE Launcher
    for (String jdkModule : JDKUtils.getJDKModuleNames()) findOrCreateModule(jdkModule, JDKUtils.jdk.version);
    for (String jdkOracleModule : JDKUtils.getOracleJDKModuleNames()) findOrCreateModule(jdkOracleModule, JDKUtils.jdk.version);
}
Also used : JdkProvider(org.eclipse.ceylon.model.loader.JdkProvider) Package(org.eclipse.ceylon.model.typechecker.model.Package) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule) Module(org.eclipse.ceylon.model.typechecker.model.Module)

Aggregations

Module (org.eclipse.ceylon.model.typechecker.model.Module)113 LazyModule (org.eclipse.ceylon.model.loader.model.LazyModule)37 Package (org.eclipse.ceylon.model.typechecker.model.Package)26 ModuleImport (org.eclipse.ceylon.model.typechecker.model.ModuleImport)25 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)20 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)19 ArrayList (java.util.ArrayList)18 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)16 File (java.io.File)14 Type (org.eclipse.ceylon.model.typechecker.model.Type)14 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)9 FunctionalInterfaceType (org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType)9 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)9 Test (org.junit.Test)9 Value (org.eclipse.ceylon.model.typechecker.model.Value)8 LinkedList (java.util.LinkedList)7 List (java.util.List)7 Backends (org.eclipse.ceylon.common.Backends)7 ClassMirror (org.eclipse.ceylon.model.loader.mirror.ClassMirror)7