Search in sources :

Example 26 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 27 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 28 with StandardJavaFileManager

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

the class BaseErrorProneCompiler method run.

public Result run(String[] argv) {
    try {
        argv = CommandLine.parse(argv);
    } catch (IOException e) {
        throw new IOError(e);
    }
    List<String> javacOpts = new ArrayList<>();
    List<String> sources = new ArrayList<>();
    for (String arg : argv) {
        // TODO(cushon): is there a better way to categorize javacopts?
        if (!arg.startsWith("-") && arg.endsWith(".java")) {
            sources.add(arg);
        } else {
            javacOpts.add(arg);
        }
    }
    StandardJavaFileManager fileManager = new MaskedFileManager();
    return run(javacOpts.toArray(new String[0]), fileManager, ImmutableList.copyOf(fileManager.getJavaFileObjectsFromStrings(sources)), null);
}
Also used : MaskedFileManager(com.google.errorprone.MaskedClassLoader.MaskedFileManager) IOError(java.io.IOError) ArrayList(java.util.ArrayList) StandardJavaFileManager(javax.tools.StandardJavaFileManager) IOException(java.io.IOException)

Example 29 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 30 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)

Aggregations

StandardJavaFileManager (javax.tools.StandardJavaFileManager)57 JavaCompiler (javax.tools.JavaCompiler)42 JavaFileObject (javax.tools.JavaFileObject)24 File (java.io.File)23 IOException (java.io.IOException)18 ArrayList (java.util.ArrayList)17 DiagnosticCollector (javax.tools.DiagnosticCollector)15 CompilationTask (javax.tools.JavaCompiler.CompilationTask)9 JavacTask (com.sun.source.util.JavacTask)7 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)4 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)4 JavacTool (com.sun.tools.javac.api.JavacTool)4 FileOutputStream (java.io.FileOutputStream)4 StringWriter (java.io.StringWriter)3 ZipFile (java.util.zip.ZipFile)3 Test (org.junit.Test)3 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)2 PrintWriter (java.io.PrintWriter)2 Method (java.lang.reflect.Method)2 URI (java.net.URI)2