Search in sources :

Example 31 with StandardJavaFileManager

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

the class Compiler method compile.

public void compile(JavaCompiler javaCompiler, Writer out, File sourceDirectory, File outputDirectory, List<File> classpath, DiagnosticListener<? super JavaFileObject> diagnosticListener, String targetVersion) {
    targetVersion = targetVersion == null ? "1.6" : targetVersion;
    StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(null, null, null);
    if (outputDirectory != null) {
        try {
            fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(outputDirectory));
            fileManager.setLocation(StandardLocation.CLASS_PATH, classpath);
        } catch (IOException e) {
            throw new RuntimeException("could not set output directory", e);
        }
    }
    Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(findAllSourceFiles(sourceDirectory));
    ArrayList<String> options = new ArrayList<String>();
    options.add("-source");
    options.add(targetVersion);
    options.add("-target");
    options.add(targetVersion);
    options.add("-encoding");
    options.add("UTF8");
    options.add("-Xlint:-options");
    options.add("-Xlint:unchecked");
    if (compilationUnits.iterator().hasNext()) {
        Boolean success = javaCompiler.getTask(out, fileManager, diagnosticListener, options, null, compilationUnits).call();
        assertThat("Compilation was not successful, check stdout for errors", success, is(true));
    }
}
Also used : ArrayList(java.util.ArrayList) StandardJavaFileManager(javax.tools.StandardJavaFileManager) IOException(java.io.IOException)

Example 32 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager 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 33 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager 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 34 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager 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)

Example 35 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project DistributedFractalNetwork by Budder21.

the class DynamicCompiler method compile.

/** compile your files by JavaCompiler. If the class was succesfuly compiled, it will print 'Succeded'. Otherwise, it
	 * will print diagnositc information. 
	 * @param files takes in an Arrays.asList of the JavaFileObject
	 * @throws NullPointerException this is thrown if the java file is run from a JRE instead of a JDK.
	 */
public static void compile(Iterable<? extends JavaFileObject> files) throws NullPointerException {
    // get system compiler:
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    // for compilation diagnostic message processing on compilation
    // WARNING/ERROR
    MyDiagnosticListener c = new MyDiagnosticListener();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(c, Locale.ENGLISH, null);
    // specify classes output folder
    Iterable options = Arrays.asList("-d", classOutputFolder);
    JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, c, options, null, files);
    Boolean result = task.call();
    if (result == true) {
        System.out.println("Succeeded");
    } else
        System.out.println("Failed");
}
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