Search in sources :

Example 46 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project auto by google.

the class GeneratedAnnotationsTest method runProcessor.

/**
 * Run {@link TestProcessor} in a compilation with the given {@code options}, and prevent the
 * compilation from accessing classes with the qualified names in {@code maskFromClasspath}.
 */
private String runProcessor(ImmutableList<String> options, @Nullable String packageToMask) throws IOException {
    File tempDir = temporaryFolder.newFolder();
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(/* diagnosticListener= */
    null, /* locale= */
    null, UTF_8);
    standardFileManager.setLocation(StandardLocation.CLASS_OUTPUT, ImmutableList.of(tempDir));
    StandardJavaFileManager proxyFileManager = Reflection.newProxy(StandardJavaFileManager.class, new FileManagerInvocationHandler(standardFileManager, packageToMask));
    CompilationTask task = compiler.getTask(/* out= */
    null, proxyFileManager, /* diagnosticListener= */
    null, options, /* classes= */
    null, ImmutableList.of(new SimpleJavaFileObject(URI.create("test"), Kind.SOURCE) {

        @Override
        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
            return "class Test {}";
        }
    }));
    task.setProcessors(ImmutableList.of(new TestProcessor()));
    assertThat(task.call()).isTrue();
    return new String(Files.readAllBytes(tempDir.toPath().resolve("G.java")), UTF_8);
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) File(java.io.File) JavaFile(com.squareup.javapoet.JavaFile) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 47 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project j2objc by google.

the class JavacParser method createEnvironment.

// Creates a javac environment from a collection of files and/or file objects.
private JavacEnvironment createEnvironment(List<File> files, List<JavaFileObject> fileObjects, boolean processAnnotations) throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
    StandardJavaFileManager fileManager = getFileManager(compiler, diagnostics);
    List<String> javacOptions = getJavacOptions(processAnnotations);
    if (fileObjects == null) {
        fileObjects = new ArrayList<>();
    }
    for (JavaFileObject jfo : fileManager.getJavaFileObjectsFromFiles(files)) {
        fileObjects.add(filterJavaFileObject(jfo));
    }
    JavacTask task = (JavacTask) compiler.getTask(null, fileManager, diagnostics, javacOptions, null, fileObjects);
    return new JavacEnvironment(task, fileManager, diagnostics);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) JavacTask(com.sun.source.util.JavacTask)

Example 48 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.

the class GenericConstructorAndDiamondTest method main.

public static void main(String... args) throws Exception {
    // create default shared JavaCompiler - reused across multiple compilations
    JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
    for (BoundKind boundKind : BoundKind.values()) {
        for (ConstructorKind constructorKind : ConstructorKind.values()) {
            for (TypeArgumentKind declArgKind : TypeArgumentKind.values()) {
                for (TypeArgArity arity : TypeArgArity.values()) {
                    for (TypeArgumentKind useArgKind : TypeArgumentKind.values()) {
                        for (TypeArgumentKind diamondArgKind : TypeArgumentKind.values()) {
                            for (ArgumentKind argKind : ArgumentKind.values()) {
                                new GenericConstructorAndDiamondTest(boundKind, constructorKind, declArgKind, arity, useArgKind, diamondArgKind, argKind).run(comp, fm);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager)

Example 49 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.

the class T6993305 method run.

void run() throws Exception {
    File testSrc = new File(System.getProperty("test.src"));
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    File f = new File(testSrc, T6993305.class.getSimpleName() + ".java");
    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(f);
    JavacTask task = tool.getTask(null, fm, null, null, null, fos);
    Iterable<? extends CompilationUnitTree> cus = task.parse();
    TestScanner s = new TestScanner();
    s.scan(cus, task);
    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
Also used : JavacTool(com.sun.tools.javac.api.JavacTool) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) File(java.io.File) IOException(java.io.IOException)

Example 50 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.

the class GenStubs method run.

boolean run(String sourcepath, File outdir, List<String> classes) {
    // System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
    if (sourcepath == null)
        throw new IllegalArgumentException("sourcepath not set");
    if (outdir == null)
        throw new IllegalArgumentException("source output dir not set");
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    try {
        fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
        fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
        List<JavaFileObject> files = new ArrayList<JavaFileObject>();
        for (String c : classes) {
            JavaFileObject fo = fm.getJavaFileForInput(StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
            if (fo == null)
                error("class not found: " + c);
            else
                files.add(fo);
        }
        JavacTask t = tool.getTask(null, fm, null, null, null, files);
        Iterable<? extends CompilationUnitTree> trees = t.parse();
        for (CompilationUnitTree tree : trees) {
            makeStub(fm, tree);
        }
    } catch (IOException e) {
        error("IO error " + e, e);
    }
    return (errors == 0);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavacTool(com.sun.tools.javac.api.JavacTool) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask)

Aggregations

StandardJavaFileManager (javax.tools.StandardJavaFileManager)124 JavaCompiler (javax.tools.JavaCompiler)99 File (java.io.File)60 JavaFileObject (javax.tools.JavaFileObject)50 IOException (java.io.IOException)37 DiagnosticCollector (javax.tools.DiagnosticCollector)36 ArrayList (java.util.ArrayList)33 CompilationTask (javax.tools.JavaCompiler.CompilationTask)25 JavacTask (com.sun.source.util.JavacTask)17 StringWriter (java.io.StringWriter)10 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)9 JavacTool (com.sun.tools.javac.api.JavacTool)8 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)6 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)5 URL (java.net.URL)5 FileOutputStream (java.io.FileOutputStream)4 PrintWriter (java.io.PrintWriter)4 Diagnostic (javax.tools.Diagnostic)4 Charset (java.nio.charset.Charset)3 HashSet (java.util.HashSet)3