Search in sources :

Example 1 with LazyModule

use of com.redhat.ceylon.model.loader.model.LazyModule in project ceylon-compiler by ceylon.

the class CeylonEnter method addModuleToClassPath.

public void addModuleToClassPath(Module module, boolean errorIfMissing, ArtifactResult result) {
    if (verbose)
        Log.printLines(log.noticeWriter, "[Adding module to classpath: " + module.getNameAsString() + "/" + module.getVersion() + "]");
    Paths.Path classPath = paths.getPathForLocation(StandardLocation.CLASS_PATH);
    File artifact = null;
    try {
        artifact = result != null ? result.artifact() : null;
    } catch (Exception e) {
        String moduleName = module.getNameAsString();
        if (!module.isDefault())
            moduleName += "/" + module.getVersion();
        log.error("ceylon", "Exception occured while trying to resolve module " + moduleName);
        e.printStackTrace();
    }
    if (verbose) {
        if (artifact != null)
            Log.printLines(log.noticeWriter, "[Found module at : " + artifact.getPath() + "]");
        else
            Log.printLines(log.noticeWriter, "[Could not find module]");
    }
    if (modulesAddedToClassPath.add(module)) {
        if (artifact != null && artifact.exists()) {
            classPath.add(artifact);
            ((LazyModule) module).loadPackageList(result);
        } else if (errorIfMissing) {
            log.error("ceylon", "Failed to find module " + module.getNameAsString() + "/" + module.getVersion() + " in repositories");
        }
    } else if (verbose) {
        Log.printLines(log.noticeWriter, "[Module already added to classpath]");
    }
}
Also used : Paths(com.sun.tools.javac.file.Paths) File(java.io.File) LazyModule(com.redhat.ceylon.model.loader.model.LazyModule) InvalidArchiveException(com.redhat.ceylon.cmr.impl.InvalidArchiveException)

Example 2 with LazyModule

use of com.redhat.ceylon.model.loader.model.LazyModule in project ceylon-compiler by ceylon.

the class CeylonDocModuleManager method createModule.

@Override
protected Module createModule(List<String> moduleName, String version) {
    String name = Util.getName(moduleName);
    // never create a reflection module for ceylon.language when we're documenting it
    Module module;
    if (name.equals(AbstractModelLoader.CEYLON_LANGUAGE) && isModuleLoadedFromSource(AbstractModelLoader.CEYLON_LANGUAGE))
        module = new Module();
    else
        module = new ReflectionModule(this);
    module.setName(moduleName);
    module.setVersion(version);
    if (module instanceof ReflectionModule)
        setupIfJDKModule((LazyModule) module);
    return module;
}
Also used : ReflectionModule(com.redhat.ceylon.model.loader.impl.reflect.model.ReflectionModule) ReflectionModule(com.redhat.ceylon.model.loader.impl.reflect.model.ReflectionModule) LazyModule(com.redhat.ceylon.model.loader.model.LazyModule) Module(com.redhat.ceylon.model.typechecker.model.Module) LazyModule(com.redhat.ceylon.model.loader.model.LazyModule)

Example 3 with LazyModule

use of com.redhat.ceylon.model.loader.model.LazyModule in project ceylon-compiler by ceylon.

the class CeylonDocModelLoader method addModuleToClassPath.

@Override
public void addModuleToClassPath(final Module module, ArtifactResult artifact) {
    // don't add the same module more than once
    if (artifact == null || !modulesAddedToClassPath.add(module))
        return;
    File file = artifact.artifact();
    // do not load classes from it if it's the language module, since it's already in our ClassLoader and
    // that would create multiple instances of the same class 
    classLoader.addJar(artifact, module, module == modules.getLanguageModule());
    log.debug("Adding jar to classpath: " + file);
    if (module instanceof LazyModule) {
        ((LazyModule) module).loadPackageList(artifact);
    }
}
Also used : File(java.io.File) LazyModule(com.redhat.ceylon.model.loader.model.LazyModule)

Example 4 with LazyModule

use of com.redhat.ceylon.model.loader.model.LazyModule in project ceylon-compiler by ceylon.

the class LazyModuleSourceMapper method resolveModule.

@Override
public void resolveModule(ArtifactResult artifact, Module module, ModuleImport moduleImport, LinkedList<Module> dependencyTree, List<PhasedUnits> phasedUnitsOfDependencies, boolean forCompiledModule) {
    String moduleName = module.getNameAsString();
    LazyModuleManager moduleManager = getModuleManager();
    boolean moduleLoadedFromSource = moduleManager.isModuleLoadedFromSource(moduleName);
    boolean isLanguageModule = module == module.getLanguageModule();
    // module in question will be in the classpath
    if (moduleLoadedFromSource || forCompiledModule) {
        String standardisedModuleName = ModuleUtil.toCeylonModuleName(moduleName);
        // check for an already loaded module with the same name but different version
        for (Module loadedModule : getContext().getModules().getListOfModules()) {
            String loadedModuleName = loadedModule.getNameAsString();
            String standardisedLoadedModuleName = ModuleUtil.toCeylonModuleName(loadedModuleName);
            boolean sameModule = loadedModuleName.equals(moduleName);
            boolean similarModule = standardisedLoadedModuleName.equals(standardisedModuleName);
            if ((sameModule || similarModule) && !loadedModule.getVersion().equals(module.getVersion()) && moduleManager.getModelLoader().isModuleInClassPath(loadedModule)) {
                if (sameModule) {
                    String[] versions = VersionComparator.orderVersions(module.getVersion(), loadedModule.getVersion());
                    String error = "source code imports two different versions of module '" + moduleName + "': " + "version '" + versions[0] + "' and version '" + versions[1] + "'";
                    addErrorToModule(dependencyTree.getFirst(), error);
                } else {
                    String moduleA;
                    String moduleB;
                    if (loadedModuleName.compareTo(moduleName) < 0) {
                        moduleA = ModuleUtil.makeModuleName(loadedModuleName, loadedModule.getVersion());
                        moduleB = ModuleUtil.makeModuleName(moduleName, module.getVersion());
                    } else {
                        moduleA = ModuleUtil.makeModuleName(moduleName, module.getVersion());
                        moduleB = ModuleUtil.makeModuleName(loadedModuleName, loadedModule.getVersion());
                    }
                    String error = "source code imports two different versions of similar modules '" + moduleA + "' and '" + moduleB + "'";
                    addWarningToModule(dependencyTree.getFirst(), Warning.similarModule, error);
                }
                return;
            }
        }
    }
    if (moduleLoadedFromSource) {
        super.resolveModule(artifact, module, moduleImport, dependencyTree, phasedUnitsOfDependencies, forCompiledModule);
    } else if (forCompiledModule || isLanguageModule || moduleManager.shouldLoadTransitiveDependencies()) {
        // we only add stuff to the classpath and load the modules if we need them to compile our modules
        // To be able to load it from the corresponding archive
        moduleManager.getModelLoader().addModuleToClassPath(module, artifact);
        if (!module.isDefault() && !moduleManager.getModelLoader().loadCompiledModule(module)) {
            // 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());
            List<ArtifactResult> deps = artifact.dependencies();
            for (ArtifactResult dep : deps) {
                Module dependency = moduleManager.getOrCreateModule(ModuleManager.splitModuleName(dep.name()), dep.version());
                ModuleImport depImport = moduleManager.findImport(module, dependency);
                if (depImport == null) {
                    moduleImport = new ModuleImport(dependency, dep.importType() == ImportType.OPTIONAL, dep.importType() == ImportType.EXPORT, Backend.Java);
                    module.addImport(moduleImport);
                }
            }
        }
        LazyModule lazyModule = (LazyModule) module;
        if (!lazyModule.isJava() && !module.isDefault()) {
            // it must be a Ceylon module
            // default modules don't have any module descriptors so we can't check them
            Overrides overrides = getContext().getRepositoryManager().getOverrides();
            if (overrides != null) {
                if (overrides.getArtifactOverrides(new ArtifactContext(artifact.name(), artifact.version())) != null) {
                    Set<ModuleDependencyInfo> existingModuleDependencies = new HashSet<>();
                    for (ModuleImport i : lazyModule.getImports()) {
                        Module m = i.getModule();
                        if (m != null) {
                            existingModuleDependencies.add(new ModuleDependencyInfo(m.getNameAsString(), m.getVersion(), i.isOptional(), i.isExport()));
                        }
                    }
                    ModuleInfo sourceModuleInfo = new ModuleInfo(null, existingModuleDependencies);
                    ModuleInfo newModuleInfo = overrides.applyOverrides(artifact.name(), artifact.version(), sourceModuleInfo);
                    List<ModuleImport> newModuleImports = new ArrayList<>();
                    for (ModuleDependencyInfo dep : newModuleInfo.getDependencies()) {
                        Module dependency = moduleManager.getOrCreateModule(ModuleManager.splitModuleName(dep.getName()), dep.getVersion());
                        Backends backends = dependency.getNativeBackends();
                        moduleImport = new ModuleImport(dependency, dep.isOptional(), dep.isExport(), backends);
                        newModuleImports.add(moduleImport);
                    }
                    module.overrideImports(newModuleImports);
                }
            }
            if (!Versions.isJvmBinaryVersionSupported(lazyModule.getMajor(), lazyModule.getMinor())) {
                attachErrorToDependencyDeclaration(moduleImport, dependencyTree, "version '" + lazyModule.getVersion() + "' of module '" + module.getNameAsString() + "' was compiled by an incompatible version of the compiler (binary version " + lazyModule.getMajor() + "." + lazyModule.getMinor() + " of module is not compatible with binary version " + Versions.JVM_BINARY_MAJOR_VERSION + "." + Versions.JVM_BINARY_MINOR_VERSION + " of this compiler)");
            }
        }
        // module is now available
        module.setAvailable(true);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) LazyModuleManager(com.redhat.ceylon.model.loader.model.LazyModuleManager) ArtifactContext(com.redhat.ceylon.cmr.api.ArtifactContext) LazyModule(com.redhat.ceylon.model.loader.model.LazyModule) ArtifactResult(com.redhat.ceylon.model.cmr.ArtifactResult) Backends(com.redhat.ceylon.common.Backends) ModuleInfo(com.redhat.ceylon.cmr.api.ModuleInfo) ModuleImport(com.redhat.ceylon.model.typechecker.model.ModuleImport) Overrides(com.redhat.ceylon.cmr.api.Overrides) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ModuleDependencyInfo(com.redhat.ceylon.cmr.api.ModuleDependencyInfo) LazyModule(com.redhat.ceylon.model.loader.model.LazyModule) Module(com.redhat.ceylon.model.typechecker.model.Module)

Aggregations

LazyModule (com.redhat.ceylon.model.loader.model.LazyModule)4 Module (com.redhat.ceylon.model.typechecker.model.Module)2 File (java.io.File)2 ArtifactContext (com.redhat.ceylon.cmr.api.ArtifactContext)1 ModuleDependencyInfo (com.redhat.ceylon.cmr.api.ModuleDependencyInfo)1 ModuleInfo (com.redhat.ceylon.cmr.api.ModuleInfo)1 Overrides (com.redhat.ceylon.cmr.api.Overrides)1 InvalidArchiveException (com.redhat.ceylon.cmr.impl.InvalidArchiveException)1 Backends (com.redhat.ceylon.common.Backends)1 ArtifactResult (com.redhat.ceylon.model.cmr.ArtifactResult)1 ReflectionModule (com.redhat.ceylon.model.loader.impl.reflect.model.ReflectionModule)1 LazyModuleManager (com.redhat.ceylon.model.loader.model.LazyModuleManager)1 ModuleImport (com.redhat.ceylon.model.typechecker.model.ModuleImport)1 Paths (com.sun.tools.javac.file.Paths)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Set (java.util.Set)1