Search in sources :

Example 6 with StandardJavaFileManager

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

the class BazelJavaCompiler method newInstance.

private static JavaCompiler newInstance(final JavaCompiler delegate) {
    // We forward most operations to the JavaCompiler implementation in langtools.jar.
    return new JavaCompiler() {

        @Override
        public CompilationTask getTask(Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Iterable<String> options, Iterable<String> classes, Iterable<? extends JavaFileObject> compilationUnits) {
            // We prepend bazel's default javacopts to user javacopts,
            // so that the user can override them. javac supports this
            // "last option wins" style of option override.
            ImmutableList.Builder<String> fullOptions = ImmutableList.builder();
            fullOptions.addAll(getDefaultJavacopts());
            if (options != null) {
                fullOptions.addAll(options);
            }
            return delegate.getTask(out, fileManager, diagnosticListener, fullOptions.build(), classes, compilationUnits);
        }

        @Override
        public StandardJavaFileManager getStandardFileManager(DiagnosticListener<? super JavaFileObject> diagnosticListener, Locale locale, Charset charset) {
            StandardJavaFileManager fileManager = delegate.getStandardFileManager(diagnosticListener, locale, charset);
            try {
                fileManager.setLocation(// bootclasspath
                StandardLocation.PLATFORM_CLASS_PATH, JavacBootclasspath.asFiles());
            } catch (IOException e) {
                // Should never happen, according to javadocs for setLocation
                throw new RuntimeException(e);
            }
            return fileManager;
        }

        @Override
        public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
            // prepend bazel's default javacopts to user arguments
            List<String> args = ImmutableList.<String>builder().addAll(getDefaultJavacopts()).add(arguments).build();
            return delegate.run(in, out, err, args.toArray(new String[0]));
        }

        @Override
        public Set<SourceVersion> getSourceVersions() {
            return delegate.getSourceVersions();
        }

        @Override
        public int isSupportedOption(String option) {
            return delegate.isSupportedOption(option);
        }
    };
}
Also used : Locale(java.util.Locale) JavaFileManager(javax.tools.JavaFileManager) StandardJavaFileManager(javax.tools.StandardJavaFileManager) ImmutableList(com.google.common.collect.ImmutableList) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) JavaCompiler(javax.tools.JavaCompiler) Charset(java.nio.charset.Charset) DiagnosticListener(javax.tools.DiagnosticListener) SourceVersion(javax.lang.model.SourceVersion) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) Writer(java.io.Writer)

Example 7 with StandardJavaFileManager

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

the class BazelJavaCompilerTest method assertCompileSucceeds.

private void assertCompileSucceeds(final String uri, final String content) throws Exception {
    JavaCompiler javac = BazelJavaCompiler.newInstance();
    JavaFileObject source = new SimpleJavaFileObject(URI.create(uri), JavaFileObject.Kind.SOURCE) {

        @Override
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            return content;
        }
    };
    StandardJavaFileManager fileManager = javac.getStandardFileManager(null, null, null);
    // setting the output path by passing a flag to getTask is not reliable
    fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(getTmpDir()));
    DiagnosticCollector<JavaFileObject> messages = new DiagnosticCollector<>();
    JavaCompiler.CompilationTask task = javac.getTask(null, fileManager, messages, null, null, Collections.singletonList(source));
    assertTrue(task.call());
    assertTrue(messages.getDiagnostics().isEmpty());
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector)

Example 8 with StandardJavaFileManager

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

the class CodeTransformerTestHelper method transform.

public JavaFileObject transform(JavaFileObject original) {
    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(original));
    try {
        SourceFile sourceFile = SourceFile.create(original);
        Iterable<? extends CompilationUnitTree> trees = task.parse();
        task.analyze();
        JCCompilationUnit tree = Iterables.getOnlyElement(Iterables.filter(trees, JCCompilationUnit.class));
        DescriptionBasedDiff diff = DescriptionBasedDiff.create(tree);
        transformer().apply(new TreePath(tree), task.getContext(), diff);
        diff.applyDifferences(sourceFile);
        return JavaFileObjects.forSourceString(Iterables.getOnlyElement(Iterables.filter(tree.getTypeDecls(), JCClassDecl.class)).sym.getQualifiedName().toString(), sourceFile.getSourceText());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavaCompiler(javax.tools.JavaCompiler) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) TreePath(com.sun.source.util.TreePath) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) SourceFile(com.google.errorprone.apply.SourceFile)

Example 9 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project gradle by gradle.

the class JdkJavaCompiler method createCompileTask.

private JavaCompiler.CompilationTask createCompileTask(JavaCompileSpec spec) {
    List<String> options = new JavaCompilerArgumentsBuilder(spec).build();
    JavaCompiler compiler = javaHomeBasedJavaCompilerFactory.create();
    CompileOptions compileOptions = spec.getCompileOptions();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, compileOptions.getEncoding() != null ? Charset.forName(compileOptions.getEncoding()) : null);
    Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(spec.getSource());
    return compiler.getTask(null, null, null, options, null, compilationUnits);
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) CompileOptions(org.gradle.api.tasks.compile.CompileOptions)

Example 10 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project mastering-java by Kingminghuang.

the class Compiler method main.

public static void main(String[] args) {
    String source = "public class Main{" + "public static void main(String[] args){System.out.println(\"Hello World!\");}}";
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    StringSourceJavaObject sourceObject = new StringSourceJavaObject("Main", source);
    JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(sourceObject));
    boolean result = task.call();
    if (result) {
        System.out.println("Compiled successfully");
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager)

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