Search in sources :

Example 61 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project bazel by bazelbuild.

the class IjarTests method makeCompilationTask.

private JavaCompiler.CompilationTask makeCompilationTask(String... files) throws IOException {
    JavaCompiler compiler = BazelJavaCompiler.newInstance();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    fileManager.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(new File("third_party/ijar/test/interface_ijar_testlib.jar")));
    fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(getTmpDir()));
    diagnostics = new DiagnosticCollector<JavaFileObject>();
    return compiler.getTask(null, fileManager, diagnostics, // used for deprecation tests
    Arrays.asList("-Xlint:deprecation"), null, fileManager.getJavaFileObjects(files));
}
Also used : JavaFileObject(javax.tools.JavaFileObject) JavaCompiler(javax.tools.JavaCompiler) BazelJavaCompiler(com.google.devtools.build.java.bazel.BazelJavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 62 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project buck by facebook.

the class DefaultClassUsageFileWriterTest method fileReadOrderDoesntAffectClassesUsedOutput.

@Test
public void fileReadOrderDoesntAffectClassesUsedOutput() throws IOException {
    ProjectFilesystem filesystem = FakeProjectFilesystem.createRealTempFilesystem();
    Path testJarPath = filesystem.getPathForRelativePath("test.jar");
    Path testTwoJarPath = filesystem.getPathForRelativePath("test2.jar");
    Path outputOne = filesystem.getPathForRelativePath("used-classes-one.json");
    Path outputTwo = filesystem.getPathForRelativePath("used-classes-two.json");
    FakeStandardJavaFileManager fakeFileManager = new FakeStandardJavaFileManager();
    fakeFileManager.addFile(testJarPath, OTHER_FILE_NAME, JavaFileObject.Kind.OTHER);
    fakeFileManager.addFile(testJarPath, SOURCE_FILE_NAME, JavaFileObject.Kind.SOURCE);
    fakeFileManager.addFile(testJarPath, HTML_FILE_NAME, JavaFileObject.Kind.HTML);
    fakeFileManager.addFile(testJarPath, SINGLE_NON_JAVA_FILE_NAME, JavaFileObject.Kind.OTHER);
    for (String fileName : FILE_NAMES) {
        fakeFileManager.addFile(testJarPath, fileName, JavaFileObject.Kind.CLASS);
    }
    for (String fileName : FILE_NAMES) {
        fakeFileManager.addFile(testTwoJarPath, fileName, JavaFileObject.Kind.CLASS);
    }
    DefaultClassUsageFileWriter writerOne = new DefaultClassUsageFileWriter(outputOne);
    {
        StandardJavaFileManager wrappedFileManager = writerOne.wrapFileManager(fakeFileManager);
        for (JavaFileObject javaFileObject : wrappedFileManager.list(null, null, null, false)) {
            javaFileObject.openInputStream();
        }
    }
    writerOne.writeFile(filesystem, ObjectMappers.newDefaultInstance());
    DefaultClassUsageFileWriter writerTwo = new DefaultClassUsageFileWriter(outputTwo);
    {
        StandardJavaFileManager wrappedFileManager = writerTwo.wrapFileManager(fakeFileManager);
        Iterable<JavaFileObject> fileObjects = wrappedFileManager.list(null, null, null, false);
        for (JavaFileObject javaFileObject : FluentIterable.from(fileObjects).toList().reverse()) {
            javaFileObject.openInputStream();
        }
    }
    writerTwo.writeFile(filesystem, ObjectMappers.newDefaultInstance());
    assertEquals(new String(Files.readAllBytes(outputOne)), new String(Files.readAllBytes(outputTwo)));
}
Also used : Path(java.nio.file.Path) JavaFileObject(javax.tools.JavaFileObject) FluentIterable(com.google.common.collect.FluentIterable) StandardJavaFileManager(javax.tools.StandardJavaFileManager) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) Test(org.junit.Test)

Example 63 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project hibernate-orm by hibernate.

the class CompilationStatement method compile.

private void compile(List<File> sourceFiles) throws Exception {
    List<String> options = createJavaOptions();
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
    Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(sourceFiles);
    compileSources(options, compiler, diagnostics, fileManager, compilationUnits);
    compilationDiagnostics.addAll(diagnostics.getDiagnostics());
    fileManager.close();
}
Also used : JavaFileObject(javax.tools.JavaFileObject) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector)

Example 64 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project error-prone by google.

the class CompilerBasedTest method compile.

protected void compile(TreeScanner scanner, JavaFileObject fileObject) {
    JavaCompiler compiler = JavacTool.create();
    DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticsCollector, Locale.ENGLISH, UTF_8);
    JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(CharStreams.nullWriter(), fileManager, diagnosticsCollector, ImmutableList.<String>of(), null, ImmutableList.of(fileObject));
    try {
        this.sourceFile = SourceFile.create(fileObject);
        Iterable<? extends CompilationUnitTree> trees = task.parse();
        task.analyze();
        for (CompilationUnitTree tree : trees) {
            scanner.scan((JCCompilationUnit) tree);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    this.context = task.getContext();
}
Also used : JavaFileObject(javax.tools.JavaFileObject) JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) IOException(java.io.IOException)

Example 65 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project jdk8u_jdk by JetBrains.

the class ManyMethodsBenchmarkApp method main.

public static void main(String[] args) throws Exception {
    System.out.println("test started: ManyMethodsBenchmarkApp");
    // Create source files with many methods
    File base = new File("Base.java");
    try (FileWriter fw = new FileWriter(base)) {
        fw.write("public class Base {\n");
        final int L = 10;
        // Each of the first L methods makes calls to its own chunk of METHOD_COUNT/L methods
        for (int k = 0; k < L; k++) {
            fw.write("    public void f" + k + "() {\n");
            int shift = (k == 0) ? L : 0;
            for (int i = (k * (METHOD_COUNT / L)) + shift; i < (k + 1) * METHOD_COUNT / L; i++) {
                fw.write("        f" + i + "();\n");
            }
            fw.write("    }\n");
        }
        // The rest of (METHOD_COUNT - L) methods have empty body
        for (int i = L; i < METHOD_COUNT; i++) {
            fw.write("    public static void f" + i + "() {}\n");
        }
        fw.write("}\n");
    }
    // Compile the generated source files.
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    List<File> files = Arrays.asList(new File[] { base });
    try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
        compiler.getTask(null, fileManager, null, null, null, fileManager.getJavaFileObjectsFromFiles(files)).call();
    }
    benchmarkClassOperations("Base");
    ManyMethodsBenchmarkAgent.instr();
    // Cleanup
    base.delete();
    new File("Base.class").delete();
    if (!ManyMethodsBenchmarkAgent.completed) {
        throw new Exception("ERROR: ManyMethodsBenchmarkAgent did not complete.");
    }
    if (ManyMethodsBenchmarkAgent.fail) {
        throw new Exception("ERROR: ManyMethodsBenchmarkAgent failed.");
    } else {
        System.out.println("ManyMethodsBenchmarkAgent succeeded.");
    }
    System.out.println("test finished: ManyMethodsBenchmarkApp");
}
Also used : FileWriter(java.io.FileWriter) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException)

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