Search in sources :

Example 71 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project narchy by automenta.

the class Compiler method compil.

/**
 * Compile a class
 *
 * @param classCompleteName
 *           Class to compile
 * @param code
 *           Code of class
 * @return Directory where lies the compiled files
 * @throws IOException
 *            On read/write issue
 */
public static File compil(final String classCompleteName, final String code) throws IOException {
    final File directory = new File(UtilIO.obtainTemporaryDirectory(), UtilText.concatenate("Compiler", File.separator));
    Debug.println(DebugLevel.INFORMATION, "Compile in ", directory.getAbsolutePath());
    // Create the compiler
    final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    // Create the list of class to compile
    final ArrayList<JavaSourceFromString> compilationUnits = new ArrayList<JavaSourceFromString>();
    compilationUnits.add(new JavaSourceFromString(classCompleteName, code));
    // We create the file manager, and says the directory to use for output
    final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    UtilIO.createDirectory(directory);
    fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(directory));
    // Do the compilation itself
    if (compiler.getTask(null, fileManager, null, null, null, compilationUnits).call() == false) {
        throw new RuntimeException(UtilText.concatenate("Compilation of ", classCompleteName, " failed !"));
    }
    fileManager.flush();
    fileManager.close();
    return directory;
}
Also used : ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) File(java.io.File)

Example 72 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project GIPC by pdewan.

the class MyDynamicCompilation method compileSourceFile.

public static void compileSourceFile(File sourceFile) throws Exception {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File("/temp")));
    // Compile the file
    CompilationTask compilationTask = compiler.getTask(null, fileManager, null, null, null, fileManager.getJavaFileObjectsFromFiles(Arrays.asList(sourceFile)));
    compilationTask.call();
    fileManager.close();
// delete the source file
// sourceFile.deleteOnExit();
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) File(java.io.File) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 73 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project fakereplace by fakereplace.

the class ClassLoaderCompiler method compile.

public void compile() {
    // Compile source file.
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager standard = compiler.getStandardFileManager(null, null, null);
    JavaCompiler.CompilationTask task = compiler.getTask(null, new ClassLoaderJavaFileManager(standard), null, null, null, classBaseNames.stream().map((name) -> new LocalFileObject(base.resolve(name.replace(".", "/") + ".java"), name, Kind.SOURCE)).collect(Collectors.toList()));
    if (!task.call()) {
        throw new RuntimeException("Compile failed");
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager)

Example 74 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project st-js by st-js.

the class CommandLine method compile.

static void compile(final String path, final List<File> sourceFiles, List<File> dependencies) {
    try {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        if (compiler == null) {
            throw new STJSRuntimeException("A Java compiler is not available for this project. " + "You may have configured your environment to run with a JRE instead of a JDK");
        }
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
        fileManager.setLocation(StandardLocation.CLASS_PATH, dependencies);
        Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(sourceFiles);
        compiler.getTask(null, fileManager, null, null, null, compilationUnits1).call();
        fileManager.close();
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : STJSRuntimeException(org.stjs.generator.STJSRuntimeException) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) STJSRuntimeException(org.stjs.generator.STJSRuntimeException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 75 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project Payara by payara.

the class JavaClientGenerator method compileSources.

private void compileSources() {
    try {
        List<File> files = new ArrayList<File>();
        gatherFiles(baseDirectory, files);
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
        List<String> options = new ArrayList<String>();
        options.add("-cp");
        StringBuilder sb = new StringBuilder();
        sb.append(ASClassLoaderUtil.getModuleClassPath(habitat, "", null));
        options.add(sb.toString());
        Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(files);
        if (!compiler.getTask(null, fileManager, null, options, null, compilationUnits).call()) {
            RestLogging.restLogger.log(Level.INFO, RestLogging.COMPILATION_FAILED);
        }
        fileManager.close();
    } catch (IOException ex) {
        RestLogging.restLogger.log(Level.SEVERE, null, ex);
    }
}
Also used : ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) IOException(java.io.IOException) File(java.io.File)

Aggregations

StandardJavaFileManager (javax.tools.StandardJavaFileManager)124 JavaCompiler (javax.tools.JavaCompiler)99 File (java.io.File)60 JavaFileObject (javax.tools.JavaFileObject)50 IOException (java.io.IOException)37 DiagnosticCollector (javax.tools.DiagnosticCollector)36 ArrayList (java.util.ArrayList)33 CompilationTask (javax.tools.JavaCompiler.CompilationTask)25 JavacTask (com.sun.source.util.JavacTask)17 StringWriter (java.io.StringWriter)10 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)9 JavacTool (com.sun.tools.javac.api.JavacTool)8 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)6 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)5 URL (java.net.URL)5 FileOutputStream (java.io.FileOutputStream)4 PrintWriter (java.io.PrintWriter)4 Diagnostic (javax.tools.Diagnostic)4 Charset (java.nio.charset.Charset)3 HashSet (java.util.HashSet)3