Search in sources :

Example 51 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class T6472751 method main.

public static void main(String[] args) throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    JavacTask task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
    trees = Trees.instance(task);
    positions = trees.getSourcePositions();
    Iterable<? extends CompilationUnitTree> asts = task.parse();
    for (CompilationUnitTree ast : asts) {
        new MyVisitor().scan(ast, null);
    }
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Example 52 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class ImplementationCacheTest method main.

public static void main(String[] args) throws IOException {
    List<? extends JavaFileObject> files = Arrays.asList(new SourceFile());
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTask ct = (JavacTask) tool.getTask(null, null, null, null, null, files);
    Context ctx = new Context();
    JavacFileManager.preRegister(ctx);
    checkImplementationCache(ct.analyze(), Types.instance(ctx));
}
Also used : Context(com.sun.tools.javac.util.Context) JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Example 53 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class Main method main.

public static void main(String[] args) throws Exception {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    fm.setLocation(CLASS_PATH, Collections.<File>emptyList());
    JavacTask javac = (JavacTask) tool.getTask(null, fm, null, null, null, null);
    Elements elements = javac.getElements();
    final Set<String> packages = new LinkedHashSet<String>();
    int nestedClasses = 0;
    int classes = 0;
    for (JavaFileObject file : fm.list(PLATFORM_CLASS_PATH, "", EnumSet.of(CLASS), true)) {
        String type = fm.inferBinaryName(PLATFORM_CLASS_PATH, file);
        if (type.endsWith("package-info"))
            continue;
        try {
            TypeElement elem = elements.getTypeElement(type);
            if (elem == null && type.indexOf('$') > 0) {
                nestedClasses++;
                type = null;
                continue;
            }
            classes++;
            packages.add(getPackage(elem).getQualifiedName().toString());
            // force completion
            elements.getTypeElement(type).getKind();
            type = null;
        } finally {
            if (type != null)
                System.err.println("Looking at " + type);
        }
    }
    javac = null;
    elements = null;
    javac = (JavacTask) tool.getTask(null, fm, null, null, null, null);
    elements = javac.getElements();
    for (String name : packages) {
        PackageElement pe = elements.getPackageElement(name);
        for (Element e : pe.getEnclosedElements()) {
            e.getSimpleName().getClass();
        }
    }
    /*
         * A few sanity checks based on current values:
         *
         * packages: 775, classes: 12429 + 5917
         *
         * As the platform evolves the numbers are likely to grow
         * monotonically but in case somebody gets a clever idea for
         * limiting the number of packages exposed, this number might
         * drop.  So we test low values.
         */
    System.out.format("packages: %s, classes: %s + %s%n", packages.size(), classes, nestedClasses);
    if (classes < 9000)
        throw new AssertionError("Too few classes in PLATFORM_CLASS_PATH ;-)");
    if (packages.size() < 530)
        throw new AssertionError("Too few packages in PLATFORM_CLASS_PATH ;-)");
    if (nestedClasses < 3000)
        throw new AssertionError("Too few nested classes in PLATFORM_CLASS_PATH ;-)");
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) PackageElement(javax.lang.model.element.PackageElement) JavacTask(com.sun.source.util.JavacTask)

Example 54 with JavacTask

use of com.sun.source.util.JavacTask in project bazel by bazelbuild.

the class BlazeJavacMain method compile.

public static BlazeJavacResult compile(BlazeJavacArguments arguments) {
    List<String> javacArguments = arguments.javacOptions();
    try {
        javacArguments = processPluginArgs(arguments.plugins(), javacArguments);
    } catch (InvalidCommandLineException e) {
        return BlazeJavacResult.error(e.getMessage());
    }
    Context context = new Context();
    setupBlazeJavaCompiler(arguments.plugins(), context);
    boolean ok = false;
    StringWriter errOutput = new StringWriter();
    // TODO(cushon): where is this used when a diagnostic listener is registered? Consider removing
    // it and handling exceptions directly in callers.
    PrintWriter errWriter = new PrintWriter(errOutput);
    Listener diagnostics = new Listener(context);
    BlazeJavaCompiler compiler;
    try (JavacFileManager fileManager = new ClassloaderMaskingFileManager()) {
        JavacTask task = JavacTool.create().getTask(errWriter, fileManager, diagnostics, javacArguments, ImmutableList.of(), /*classes*/
        fileManager.getJavaFileObjectsFromPaths(arguments.sourceFiles()), context);
        if (arguments.processors() != null) {
            task.setProcessors(arguments.processors());
        }
        fileManager.setContext(context);
        setLocations(fileManager, arguments);
        try {
            ok = task.call();
        } catch (PropagatedException e) {
            throw e.getCause();
        }
    } catch (Throwable t) {
        t.printStackTrace(errWriter);
        ok = false;
    } finally {
        compiler = (BlazeJavaCompiler) JavaCompiler.instance(context);
        if (ok) {
            // or empty source files.
            if (compiler.skippedFlowEvents() > 0 && compiler.flowEvents() == 0) {
                errWriter.println("Expected at least one FLOW event");
                ok = false;
            }
        }
    }
    errWriter.flush();
    return new BlazeJavacResult(ok, filterDiagnostics(diagnostics.build()), errOutput.toString(), compiler);
}
Also used : Context(com.sun.tools.javac.util.Context) Listener(com.google.devtools.build.buildjar.javac.FormattedDiagnostic.Listener) PropagatedException(com.sun.tools.javac.util.PropagatedException) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) StringWriter(java.io.StringWriter) InvalidCommandLineException(com.google.devtools.build.buildjar.InvalidCommandLineException) JavacTask(com.sun.source.util.JavacTask) PrintWriter(java.io.PrintWriter)

Example 55 with JavacTask

use of com.sun.source.util.JavacTask in project bazel by bazelbuild.

the class JavacTurbineCompiler method compile.

static JavacTurbineCompileResult compile(JavacTurbineCompileRequest request) throws IOException {
    Map<String, OutputFileObject> files = new LinkedHashMap<>();
    Status status;
    StringWriter sw = new StringWriter();
    Context context = new Context();
    try (PrintWriter pw = new PrintWriter(sw)) {
        setupContext(context, request.strictJavaDepsPlugin());
        CacheFSInfo.preRegister(context);
        try (ZipOutputFileManager fm = new ZipOutputFileManager(files)) {
            JavacTask task = JavacTool.create().getTask(pw, fm, null, /*diagnostics*/
            request.javacOptions(), ImmutableList.of(), /*classes*/
            fm.getJavaFileObjectsFromPaths(request.sources()), context);
            fm.setContext(context);
            fm.setLocationFromPaths(StandardLocation.SOURCE_PATH, Collections.<Path>emptyList());
            fm.setLocationFromPaths(StandardLocation.CLASS_PATH, request.classPath());
            fm.setLocationFromPaths(StandardLocation.PLATFORM_CLASS_PATH, request.bootClassPath());
            fm.setLocationFromPaths(StandardLocation.ANNOTATION_PROCESSOR_PATH, request.processorClassPath());
            status = task.call() ? Status.OK : Status.ERROR;
        } catch (Throwable t) {
            t.printStackTrace(pw);
            status = Status.ERROR;
        }
    }
    return new JavacTurbineCompileResult(ImmutableMap.copyOf(files), status, sw, context);
}
Also used : Status(com.google.devtools.build.java.turbine.javac.JavacTurbineCompileResult.Status) Context(com.sun.tools.javac.util.Context) OutputFileObject(com.google.devtools.build.java.turbine.javac.ZipOutputFileManager.OutputFileObject) StringWriter(java.io.StringWriter) JavacTask(com.sun.source.util.JavacTask) LinkedHashMap(java.util.LinkedHashMap) PrintWriter(java.io.PrintWriter)

Aggregations

JavacTask (com.sun.source.util.JavacTask)97 JavaCompiler (javax.tools.JavaCompiler)45 JavaFileObject (javax.tools.JavaFileObject)27 JavacTool (com.sun.tools.javac.api.JavacTool)24 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)23 StandardJavaFileManager (javax.tools.StandardJavaFileManager)17 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)16 IOException (java.io.IOException)14 DiagnosticCollector (javax.tools.DiagnosticCollector)13 File (java.io.File)12 StringWriter (java.io.StringWriter)12 Context (com.sun.tools.javac.util.Context)11 PrintWriter (java.io.PrintWriter)11 JavacFileManager (com.sun.tools.javac.file.JavacFileManager)9 Path (java.nio.file.Path)8 ArrayList (java.util.ArrayList)8 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)7 Diagnostic (javax.tools.Diagnostic)7 ClassTree (com.sun.source.tree.ClassTree)6 Trees (com.sun.source.util.Trees)6