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));
}
}
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;
}
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());
}
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);
}
}
}
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");
}
Aggregations