Search in sources :

Example 21 with CompilationTask

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);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) Callable(java.util.concurrent.Callable) CompilationTask(javax.tools.JavaCompiler.CompilationTask) Test(org.junit.Test)

Example 22 with CompilationTask

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");
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) File(java.io.File) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 23 with CompilationTask

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;
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) JavaCompiler(javax.tools.JavaCompiler) DiagnosticCollector(javax.tools.DiagnosticCollector) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 24 with CompilationTask

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");
    }
}
Also used : PrintWriter(java.io.PrintWriter) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) Files(java.nio.file.Files) JavaCompiler(javax.tools.JavaCompiler) IOException(java.io.IOException) File(java.io.File) ArrayList(java.util.ArrayList) JavaFileObject(javax.tools.JavaFileObject) List(java.util.List) ClassNode(jadx.core.dex.nodes.ClassNode) CompilationTask(javax.tools.JavaCompiler.CompilationTask) IntegrationTest(jadx.tests.api.IntegrationTest) Locale(java.util.Locale) Closeable(java.io.Closeable) Writer(java.io.Writer) DiagnosticListener(javax.tools.DiagnosticListener) NotNull(org.jetbrains.annotations.NotNull) Method(java.lang.reflect.Method) Path(java.nio.file.Path) FileUtils(jadx.core.utils.files.FileUtils) ToolProvider(javax.tools.ToolProvider) ArrayList(java.util.ArrayList) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) CompilationTask(javax.tools.JavaCompiler.CompilationTask) PrintWriter(java.io.PrintWriter)

Example 25 with CompilationTask

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);
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) File(java.io.File) JavaFile(com.squareup.javapoet.JavaFile) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Aggregations

CompilationTask (javax.tools.JavaCompiler.CompilationTask)84 JavaCompiler (javax.tools.JavaCompiler)38 File (java.io.File)33 JavaFileObject (javax.tools.JavaFileObject)32 StandardJavaFileManager (javax.tools.StandardJavaFileManager)28 DiagnosticCollector (javax.tools.DiagnosticCollector)27 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)18 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)14 URLClassLoader (java.net.URLClassLoader)10 StringWriter (java.io.StringWriter)9 URL (java.net.URL)6 JavaFileManager (javax.tools.JavaFileManager)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Test (org.junit.Test)5 OutputStream (java.io.OutputStream)4 PrintWriter (java.io.PrintWriter)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3