Search in sources :

Example 31 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.

the class Test method createFileManager.

JavacFileManager createFileManager(boolean useOptimizedZip, boolean useSymbolFile) {
    Context c = new Context();
    Options options = Options.instance(c);
    options.put("useOptimizedZip", Boolean.toString(useOptimizedZip));
    if (!useSymbolFile) {
        options.put("ignore.symbol.file", "true");
    }
    return new JavacFileManager(c, false, null);
}
Also used : Context(com.sun.tools.javac.util.Context) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) Options(com.sun.tools.javac.util.Options)

Example 32 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.

the class LanguageCompiler method getPackage.

private String getPackage(JavaFileObject file) throws IOException {
    Iterable<? extends File> prefixes = ((JavacFileManager) fileManager).getLocation(StandardLocation.SOURCE_PATH);
    // Figure out the package name by stripping the "-src" prefix and
    // extracting
    // the package part of the fullname.
    String filePath = file.toUri().getPath();
    // go absolute
    filePath = new File(filePath).getCanonicalPath();
    int srcDirLength = 0;
    for (File prefixFile : prefixes) {
        String prefix = prefixFile.getCanonicalPath();
        if (filePath.startsWith(prefix) && prefix.length() > srcDirLength) {
            srcDirLength = prefix.length();
        }
    }
    if (srcDirLength > 0) {
        String fullname = filePath.substring(srcDirLength);
        assert fullname.endsWith(".ceylon");
        fullname = fullname.substring(0, fullname.length() - ".ceylon".length());
        fullname = fullname.replace(File.separator, ".");
        if (fullname.startsWith("."))
            fullname = fullname.substring(1);
        String packageName = Convert.packagePart(fullname);
        if (!packageName.equals(""))
            return packageName;
    }
    return null;
}
Also used : JavacFileManager(com.sun.tools.javac.file.JavacFileManager) VirtualFile(com.redhat.ceylon.compiler.typechecker.io.VirtualFile) File(java.io.File)

Example 33 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.

the class LanguageCompiler method getSrcDir.

// FIXME: this function is terrible, possibly refactor it with getPackage?
private File getSrcDir(File sourceFile) throws IOException {
    Iterable<? extends File> prefixes = ((JavacFileManager) fileManager).getLocation(StandardLocation.SOURCE_PATH);
    File srcDirFile = FileUtil.selectPath(prefixes, sourceFile.getPath());
    if (srcDirFile != null) {
        return srcDirFile;
    } else {
        // This error should have been caught by the tool chain
        throw new RuntimeException(sourceFile.getPath() + " is not in the current source path");
    }
}
Also used : JavacFileManager(com.sun.tools.javac.file.JavacFileManager) VirtualFile(com.redhat.ceylon.compiler.typechecker.io.VirtualFile) File(java.io.File)

Example 34 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.

the class AptJavaCompiler method compile.

/** Main method: compile a list of files, return all compiled classes
     *  @param filenames     The names of all files to be compiled.
     */
public List<ClassSymbol> compile(List<String> filenames, Map<String, String> origOptions, ClassLoader aptCL, AnnotationProcessorFactory providedFactory, java.util.Set<Class<? extends AnnotationProcessorFactory>> productiveFactories, java.util.Set<java.io.File> aggregateGenFiles) throws Throwable {
    // it has been used before.
    assert !hasBeenUsed : "attempt to reuse JavaCompiler";
    hasBeenUsed = true;
    this.aggregateGenFiles = aggregateGenFiles;
    long msec = System.currentTimeMillis();
    ListBuffer<ClassSymbol> classes = new ListBuffer<ClassSymbol>();
    try {
        JavacFileManager fm = (JavacFileManager) fileManager;
        //parse all files
        ListBuffer<JCCompilationUnit> trees = new ListBuffer<JCCompilationUnit>();
        for (List<String> l = filenames; l.nonEmpty(); l = l.tail) {
            if (classesAsDecls) {
                if (!l.head.endsWith(".java")) {
                    // process as class file
                    ClassSymbol cs = reader.enterClass(names.fromString(l.head));
                    try {
                        cs.complete();
                    } catch (Symbol.CompletionFailure cf) {
                        bark.aptError("CantFindClass", l);
                        continue;
                    }
                    // add to list of classes
                    classes.append(cs);
                    continue;
                }
            }
            JavaFileObject fo = fm.getJavaFileObjectsFromStrings(List.of(l.head)).iterator().next();
            trees.append(parse(fo));
        }
        //enter symbols for all files
        List<JCCompilationUnit> roots = trees.toList();
        if (errorCount() == 0) {
            boolean prev = bark.setDiagnosticsIgnored(true);
            try {
                enter.main(roots);
            } finally {
                bark.setDiagnosticsIgnored(prev);
            }
        }
        if (errorCount() == 0) {
            apt.main(roots, classes, origOptions, aptCL, providedFactory, productiveFactories);
            genSourceFileNames.addAll(apt.getSourceFileNames());
            genClassFileNames.addAll(apt.getClassFileNames());
        }
    } catch (Abort ex) {
    }
    if (verbose)
        log.printVerbose("total", Long.toString(System.currentTimeMillis() - msec));
    chk.reportDeferredDiagnostics();
    printCount("error", errorCount());
    printCount("warn", warningCount());
    return classes.toList();
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) JavaFileObject(javax.tools.JavaFileObject)

Example 35 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.

the class JavacProcessingEnvironment method handleServiceLoaderUnavailability.

/**
     * Returns an empty processor iterator if no processors are on the
     * relevant path, otherwise if processors are present, logs an
     * error.  Called when a service loader is unavailable for some
     * reason, either because a service loader class cannot be found
     * or because a security policy prevents class loaders from being
     * created.
     *
     * @param key The resource key to use to log an error message
     * @param e   If non-null, pass this exception to Abort
     */
private Iterator<Processor> handleServiceLoaderUnavailability(String key, Exception e) {
    JavaFileManager fileManager = context.get(JavaFileManager.class);
    if (fileManager instanceof JavacFileManager) {
        StandardJavaFileManager standardFileManager = (JavacFileManager) fileManager;
        Iterable<? extends File> workingPath = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH) ? standardFileManager.getLocation(ANNOTATION_PROCESSOR_PATH) : standardFileManager.getLocation(CLASS_PATH);
        if (needClassLoader(options.get(PROCESSOR), workingPath))
            handleException(key, e);
    } else {
        handleException(key, e);
    }
    java.util.List<Processor> pl = Collections.emptyList();
    return pl.iterator();
}
Also used : JavacFileManager(com.sun.tools.javac.file.JavacFileManager) java.util(java.util) javax.lang.model.util(javax.lang.model.util) JavaFileManager(javax.tools.JavaFileManager) StandardJavaFileManager(javax.tools.StandardJavaFileManager) StandardJavaFileManager(javax.tools.StandardJavaFileManager)

Aggregations

JavacFileManager (com.sun.tools.javac.file.JavacFileManager)35 Context (com.sun.tools.javac.util.Context)19 JavaFileObject (javax.tools.JavaFileObject)10 File (java.io.File)9 IOException (java.io.IOException)7 PrintWriter (java.io.PrintWriter)7 JavaFileManager (javax.tools.JavaFileManager)7 VirtualFile (com.redhat.ceylon.compiler.typechecker.io.VirtualFile)5 Path (java.nio.file.Path)5 JavacTask (com.sun.source.util.JavacTask)4 Options (com.sun.tools.javac.util.Options)4 OutputStreamWriter (java.io.OutputStreamWriter)4 BufferedWriter (java.io.BufferedWriter)3 Deps (com.google.devtools.build.lib.view.proto.Deps)2 JavacTool (com.sun.tools.javac.api.JavacTool)2 JavaCompiler (com.sun.tools.javac.main.JavaCompiler)2 AnnotationProcessingError (com.sun.tools.javac.processing.AnnotationProcessingError)2 PropagatedException (com.sun.tools.javac.util.PropagatedException)2 DiagnosticCollector (javax.tools.DiagnosticCollector)2 Test (org.junit.Test)2