Search in sources :

Example 96 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon by eclipse.

the class T6963934 method main.

public static void main(String[] args) throws Exception {
    File testSrc = new File(System.getProperty("test.src"));
    File thisSrc = new File(testSrc, T6963934.class.getSimpleName() + ".java");
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    JavacTask task = (JavacTask) compiler.getTask(null, fileManager, null, null, null, fileManager.getJavaFileObjects(thisSrc));
    CompilationUnitTree tree = task.parse().iterator().next();
    int count = 0;
    for (ImportTree importTree : tree.getImports()) {
        System.out.println(importTree);
        count++;
    }
    int expected = 7;
    if (count != expected)
        throw new Exception("unexpected number of imports found: " + count + ", expected: " + expected);
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) File(java.io.File) ImportTree(com.sun.source.tree.ImportTree)

Example 97 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon by eclipse.

the class ToolsTest method compileAndJar.

public static File compileAndJar(String javaModule, String javaClassName, File addToClassPath) throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    String javaModulePath = javaModule.replace('.', '/');
    File moduleFile = new File("test/" + javaModulePath, javaClassName + ".java");
    Iterable<? extends JavaFileObject> units = fileManager.getJavaFileObjects(moduleFile);
    File destDir = new File("build/javaModules");
    FileUtil.delete(destDir);
    destDir.mkdirs();
    List<String> options = new LinkedList<String>();
    options.add("-d");
    options.add(destDir.getPath());
    if (addToClassPath != null) {
        options.add("-cp");
        options.add(addToClassPath.getPath());
    }
    CompilationTask task = compiler.getTask(null, null, null, options, null, units);
    Boolean result = task.call();
    assertTrue(result != null && result.booleanValue());
    File compiledModuleFile = new File(destDir, javaModulePath + "/" + javaClassName + ".class");
    assertTrue(compiledModuleFile.isFile());
    return jar(compiledModuleFile, javaModulePath);
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) File(java.io.File) LinkedList(java.util.LinkedList) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 98 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager 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(getTempTestDir(), 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", getTempDirPath(), "-cp", getTempDirPath() + PATH_SEP + 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;
}
Also used : ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) STGroupString(org.stringtemplate.v4.STGroupString) IOException(java.io.IOException) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)

Example 99 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project flink by apache.

the class TestUserClassLoaderJar method createJarFile.

/**
 * Pack the generated UDF class into a JAR and return the path of the JAR.
 */
public static File createJarFile(File tmpDir, String jarName, String className, String javaCode) throws IOException {
    // write class source code to file
    File javaFile = Paths.get(tmpDir.toString(), className + ".java").toFile();
    // noinspection ResultOfMethodCallIgnored
    javaFile.createNewFile();
    FileUtils.writeFileUtf8(javaFile, javaCode);
    // compile class source code
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
    Iterable<? extends JavaFileObject> compilationUnit = fileManager.getJavaFileObjectsFromFiles(Collections.singletonList(javaFile));
    JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, Collections.emptyList(), null, compilationUnit);
    task.call();
    // pack class file to jar
    File classFile = Paths.get(tmpDir.toString(), className + ".class").toFile();
    File jarFile = Paths.get(tmpDir.toString(), jarName).toFile();
    JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile));
    JarEntry jarEntry = new JarEntry(className + ".class");
    jos.putNextEntry(jarEntry);
    byte[] classBytes = FileUtils.readAllBytes(classFile.toPath());
    jos.write(classBytes);
    jos.closeEntry();
    jos.close();
    return jarFile;
}
Also used : JavaFileObject(javax.tools.JavaFileObject) FileOutputStream(java.io.FileOutputStream) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JarOutputStream(java.util.jar.JarOutputStream) DiagnosticCollector(javax.tools.DiagnosticCollector) JarEntry(java.util.jar.JarEntry) File(java.io.File)

Example 100 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager 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());
}
Also used : Processor(javax.annotation.processing.Processor) ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) IOException(java.io.IOException) CompilationTask(javax.tools.JavaCompiler.CompilationTask) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector)

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