Search in sources :

Example 6 with JavaCompiler

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

the class DalvikStatsToolTest method compileSources.

private void compileSources(File outputDir, JavaSourceFromString... sources) {
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    builder.add("-d", outputDir.toString());
    builder.add("-target", TARGETED_JAVA_VERSION);
    builder.add("-source", TARGETED_JAVA_VERSION);
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, builder.build(), null, Arrays.asList(sources));
    assertTrue(task.call());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) JavaCompiler(javax.tools.JavaCompiler)

Example 7 with JavaCompiler

use of javax.tools.JavaCompiler in project jsonschema2pojo by joelittlejohn.

the class CompilerWarningIT method parameters.

@Parameters(name = "{0}")
public static Collection<Object[]> parameters() {
    JavaCompiler systemJavaCompiler = Compiler.systemJavaCompiler();
    JavaCompiler eclipseCompiler = Compiler.eclipseCompiler();
    return Arrays.asList(new Object[][] { { "includeAccessorsWithSystemJavaCompiler", systemJavaCompiler, config("includeDynamicAccessors", true), "/schema/dynamic/parentType.json", Matchers.empty() }, { "includeAccessorsWithEclipseCompiler", eclipseCompiler, config("includeDynamicAccessors", true), "/schema/dynamic/parentType.json", onlyCastExceptions() } });
}
Also used : JavaCompiler(javax.tools.JavaCompiler) Parameters(org.junit.runners.Parameterized.Parameters)

Example 8 with JavaCompiler

use of javax.tools.JavaCompiler in project querydsl by querydsl.

the class MetaDataSerializerTest method compile.

private void compile(MetaDataExporter exporter) {
    JavaCompiler compiler = new SimpleCompiler();
    Set<String> classes = exporter.getClasses();
    int compilationResult = compiler.run(null, null, null, classes.toArray(new String[classes.size()]));
    if (compilationResult == 0) {
        System.out.println("Compilation is successful");
    } else {
        Assert.fail("Compilation Failed");
    }
}
Also used : SimpleCompiler(com.mysema.codegen.SimpleCompiler) JavaCompiler(javax.tools.JavaCompiler)

Example 9 with JavaCompiler

use of javax.tools.JavaCompiler in project lombok by rzwitserloot.

the class TestClassFileMetaData method compile.

static byte[] compile(File file) {
    try {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        File tempDir = getTempDir();
        tempDir.mkdirs();
        List<String> options = Arrays.asList("-proc:none", "-d", tempDir.getAbsolutePath());
        StringWriter captureWarnings = new StringWriter();
        final StringBuilder compilerErrors = new StringBuilder();
        DiagnosticListener<JavaFileObject> diagnostics = new DiagnosticListener<JavaFileObject>() {

            @Override
            public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
                compilerErrors.append(diagnostic.toString()).append("\n");
            }
        };
        CompilationTask task = compiler.getTask(captureWarnings, null, diagnostics, options, null, Collections.singleton(new ContentBasedJavaFileObject(file.getPath(), readFileAsString(file))));
        Boolean taskResult = task.call();
        assertTrue("Compilation task didn't succeed: \n<Warnings and Errors>\n" + compilerErrors.toString() + "\n</Warnings and Errors>", taskResult);
        return PostCompilerApp.readFile(new File(tempDir, file.getName().replaceAll("\\.java$", ".class")));
    } catch (Exception e) {
        throw Lombok.sneakyThrow(e);
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) Diagnostic(javax.tools.Diagnostic) DiagnosticListener(javax.tools.DiagnosticListener) CompilationTask(javax.tools.JavaCompiler.CompilationTask) IOException(java.io.IOException) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) StringWriter(java.io.StringWriter) File(java.io.File)

Example 10 with JavaCompiler

use of javax.tools.JavaCompiler in project jadx by skylot.

the class StaticCompiler method compile.

public static List<File> compile(List<File> files, File outDir, boolean includeDebugInfo) throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(files);
    StaticFileManager staticFileManager = new StaticFileManager(fileManager, outDir);
    List<String> options = new ArrayList<String>();
    options.add(includeDebugInfo ? "-g" : "-g:none");
    options.addAll(COMMON_ARGS);
    CompilationTask task = compiler.getTask(null, staticFileManager, null, options, null, compilationUnits);
    Boolean result = task.call();
    fileManager.close();
    if (Boolean.TRUE.equals(result)) {
        return staticFileManager.outputFiles();
    }
    return Collections.emptyList();
}
Also used : ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Aggregations

JavaCompiler (javax.tools.JavaCompiler)101 StandardJavaFileManager (javax.tools.StandardJavaFileManager)43 JavaFileObject (javax.tools.JavaFileObject)39 File (java.io.File)31 DiagnosticCollector (javax.tools.DiagnosticCollector)24 ArrayList (java.util.ArrayList)22 JavacTask (com.sun.source.util.JavacTask)20 IOException (java.io.IOException)17 CompilationTask (javax.tools.JavaCompiler.CompilationTask)16 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)15 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)11 Diagnostic (javax.tools.Diagnostic)11 PrintWriter (java.io.PrintWriter)9 Test (org.junit.Test)9 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 FileOutputStream (java.io.FileOutputStream)5 StringWriter (java.io.StringWriter)5 JavaFileManager (javax.tools.JavaFileManager)5 OutputStream (java.io.OutputStream)4