Search in sources :

Example 6 with LazyModule

use of org.eclipse.ceylon.model.loader.model.LazyModule in project ceylon by eclipse.

the class RuntimeModuleManager method loadModule.

public boolean loadModule(String name, String version, ArtifactResult artifact, ClassLoader classLoader, boolean staticMetamodel) {
    RuntimeModelLoader modelLoader = getModelLoader();
    synchronized (modelLoader.getLock()) {
        manualMetamodelSetup = true;
        Module module = getOrCreateModule(splitModuleName(name), version);
        // ones setting the module's Unit
        if (module.isDefaultModule() ? module.getUnit() != null : module.isAvailable())
            return false;
        modelLoader.addModuleToClassPath(module, artifact);
        modelLoader.addModuleClassLoader(module, classLoader);
        module.setAvailable(true);
        Unit u = new Unit();
        u.setFilename(artifact.name());
        if (artifact.artifact() != null) {
            u.setFullPath(artifact.artifact().getAbsolutePath());
        }
        module.setUnit(u);
        if (module.isLanguageModule())
            modelLoader.loadPackageDescriptors();
        if (!module.isDefaultModule()) {
            // FIXME: dependencies of Ceylon modules?
            if (!modelLoader.loadCompiledModule(module, !staticMetamodel)) {
                // we didn't find module.class so it must be a java module if it's not the default module
                ((LazyModule) module).setJava(true);
                module.setNativeBackends(Backend.Java.asSet());
                // Java modules must have their dependencies set by the artifact result, as there is no module info in the jar
                loadModuleImportsFromArtifact(module, artifact);
            } else if (staticMetamodel) {
                // for a static metamodel we get the dependencies from the artifact too
                loadModuleImportsFromArtifact(module, artifact);
            }
        }
        return true;
    }
}
Also used : RuntimeModelLoader(org.eclipse.ceylon.compiler.java.runtime.model.RuntimeModelLoader) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule) ReflectionModule(org.eclipse.ceylon.model.loader.impl.reflect.model.ReflectionModule) Module(org.eclipse.ceylon.model.typechecker.model.Module) Unit(org.eclipse.ceylon.model.typechecker.model.Unit) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule)

Example 7 with LazyModule

use of org.eclipse.ceylon.model.loader.model.LazyModule in project ceylon by eclipse.

the class CeylonEnter method addModuleToClassPath.

public void addModuleToClassPath(Module module, boolean errorIfMissing, ArtifactResult result) {
    if (verbose)
        log.printRawLines(WriterKind.NOTICE, "[Adding module to classpath: " + module.getNameAsString() + "/" + module.getVersion() + "]");
    Collection<File> classPath = fileManager.getLocations().getLocation(StandardLocation.CLASS_PATH);
    File artifact = null;
    try {
        artifact = result != null ? result.artifact() : null;
    } catch (Exception e) {
        String moduleName = module.getNameAsString();
        if (!module.isDefaultModule())
            moduleName += "/" + module.getVersion();
        log.error("ceylon", "Exception occured while trying to resolve module " + moduleName);
        e.printStackTrace();
    }
    if (verbose) {
        if (artifact != null)
            log.printRawLines(WriterKind.NOTICE, "[Found module at : " + artifact.getPath() + "]");
        else
            log.printRawLines(WriterKind.NOTICE, "[Could not find module]");
    }
    if (modulesAddedToClassPath.add(module)) {
        if (artifact != null && artifact.exists()) {
            ArrayList<File> newClassPath = new ArrayList<File>(classPath);
            newClassPath.add(artifact);
            try {
                fileManager.getLocations().setLocation(StandardLocation.CLASS_PATH, newClassPath);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            ((LazyModule) module).loadPackageList(result);
        } else if (errorIfMissing) {
            log.error("ceylon", "Failed to find module " + module.getNameAsString() + "/" + module.getVersion() + " in repositories");
        }
    } else if (verbose) {
        log.printRawLines(WriterKind.NOTICE, "[Module already added to classpath]");
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule) InvalidArchiveException(org.eclipse.ceylon.cmr.impl.InvalidArchiveException) IOException(java.io.IOException)

Example 8 with LazyModule

use of org.eclipse.ceylon.model.loader.model.LazyModule in project ceylon by eclipse.

the class LazyModuleSourceMapper method setupJavaModule.

private void setupJavaModule(ModuleImport moduleImport, LazyModule module, AbstractModelLoader modelLoader, ModuleManager moduleManager, ArtifactResult artifact) {
    // we didn't find module.class so it must be a java module if it's not the default module
    module.setJava(true);
    module.setNativeBackends(Backend.Java.asSet());
    modelLoader.loadJava9Module(module, artifact.artifact());
    List<ArtifactResult> deps = artifact.dependencies();
    boolean forceExport = ModuleUtil.isMavenModule(module.getNameAsString()) && modelLoader.isFullyExportMavenDependencies();
    for (ArtifactResult dep : deps) {
        // forget runtime, test and even provided
        if (dep.moduleScope() != ModuleScope.COMPILE)
            continue;
        // re-export them anyway
        if (dep.optional())
            continue;
        Module dependency = moduleManager.getOrCreateModule(ModuleManager.splitModuleName(dep.name()), dep.version());
        ModuleImport depImport = moduleManager.findImport(module, dependency);
        if (depImport == null) {
            moduleImport = new ModuleImport(dep.namespace(), dependency, dep.optional(), // allow forcing export but not for optional modules
            dep.exported() || forceExport && !dep.optional(), Backend.Java);
            module.addImport(moduleImport);
        }
    }
}
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) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult)

Example 9 with LazyModule

use of org.eclipse.ceylon.model.loader.model.LazyModule in project ceylon by eclipse.

the class AbstractModelLoader method loadCompiledModule.

private boolean loadCompiledModule(Module module, ClassMirror moduleClass, boolean loadModuleImports) {
    String moduleClassName = moduleClass.getQualifiedName();
    String name = getAnnotationStringValue(moduleClass, CEYLON_MODULE_ANNOTATION, "name");
    String version = getAnnotationStringValue(moduleClass, CEYLON_MODULE_ANNOTATION, "version");
    if (name == null || name.isEmpty()) {
        logWarning("Module class " + moduleClassName + " contains no name, ignoring it");
        return false;
    }
    if (!name.equals(module.getNameAsString())) {
        logWarning("Module class " + moduleClassName + " declares an invalid name: " + name + ". It should be: " + module.getNameAsString());
        return false;
    }
    if (version == null || version.isEmpty()) {
        logWarning("Module class " + moduleClassName + " contains no version, ignoring it");
        return false;
    }
    if (!version.equals(module.getVersion())) {
        logWarning("Module class " + moduleClassName + " declares an invalid version: " + version + ". It should be: " + module.getVersion());
        return false;
    }
    // String label = getAnnotationStringValue(moduleClass, CEYLON_MODULE_ANNOTATION, "label");
    // module.setLabel(label);
    int major = getAnnotationIntegerValue(moduleClass, CEYLON_CEYLON_ANNOTATION, "major", 0);
    int minor = getAnnotationIntegerValue(moduleClass, CEYLON_CEYLON_ANNOTATION, "minor", 0);
    module.setJvmMajor(major);
    module.setJvmMinor(minor);
    // no need to load the "nativeBackends" annotation value, it's loaded from annotations
    setAnnotations(module, moduleClass, false);
    if (loadModuleImports) {
        List<AnnotationMirror> imports = getAnnotationArrayValue(moduleClass, CEYLON_MODULE_ANNOTATION, "dependencies");
        if (imports != null) {
            boolean supportsNamespaces = ModuleUtil.supportsImportsWithNamespaces(major, minor);
            for (AnnotationMirror importAttribute : imports) {
                String dependencyName = (String) importAttribute.getValue("name");
                if (dependencyName != null) {
                    String namespace;
                    if (supportsNamespaces) {
                        namespace = (String) importAttribute.getValue("namespace");
                        if (namespace != null && namespace.isEmpty()) {
                            namespace = null;
                        }
                    } else {
                        if (ModuleUtil.isMavenModule(dependencyName)) {
                            namespace = "maven";
                        } else {
                            namespace = null;
                        }
                    }
                    String dependencyVersion = (String) importAttribute.getValue("version");
                    Module dependency = moduleManager.getOrCreateModule(ModuleManager.splitModuleName(dependencyName), dependencyVersion);
                    Boolean optionalVal = (Boolean) importAttribute.getValue("optional");
                    Boolean exportVal = (Boolean) importAttribute.getValue("export");
                    List<String> nativeBackends = (List<String>) importAttribute.getValue("nativeBackends");
                    Backends backends = nativeBackends == null ? Backends.ANY : Backends.fromAnnotations(nativeBackends);
                    ModuleImport moduleImport = moduleManager.findImport(module, dependency);
                    if (moduleImport == null) {
                        boolean optional = optionalVal != null && optionalVal;
                        boolean export = exportVal != null && exportVal;
                        moduleImport = new ModuleImport(namespace, dependency, optional, export, backends);
                        module.addImport(moduleImport);
                    }
                }
            }
        }
    }
    module.setAvailable(true);
    modules.getListOfModules().add(module);
    Module languageModule = modules.getLanguageModule();
    module.setLanguageModule(languageModule);
    if (loadModuleImports) {
        if (!ModelUtil.equalModules(module, languageModule)) {
            boolean found = false;
            for (ModuleImport mi : module.getImports()) {
                if (mi.getModule().isLanguageModule()) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                // It's not really a LazyModule because we're not loading
                // it lazily. It's only here for module version analysis.
                // But other stuff expects non-source modules to be lazy.
                LazyModule oldLangMod = new LazyModule() {

                    @Override
                    protected AbstractModelLoader getModelLoader() {
                        return AbstractModelLoader.this;
                    }
                };
                oldLangMod.setLanguageModule(oldLangMod);
                oldLangMod.setName(Arrays.asList("ceylon", "language"));
                oldLangMod.setVersion(getJvmLanguageModuleVersion(major, minor));
                oldLangMod.setNativeBackends(Backends.JAVA);
                oldLangMod.setJvmMajor(major);
                oldLangMod.setJvmMinor(minor);
                ModuleImport moduleImport = new ModuleImport(null, oldLangMod, false, false);
                module.addImport(moduleImport);
            }
        }
    }
    return true;
}
Also used : AnnotationMirror(org.eclipse.ceylon.model.loader.mirror.AnnotationMirror) Backends(org.eclipse.ceylon.common.Backends) ModuleImport(org.eclipse.ceylon.model.typechecker.model.ModuleImport) 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) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule)

Example 10 with LazyModule

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

Aggregations

LazyModule (org.eclipse.ceylon.model.loader.model.LazyModule)10 Module (org.eclipse.ceylon.model.typechecker.model.Module)7 ReflectionModule (org.eclipse.ceylon.model.loader.impl.reflect.model.ReflectionModule)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 ModuleImport (org.eclipse.ceylon.model.typechecker.model.ModuleImport)2 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 InvalidArchiveException (org.eclipse.ceylon.cmr.impl.InvalidArchiveException)1 Backends (org.eclipse.ceylon.common.Backends)1 RuntimeModelLoader (org.eclipse.ceylon.compiler.java.runtime.model.RuntimeModelLoader)1 ArtifactResult (org.eclipse.ceylon.model.cmr.ArtifactResult)1 AbstractModelLoader (org.eclipse.ceylon.model.loader.AbstractModelLoader)1 AnnotationMirror (org.eclipse.ceylon.model.loader.mirror.AnnotationMirror)1 ClassMirror (org.eclipse.ceylon.model.loader.mirror.ClassMirror)1 LazyModuleManager (org.eclipse.ceylon.model.loader.model.LazyModuleManager)1 ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)1 Unit (org.eclipse.ceylon.model.typechecker.model.Unit)1