Search in sources :

Example 66 with JavaCompiler

use of javax.tools.JavaCompiler in project neo4j by neo4j.

the class DbStructureInvocationTracingAcceptanceTest method compile.

private <T> T compile(String className, String source, CompilationListener<T> listener) {
    JavaCompiler systemCompiler = ToolProvider.getSystemJavaCompiler();
    JavaFileManager manager = new InMemFileManager();
    DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<JavaFileObject>();
    Iterable<? extends JavaFileObject> sources = Arrays.asList(new InMemSource(className, source));
    CompilationTask task = systemCompiler.getTask(null, manager, diagnosticsCollector, null, null, sources);
    Boolean success = task.call();
    return listener.compiled(success, manager, diagnosticsCollector.getDiagnostics());
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) JavaFileManager(javax.tools.JavaFileManager) ForwardingJavaFileManager(javax.tools.ForwardingJavaFileManager) JavaCompiler(javax.tools.JavaCompiler) DiagnosticCollector(javax.tools.DiagnosticCollector) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 67 with JavaCompiler

use of javax.tools.JavaCompiler in project jadx by skylot.

the class DynamicCompiler method compile.

public boolean compile() throws Exception {
    String fullName = clsNode.getFullName();
    String code = clsNode.getCode().toString();
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    fileManager = new ClassFileManager(compiler.getStandardFileManager(null, null, null));
    List<JavaFileObject> jFiles = new ArrayList<JavaFileObject>(1);
    jFiles.add(new CharSequenceJavaFileObject(fullName, code));
    CompilationTask compilerTask = compiler.getTask(null, fileManager, null, null, null, jFiles);
    return Boolean.TRUE.equals(compilerTask.call());
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 68 with JavaCompiler

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

Example 69 with JavaCompiler

use of javax.tools.JavaCompiler in project intellij-community by JetBrains.

the class JavacCompilerTool method createCompiler.

@NotNull
@Override
public JavaCompiler createCompiler() throws CannotCreateJavaCompilerException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    if (compiler != null) {
        return compiler;
    }
    String message = "System Java Compiler was not found in classpath";
    // trying to obtain additional diagnostic for the case when compiler.jar is present, but there were problems with compiler class loading:
    try {
        Class.forName("com.sun.tools.javac.api.JavacTool", false, JavacMain.class.getClassLoader());
    } catch (Throwable ex) {
        StringWriter stringWriter = new StringWriter();
        stringWriter.write(message);
        stringWriter.write(":\n");
        ex.printStackTrace(new PrintWriter(stringWriter));
        message = stringWriter.getBuffer().toString();
    }
    throw new CannotCreateJavaCompilerException(message);
}
Also used : StringWriter(java.io.StringWriter) JavaCompiler(javax.tools.JavaCompiler) CannotCreateJavaCompilerException(org.jetbrains.jps.builders.java.CannotCreateJavaCompilerException) JavacMain(org.jetbrains.jps.javac.JavacMain) PrintWriter(java.io.PrintWriter) NotNull(org.jetbrains.annotations.NotNull)

Example 70 with JavaCompiler

use of javax.tools.JavaCompiler in project intellij-community by JetBrains.

the class EclipseCompilerTool method getDescription.

@NotNull
@Override
public String getDescription() {
    String version = myVersion;
    if (version == null) {
        version = "";
        //final File file = findEcjJarFile();
        final JavaCompiler compiler = findCompiler();
        final String path = compiler != null ? PathManager.getJarPathForClass(compiler.getClass()) : null;
        final File file = path != null ? new File(path) : null;
        if (file != null) {
            final String name = file.getName();
            if (name.startsWith(JAR_FILE_NAME_PREFIX) && name.endsWith(JAR_FILE_NAME_SUFFIX)) {
                version = " " + name.substring(JAR_FILE_NAME_PREFIX.length(), name.length() - JAR_FILE_NAME_SUFFIX.length());
            }
        }
        myVersion = version;
    }
    return "Eclipse compiler" + version;
}
Also used : JavaCompiler(javax.tools.JavaCompiler) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

JavaCompiler (javax.tools.JavaCompiler)101 StandardJavaFileManager (javax.tools.StandardJavaFileManager)43 JavaFileObject (javax.tools.JavaFileObject)39 File (java.io.File)31 DiagnosticCollector (javax.tools.DiagnosticCollector)24 ArrayList (java.util.ArrayList)22 JavacTask (com.sun.source.util.JavacTask)20 IOException (java.io.IOException)17 CompilationTask (javax.tools.JavaCompiler.CompilationTask)16 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)15 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)11 Diagnostic (javax.tools.Diagnostic)11 PrintWriter (java.io.PrintWriter)9 Test (org.junit.Test)9 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 FileOutputStream (java.io.FileOutputStream)5 StringWriter (java.io.StringWriter)5 JavaFileManager (javax.tools.JavaFileManager)5 OutputStream (java.io.OutputStream)4