use of javax.tools.JavaCompiler.CompilationTask in project javapoet by square.
the class FileReadingTest method compileJavaFile.
@Test
public void compileJavaFile() throws Exception {
final String value = "Hello World!";
TypeSpec type = TypeSpec.classBuilder("Test").addModifiers(Modifier.PUBLIC).addSuperinterface(ParameterizedTypeName.get(Callable.class, String.class)).addMethod(MethodSpec.methodBuilder("call").returns(String.class).addModifiers(Modifier.PUBLIC).addStatement("return $S", value).build()).build();
JavaFile javaFile = JavaFile.builder("foo", type).build();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, Locale.getDefault(), UTF_8);
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(temporaryFolder.newFolder()));
CompilationTask task = compiler.getTask(null, fileManager, diagnosticCollector, Collections.emptySet(), Collections.emptySet(), Collections.singleton(javaFile.toJavaFileObject()));
assertThat(task.call()).isTrue();
assertThat(diagnosticCollector.getDiagnostics()).isEmpty();
ClassLoader loader = fileManager.getClassLoader(StandardLocation.CLASS_OUTPUT);
Callable<?> test = Class.forName("foo.Test", true, loader).asSubclass(Callable.class).getDeclaredConstructor().newInstance();
assertThat(Callable.class.getMethod("call").invoke(test)).isEqualTo(value);
}
use of javax.tools.JavaCompiler.CompilationTask in project serverless by bluenimble.
the class SourceCompiler method compile.
public void compile() throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, Locale.getDefault(), null);
List<JavaFileObject> javaObjects = scanRecursivelyForJavaObjects(sources, fileManager);
if (javaObjects.size() == 0) {
throw new Exception("There are no source files to compile in " + sources.getAbsolutePath());
}
fileManager.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(new File("build-libs").listFiles()));
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(binaries));
CompilationTask compilerTask = compiler.getTask(null, fileManager, diagnostics, new ArrayList<String>(), null, javaObjects);
if (!compilerTask.call()) {
for (Diagnostic<?> diagnostic : diagnostics.getDiagnostics()) {
System.err.format("Error on line %d in %s", diagnostic.getLineNumber(), diagnostic);
}
throw new Exception("Could not compile project");
}
}
use of javax.tools.JavaCompiler.CompilationTask in project yyl_example by Relucent.
the class JavaCompilerTest method compile.
private static boolean compile(String className, String source) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
// DiagnosticListener 编译的诊断信息
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
CompilationTask task = compiler.getTask(null, null, diagnostics, Arrays.asList("-d", tempdir), null, Arrays.asList(new JavaSourceFromString(className, source)));
boolean success = task.call();
System.out.println("Success: " + success);
print(diagnostics);
return success;
}
use of javax.tools.JavaCompiler.CompilationTask in project jadx by skylot.
the class TestCompiler method compile.
private void compile(List<JavaFileObject> jfObjects) {
List<String> arguments = new ArrayList<>();
arguments.add(options.isIncludeDebugInfo() ? "-g" : "-g:none");
int javaVersion = options.getJavaVersion();
String javaVerStr = javaVersion <= 8 ? "1." + javaVersion : Integer.toString(javaVersion);
arguments.add("-source");
arguments.add(javaVerStr);
arguments.add("-target");
arguments.add(javaVerStr);
arguments.addAll(options.getArguments());
DiagnosticListener<? super JavaFileObject> diagnostic = diagObj -> System.out.println("Compiler diagnostic: " + diagObj.getMessage(Locale.ROOT));
Writer out = new PrintWriter(System.out);
CompilationTask compilerTask = compiler.getTask(out, fileManager, diagnostic, arguments, null, jfObjects);
if (Boolean.FALSE.equals(compilerTask.call())) {
throw new RuntimeException("Compilation failed");
}
}
use of javax.tools.JavaCompiler.CompilationTask in project auto by google.
the class GeneratedAnnotationsTest method runProcessor.
/**
* Run {@link TestProcessor} in a compilation with the given {@code options}, and prevent the
* compilation from accessing classes with the qualified names in {@code maskFromClasspath}.
*/
private String runProcessor(ImmutableList<String> options, @Nullable String packageToMask) throws IOException {
File tempDir = temporaryFolder.newFolder();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(/* diagnosticListener= */
null, /* locale= */
null, UTF_8);
standardFileManager.setLocation(StandardLocation.CLASS_OUTPUT, ImmutableList.of(tempDir));
StandardJavaFileManager proxyFileManager = Reflection.newProxy(StandardJavaFileManager.class, new FileManagerInvocationHandler(standardFileManager, packageToMask));
CompilationTask task = compiler.getTask(/* out= */
null, proxyFileManager, /* diagnosticListener= */
null, options, /* classes= */
null, ImmutableList.of(new SimpleJavaFileObject(URI.create("test"), Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return "class Test {}";
}
}));
task.setProcessors(ImmutableList.of(new TestProcessor()));
assertThat(task.call()).isTrue();
return new String(Files.readAllBytes(tempDir.toPath().resolve("G.java")), UTF_8);
}
Aggregations