use of org.eclipse.ceylon.javax.tools.JavaFileObject in project ceylon by eclipse.
the class CeylonModelLoader method loadPackage.
@Override
public boolean loadPackage(Module module, String packageName, boolean loadDeclarations) {
synchronized (getLock()) {
// abort if we already loaded it, but only record that we loaded it if we want
// to load the declarations, because merely calling complete() on the package
// is OK
packageName = Util.quoteJavaKeywords(packageName);
String cacheKey = cacheKeyByModule(module, packageName);
if (loadDeclarations) {
if (!loadedPackages.add(cacheKey)) {
return true;
}
} else {
Boolean exists = packageExistence.get(cacheKey);
if (exists != null)
return exists.booleanValue();
}
PackageSymbol ceylonPkg = packageName.equals("") ? syms().unnamedPackage : reader.enterPackage(names.fromString(packageName));
if (loadDeclarations) {
logVerbose("load package " + packageName + " full");
ceylonPkg.complete();
/*
* Eventually this will go away as we get a hook from the typechecker to load on demand, but
* for now the typechecker requires at least ceylon.language to be loaded
*/
for (Symbol m : ceylonPkg.members().getElements()) {
// skip things that are not classes (perhaps package-info?)
if (!(m instanceof ClassSymbol))
continue;
ClassSymbol enclosingClass = getEnclosing((ClassSymbol) m);
if (enclosingClass == m && !Util.isLoadedFromSource(enclosingClass)) {
m.complete();
// avoid anonymous and local classes
if (isAnonymousOrLocal((ClassSymbol) m))
continue;
// avoid member classes
if (((ClassSymbol) m).getNestingKind() != NestingKind.TOP_LEVEL)
continue;
// skip module and package descriptors
if (isModuleOrPackageDescriptorName(m.name.toString()))
continue;
ClassMirror classMirror = lookupClassMirror(module, m.getQualifiedName().toString());
// So ATM we just avoid it, presumably we don't support what it does anyways
if (classMirror != null)
convertToDeclaration(module, classMirror, DeclarationType.VALUE);
}
}
if (module.getNameAsString().equals(JAVA_BASE_MODULE_NAME) && packageName.equals("java.lang"))
loadJavaBaseExtras();
// might be too late
return ceylonPkg.members().getElements().iterator().hasNext();
} else {
logVerbose("load package " + packageName + " light");
try {
// it is cheaper to verify that we have a class file somewhere than to complete the whole package
// just to check for its existence
Iterable<JavaFileObject> list = fileManager.list(PLATFORM_CLASS_PATH, packageName, EnumSet.of(JavaFileObject.Kind.CLASS), false);
if (list.iterator().hasNext()) {
packageExistence.put(cacheKey, Boolean.TRUE);
return true;
}
list = fileManager.list(CLASS_PATH, packageName, EnumSet.of(JavaFileObject.Kind.CLASS), false);
if (list.iterator().hasNext()) {
packageExistence.put(cacheKey, Boolean.TRUE);
return true;
} else {
packageExistence.put(cacheKey, Boolean.FALSE);
return false;
}
} catch (IOException e) {
return false;
}
}
}
}
use of org.eclipse.ceylon.javax.tools.JavaFileObject in project ceylon by eclipse.
the class LanguageCompiler method addResources.
private void addResources() throws Abort {
HashSet<String> written = new HashSet<String>();
try {
for (JavaFileObject fo : resourceFileObjects) {
CeyloncFileManager dfm = (CeyloncFileManager) fileManager;
String jarFileName = JarUtils.toPlatformIndependentPath(dfm.getLocation(CeylonLocation.RESOURCE_PATH), fo.getName());
if (!written.contains(jarFileName)) {
dfm.setModule(modelLoader.findModuleForFile(new File(jarFileName)));
FileObject outFile = dfm.getFileForOutput(StandardLocation.CLASS_OUTPUT, "", jarFileName, null);
OutputStream out = outFile.openOutputStream();
try {
InputStream in = new FileInputStream(new File(fo.getName()));
try {
JarUtils.copy(in, out);
} finally {
in.close();
}
} finally {
out.close();
}
written.add(jarFileName);
}
}
} catch (IOException ex) {
throw new Abort(ex);
}
}
use of org.eclipse.ceylon.javax.tools.JavaFileObject in project ceylon by eclipse.
the class LanguageCompiler method loadModuleFromSource.
private Module loadModuleFromSource(String pkgName, LinkedList<JCCompilationUnit> moduleTrees, List<JCCompilationUnit> parsedTrees) {
if (pkgName.isEmpty())
return null;
String moduleClassName = pkgName + ".module";
JavaFileObject fileObject;
try {
if (options.get(Option.VERBOSE) != null) {
log.printRawLines(WriterKind.NOTICE, "[Trying to load source for module " + moduleClassName + "]");
}
fileObject = fileManager.getJavaFileForInput(StandardLocation.SOURCE_PATH, moduleClassName, Kind.SOURCE);
if (options.get(Option.VERBOSE) != null) {
log.printRawLines(WriterKind.NOTICE, "[Got file object: " + fileObject + "]");
}
} catch (IOException e) {
e.printStackTrace();
return loadModuleFromSource(getParentPackage(pkgName), moduleTrees, parsedTrees);
}
if (fileObject != null) {
// we really want to compile.
for (JCCompilationUnit parsedTree : parsedTrees) {
if (parsedTree.sourcefile.equals(fileObject) && parsedTree instanceof CeylonCompilationUnit) {
// same file! we already parsed it, let's return this one's module
PhasedUnit phasedUnit = ((CeylonCompilationUnit) parsedTree).phasedUnit;
// the module visitor does load the module but does not set the unit's package module
if (phasedUnit.getPackage().getModule() == null) {
// so find the module it created
for (Module mod : ceylonContext.getModules().getListOfModules()) {
// we recognise it with the unit
if (mod.getUnit() == phasedUnit.getUnit()) {
// set the package's module
Package pkg = phasedUnit.getPackage();
pkg.setModule(mod);
mod.getPackages().add(pkg);
modulesLoadedFromSource.add(mod);
break;
}
}
}
// now return it
return phasedUnit.getPackage().getModule();
}
}
JCCompilationUnit javaCompilationUnit = parse(fileObject);
Module module;
if (javaCompilationUnit instanceof CeylonCompilationUnit) {
CeylonCompilationUnit ceylonCompilationUnit = (CeylonCompilationUnit) javaCompilationUnit;
moduleTrees.add(ceylonCompilationUnit);
// parse the module info from there
module = ceylonCompilationUnit.phasedUnit.visitSrcModulePhase();
ceylonCompilationUnit.phasedUnit.visitRemainingModulePhase();
// now set the module
if (module != null) {
ceylonCompilationUnit.phasedUnit.getPackage().setModule(module);
}
} else {
// there was a syntax error in the module descriptor, make a pretend module so that we can
// correctly mark all declarations as part of that module, but we won't generate any code
// for it
ModuleManager moduleManager = phasedUnits.getModuleManager();
module = moduleManager.getOrCreateModule(Arrays.asList(pkgName.split("\\.")), "bogus");
}
// now remember it
if (module != null) {
modulesLoadedFromSource.add(module);
return module;
}
}
return loadModuleFromSource(getParentPackage(pkgName), moduleTrees, parsedTrees);
}
use of org.eclipse.ceylon.javax.tools.JavaFileObject in project ceylon by eclipse.
the class LanguageCompiler method parse.
/**
* Parse contents of file.
* @param filename The name of the file to be parsed.
*/
public JCTree.JCCompilationUnit parse(JavaFileObject filename) {
JavaFileObject prev = log.useSource(filename);
try {
JCTree.JCCompilationUnit t;
if (filename.getName().endsWith(".java")) {
t = parse(filename, readSource(filename));
} else {
t = ceylonParse(filename, readSource(filename));
t.endPositions = new JavacParser.EmptyEndPosTable(null);
}
if (t.endPositions != null)
log.setEndPosTable(filename, t.endPositions);
return t;
} finally {
log.useSource(prev);
}
}
use of org.eclipse.ceylon.javax.tools.JavaFileObject in project ceylon by eclipse.
the class LanguageCompiler method compile.
@Override
public void compile(List<JavaFileObject> fileObjects, List<String> classnames, Iterable<? extends Processor> processors) {
// Now we first split the files into sources/modules and resources
List<JavaFileObject> sourceFiles = List.nil();
List<JavaFileObject> resourceFiles = List.nil();
for (JavaFileObject fo : fileObjects) {
if (isResource(fo)) {
resourceFiles = resourceFiles.append(fo);
} else {
sourceFiles = sourceFiles.append(fo);
}
}
this.resourceFileObjects = resourceFiles;
// Add any module files for the resources (if needed)
sourceFiles = addModuleDescriptors(sourceFiles, resourceFiles);
// And then continue to the compilation of the source files
super.compile(sourceFiles, classnames, processors);
}
Aggregations