Search in sources :

Example 61 with JavaCompiler

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

the class CodeGenerationHelper method compile.

public static ClassLoader compile(JavaCompiler compiler, Writer out, File sourceDirectory, File outputDirectory, List<File> classpath, Map<String, Object> config, DiagnosticListener<? super JavaFileObject> listener) {
    List<File> fullClasspath = new ArrayList<File>();
    fullClasspath.addAll(classpath);
    fullClasspath.addAll(CodeGenerationHelper.classpathToFileArray(System.getProperty("java.class.path")));
    new Compiler().compile(compiler, out, sourceDirectory, outputDirectory, fullClasspath, listener, (String) config.get("targetVersion"));
    try {
        return URLClassLoader.newInstance(new URL[] { outputDirectory.toURI().toURL() }, Thread.currentThread().getContextClassLoader());
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) Compiler.systemJavaCompiler(org.jsonschema2pojo.integration.util.Compiler.systemJavaCompiler) MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) File(java.io.File)

Example 62 with JavaCompiler

use of javax.tools.JavaCompiler in project antlr4 by antlr.

the class BaseJavaTest method compile.

/**
	 * Wow! much faster than compiling outside of VM. Finicky though.
	 * Had rules called r and modulo. Wouldn't compile til I changed to 'a'.
	 */
protected boolean compile(String... fileNames) {
    List<File> files = new ArrayList<File>();
    for (String fileName : fileNames) {
        File f = new File(tmpdir, fileName);
        files.add(f);
    }
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    //		DiagnosticCollector<JavaFileObject> diagnostics =
    //			new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(files);
    Iterable<String> compileOptions = Arrays.asList("-g", "-source", "1.6", "-target", "1.6", "-implicit:class", "-Xlint:-options", "-d", tmpdir, "-cp", tmpdir + pathSep + CLASSPATH);
    JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, compileOptions, null, compilationUnits);
    boolean ok = task.call();
    try {
        fileManager.close();
    } catch (IOException ioe) {
        ioe.printStackTrace(System.err);
    }
    return ok;
}
Also used : ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) STGroupString(org.stringtemplate.v4.STGroupString) IOException(java.io.IOException) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)

Example 63 with JavaCompiler

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

the class ProcessorTestHelper method compileFiles.

public CompileResult compileFiles(Collection<File> compilationUnits) {
    DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, null, null)) {
        CompilationTask task = compiler.getTask(null, fileManager, diagnosticCollector, compilerOptions, null, fileManager.getJavaFileObjectsFromFiles(compilationUnits));
        List<Processor> processors = new ArrayList<>();
        for (Class<? extends Processor> processorClass : processorsClasses) {
            try {
                processors.add(processorClass.newInstance());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        task.setProcessors(processors);
        task.call();
    } catch (IOException e) {
    // we should always be able to close the manager
    }
    return new CompileResult(diagnosticCollector.getDiagnostics());
}
Also used : Processor(javax.annotation.processing.Processor) ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) IOException(java.io.IOException) CompilationTask(javax.tools.JavaCompiler.CompilationTask) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector)

Example 64 with JavaCompiler

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

the class EclipseCompilationTest method test.

@Test
@Ignore
public void test() throws IOException {
    System.setProperty("jdt.compiler.useSingleThread", "true");
    // select classes
    List<String> classes = new ArrayList<String>();
    for (File file : new File(packagePath).listFiles()) {
        if (file.getName().endsWith(".java")) {
            classes.add(file.getPath());
        }
    }
    // prepare output
    File out = new File("target/out-eclipse");
    FileUtils.delete(out);
    if (!out.mkdirs()) {
        Assert.fail("Creation of " + out.getPath() + " failed");
    }
    String classPath = SimpleCompiler.getClassPath((URLClassLoader) getClass().getClassLoader());
    JavaCompiler compiler = new EclipseCompiler();
    List<String> options = new ArrayList<String>();
    options.add("-s");
    options.add("target/out-eclipse");
    options.add("-proc:only");
    options.add("-processor");
    options.add(QuerydslAnnotationProcessor.class.getName());
    options.add("-Aquerydsl.entityAccessors=true");
    options.add("-cp");
    options.add(classPath);
    options.add("-source");
    options.add("1.6");
    options.add("-verbose");
    options.addAll(classes);
    int compilationResult = compiler.run(null, System.out, System.err, options.toArray(new String[options.size()]));
    if (compilationResult == 0) {
        System.out.println("Compilation is successful");
    } else {
        Assert.fail("Compilation Failed");
    }
    File resultFile = new File("target/out-eclipse/com/querydsl/eclipse/QSimpleEntity.java");
    assertTrue(resultFile.exists());
    String result = Files.toString(resultFile, Charsets.UTF_8);
    assertTrue(result.contains("NumberPath<java.math.BigDecimal> bigDecimalProp"));
    assertTrue(result.contains("NumberPath<Integer> integerProp"));
    assertTrue(result.contains("NumberPath<Integer> intProp"));
    assertTrue(result.contains("StringPath stringProp"));
}
Also used : EclipseCompiler(org.eclipse.jdt.internal.compiler.tool.EclipseCompiler) ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 65 with JavaCompiler

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

the class CompileMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager sjfm = jc.getStandardFileManager(null, null, null);
    try {
        Set<File> generatedFiles = getJavaFiles(sourceFolder);
        Iterable<? extends JavaFileObject> fileObjects = sjfm.getJavaFileObjectsFromFiles(generatedFiles);
        List<String> opts = getCompilerOptions();
        jc.getTask(null, null, null, opts, null, fileObjects).call();
    } finally {
        try {
            sjfm.close();
        } catch (IOException e) {
            throw new MojoFailureException(e.getMessage(), e);
        }
    }
}
Also used : MojoFailureException(org.apache.maven.plugin.MojoFailureException) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) IOException(java.io.IOException) File(java.io.File)

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