Search in sources :

Example 26 with JavaFileObject

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;
            }
        }
    }
}
Also used : PackageSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.PackageSymbol) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) MethodSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.MethodSymbol) TypeSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.TypeSymbol) ClassSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.ClassSymbol) Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol) PackageSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.PackageSymbol) ClassSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.ClassSymbol) IOException(java.io.IOException) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror)

Example 27 with JavaFileObject

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);
    }
}
Also used : JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) Abort(org.eclipse.ceylon.langtools.tools.javac.util.Abort) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) FileObject(org.eclipse.ceylon.javax.tools.FileObject) CeylonFileObject(org.eclipse.ceylon.compiler.java.codegen.CeylonFileObject) IOException(java.io.IOException) VirtualFile(org.eclipse.ceylon.compiler.typechecker.io.VirtualFile) File(java.io.File) FileInputStream(java.io.FileInputStream) HashSet(java.util.HashSet)

Example 28 with JavaFileObject

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);
}
Also used : JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) CeylonCompilationUnit(org.eclipse.ceylon.compiler.java.codegen.CeylonCompilationUnit) IOException(java.io.IOException) Package(org.eclipse.ceylon.model.typechecker.model.Package) Module(org.eclipse.ceylon.model.typechecker.model.Module) ModuleManager(org.eclipse.ceylon.model.typechecker.util.ModuleManager) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)

Example 29 with JavaFileObject

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);
    }
}
Also used : JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) JavacParser(org.eclipse.ceylon.langtools.tools.javac.parser.JavacParser) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

Example 30 with JavaFileObject

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);
}
Also used : JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject)

Aggregations

JavaFileObject (org.eclipse.ceylon.javax.tools.JavaFileObject)53 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)9 File (java.io.File)8 IOException (java.io.IOException)8 HashSet (java.util.HashSet)8 CeylonFileObject (org.eclipse.ceylon.compiler.java.codegen.CeylonFileObject)5 TaskEvent (org.eclipse.ceylon.langtools.source.util.TaskEvent)5 Symbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol)5 Type (org.eclipse.ceylon.langtools.tools.javac.code.Type)5 InputStream (java.io.InputStream)4 ListBuffer (org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer)4 LinkedHashSet (java.util.LinkedHashSet)3 VirtualFile (org.eclipse.ceylon.compiler.typechecker.io.VirtualFile)3 JavacFileManager (org.eclipse.ceylon.langtools.tools.javac.file.JavacFileManager)3 JCCompilationUnit (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit)3 FileInputStream (java.io.FileInputStream)2 OutputStream (java.io.OutputStream)2 PrintWriter (java.io.PrintWriter)2 ByteBuffer (java.nio.ByteBuffer)2 CharBuffer (java.nio.CharBuffer)2