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