use of org.eclipse.ceylon.model.loader.AbstractModelLoader in project ceylon by eclipse.
the class ModelLoaderTests method verifyCompilerClassLoading.
protected void verifyCompilerClassLoading(String ceylon, final ModelComparison modelCompare) {
// now compile the ceylon decl file
CeyloncTaskImpl task = getCompilerTask(ceylon);
// get the context to grab the phased units
Context context = task.getContext();
if (simpleAnnotationModels) {
CeylonEnter.instance(context);
ExpressionTransformer.getInstance(context).simpleAnnotationModels = true;
CeylonTransformer.getInstance(context).simpleAnnotationModels = true;
StatementTransformer.getInstance(context).simpleAnnotationModels = true;
ClassTransformer.getInstance(context).simpleAnnotationModels = true;
}
Boolean success = task.call();
Assert.assertTrue(success);
PhasedUnits phasedUnits = LanguageCompiler.getPhasedUnitsInstance(context);
// find out what was in that file
Assert.assertEquals(2, phasedUnits.getPhasedUnits().size());
PhasedUnit one = phasedUnits.getPhasedUnits().get(0);
PhasedUnit two = phasedUnits.getPhasedUnits().get(1);
PhasedUnit phasedUnit = one.getUnitFile().getName().endsWith("module.ceylon") ? two : one;
final Map<String, Declaration> decls = new HashMap<String, Declaration>();
for (Declaration decl : phasedUnit.getUnit().getDeclarations()) {
if (decl.isToplevel()) {
decls.put(getQualifiedPrefixedName(decl), decl);
}
}
// now compile the ceylon usage file
// remove the extension, make lowercase and add "test"
String testfile = ceylon.substring(0, ceylon.length() - 7).toLowerCase() + "test.ceylon";
JavacTaskImpl task2 = getCompilerTask(testfile);
// get the context to grab the declarations
final Context context2 = task2.getContext();
// declarations from the jar anymore because we've overridden the jar and the javac jar index is corrupted
class Listener implements TaskListener {
@Override
public void started(TaskEvent e) {
}
@Override
public void finished(TaskEvent e) {
if (e.getKind() == Kind.ENTER) {
AbstractModelLoader modelLoader = CeylonModelLoader.instance(context2);
Modules modules = LanguageCompiler.getCeylonContextInstance(context2).getModules();
// now see if we can find our declarations
compareDeclarations(modelCompare, decls, modelLoader, modules);
}
}
}
Listener listener = new Listener();
task2.setTaskListener(listener);
success = task2.call();
Assert.assertTrue("Compilation failed", success);
// now check with the runtime model loader too
String module = moduleForJavaModelLoading();
String version = "1";
ModuleWithArtifact moduleWithArtifact = new ModuleWithArtifact(module, version);
synchronized (RUN_LOCK) {
// this initialises the metamodel, even if we don't use the resulting ClassLoader
URLClassLoader classLoader;
try {
classLoader = getClassLoader("runtime model loader tests", moduleWithArtifact);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
try {
RuntimeModuleManager moduleManager = Metamodel.getModuleManager();
RuntimeModelLoader modelLoader = moduleManager.getModelLoader();
Modules modules = moduleManager.getModules();
// now see if we can find our declarations
compareDeclarations(modelCompare, decls, modelLoader, modules);
} finally {
try {
classLoader.close();
} catch (IOException e) {
// ignore
e.printStackTrace();
}
}
}
}
use of org.eclipse.ceylon.model.loader.AbstractModelLoader in project ceylon by eclipse.
the class LazyModuleSourceMapper method preResolveDependenciesIfRequired.
@Override
public void preResolveDependenciesIfRequired(RepositoryManager repositoryManager) {
AbstractModelLoader modelLoader = getModuleManager().getModelLoader();
if (!modelLoader.isFullyExportMavenDependencies())
return;
if (statusPrinter != null) {
statusPrinter.clearLine();
statusPrinter.log("Pre-resolving dependencies");
}
if (verbose) {
log.printRawLines(WriterKind.NOTICE, "[Pre-resolving dependencies]");
}
Set<Module> compiledModules = getCompiledModules();
Map<String, String> modules = new HashMap<>();
ModuleImport anyImport = null;
for (Module module : compiledModules) {
for (ModuleImport imp : module.getImports()) {
if (imp.getModule() == null || !compiledModules.contains(imp.getModule())) {
if (anyImport == null)
anyImport = imp;
String name = imp.getModule().getNameAsString();
if (imp.getNamespace() != null)
name = imp.getNamespace() + ":" + name;
modules.put(name, imp.getModule().getVersion());
}
}
}
if (statusPrinter != null) {
statusPrinter.clearLine();
statusPrinter.log("Pre-resolving found " + modules.size() + " to pre-resolve");
}
if (verbose) {
log.printRawLines(WriterKind.NOTICE, "[Pre-resolving " + modules.size() + " modules]");
}
if (modules.isEmpty())
return;
Entry<String, String> first = modules.entrySet().iterator().next();
CompilerModuleLoader ml = new CompilerModuleLoader(repositoryManager, null, modules, verbose, statusPrinter, log);
boolean giveup = false;
try {
ml.loadModule(first.getKey(), first.getValue(), ModuleScope.COMPILE);
} catch (ModuleNotFoundException e) {
attachErrorToDependencyDeclaration(anyImport, "Pre-resolving of module failed: " + e.getMessage(), true);
giveup = true;
}
if (statusPrinter != null) {
statusPrinter.clearLine();
// don't try to read the module count if pre-resolving failed
if (giveup)
statusPrinter.log("Pre-resolving failed");
else
statusPrinter.log("Pre-resolving resolved " + ml.getModuleCount());
}
if (verbose) {
// don't try to read the module count if pre-resolving failed
if (giveup)
log.printRawLines(WriterKind.NOTICE, "[Pre-resolved failed]");
else
log.printRawLines(WriterKind.NOTICE, "[Pre-resolved " + ml.getModuleCount() + " modules]");
}
if (giveup)
return;
Overrides overrides = repositoryManager.getOverrides();
if (overrides == null) {
overrides = Overrides.create();
repositoryManager.setOverrides(overrides);
}
ml.setupOverrides(overrides);
ml.cleanup();
}
use of org.eclipse.ceylon.model.loader.AbstractModelLoader in project ceylon by eclipse.
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();
AbstractModelLoader modelLoader = moduleManager.getModelLoader();
if (modelLoader.getJdkProviderModule() == module) {
modelLoader.setupAlternateJdk(module, artifact);
}
// module in question will be in the classpath
if (moduleLoadedFromSource || forCompiledModule) {
if (shouldAbortOnDuplicateModule(module, moduleName, modelLoader, dependencyTree))
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
modelLoader.addModuleToClassPath(module, artifact);
LazyModule lazyModule = (LazyModule) module;
if (!module.isDefaultModule()) {
if (artifact.groupId() != null)
module.setGroupId(artifact.groupId());
if (artifact.artifactId() != null)
module.setArtifactId(artifact.artifactId());
if (artifact.classifier() != null)
module.setClassifier(artifact.classifier());
if (!modelLoader.loadCompiledModule(module)) {
setupJavaModule(moduleImport, lazyModule, modelLoader, moduleManager, artifact);
} else {
setupCeylonModule(moduleImport, lazyModule, modelLoader, moduleManager, artifact, dependencyTree);
}
}
// module is now available
module.setAvailable(true);
}
}
use of org.eclipse.ceylon.model.loader.AbstractModelLoader in project ceylon by eclipse.
the class LazyModule method getPackage.
@Override
public Package getPackage(String name) {
// try here first
Package pkg = null;
// unless we're the default module, in which case we have to check this at the end,
// since every package can be part of the default module
boolean defaultModule = isDefaultModule();
if (!defaultModule) {
pkg = findPackageInModule(this, name);
if (pkg != null)
return loadPackage(pkg);
}
// then try in dependencies
Set<Module> visited = new HashSet<Module>();
for (ModuleImport dependency : getImports()) {
// we don't have to worry about the default module here since we can't depend on it
pkg = findPackageInImport(name, dependency, visited);
if (pkg != null)
return loadPackage(pkg);
}
AbstractModelLoader modelLoader = getModelLoader();
JdkProvider jdkProvider = modelLoader.getJdkProvider();
// so we pretend the JDK imports the language module
if (jdkProvider != null && jdkProvider.isJDKModule(getNameAsString())) {
Module languageModule = getModelLoader().getLanguageModule();
if (languageModule instanceof LazyModule) {
pkg = findPackageInModule((LazyModule) languageModule, name);
if (pkg != null)
return loadPackage(pkg);
}
}
// work and appear to come from there
if (jdkProvider.isJDKPackage(name)) {
return null;
}
// do the lookup of the default module last
if (defaultModule)
pkg = modelLoader.findExistingPackage(this, name);
return pkg;
}
use of org.eclipse.ceylon.model.loader.AbstractModelLoader in project ceylon by eclipse.
the class CeylonModelLoader method instance.
public static AbstractModelLoader instance(Context context) {
AbstractModelLoader instance = context.get(AbstractModelLoader.class);
if (instance == null) {
ModelLoaderFactory factory = context.get(ModelLoaderFactory.class);
if (factory != null) {
instance = factory.createModelLoader(context);
} else {
instance = new CeylonModelLoader(context);
}
context.put(AbstractModelLoader.class, instance);
}
return instance;
}
Aggregations