Search in sources :

Example 41 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.

the class JarFromManifestFailure method compile.

static void compile(File classOutDir, Iterable<File> classPath, File... files) {
    System.err.println("compile...");
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> fileObjects = fm.getJavaFileObjectsFromFiles(Arrays.asList(files));
    List<String> options = new ArrayList<String>();
    if (classOutDir != null) {
        options.add("-d");
        options.add(classOutDir.getPath());
    }
    if (classPath != null) {
        options.add("-classpath");
        options.add(join(classPath, File.pathSeparator));
    }
    options.add("-verbose");
    JavaCompiler.CompilationTask task = compiler.getTask(null, fm, null, options, null, fileObjects);
    if (!task.call())
        throw new AssertionError("compilation failed");
}
Also used : StandardJavaFileManager(javax.tools.StandardJavaFileManager)

Example 42 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.

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 43 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.

the class ParserTest method main.

public static void main(String... args) throws Exception {
    //create default shared JavaCompiler - reused across multiple compilations
    JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
    for (TypeQualifierArity arity : TypeQualifierArity.values()) {
        for (TypeArgumentKind tak1 : TypeArgumentKind.values()) {
            if (arity == TypeQualifierArity.ONE) {
                new ParserTest(arity, tak1).run(comp, fm);
                continue;
            }
            for (TypeArgumentKind tak2 : TypeArgumentKind.values()) {
                if (arity == TypeQualifierArity.TWO) {
                    new ParserTest(arity, tak1, tak2).run(comp, fm);
                    continue;
                }
                for (TypeArgumentKind tak3 : TypeArgumentKind.values()) {
                    if (arity == TypeQualifierArity.THREE) {
                        new ParserTest(arity, tak1, tak2, tak3).run(comp, fm);
                        continue;
                    }
                    for (TypeArgumentKind tak4 : TypeArgumentKind.values()) {
                        new ParserTest(arity, tak1, tak2, tak3, tak4).run(comp, fm);
                    }
                }
            }
        }
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager)

Example 44 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.

the class TestTreePath method run.

public void run() throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> tests = fileManager.getJavaFileObjects(writeTestFile());
    JavaCompiler.CompilationTask task = ToolProvider.getSystemJavaCompiler().getTask(null, null, null, Arrays.asList("-processor", this.getClass().getName()), null, tests);
    task.call();
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager)

Example 45 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.

the class T6956638 method test.

void test(File... sourceFiles) throws Exception {
    System.err.println("Test " + (++count) + ": " + Arrays.asList(sourceFiles));
    File classesDir = new File("classes" + count);
    classesDir.mkdirs();
    StringWriter compilerOutputStream = new StringWriter();
    List<String> compileOptions = Arrays.asList("-d", classesDir.getPath());
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, null, null);
    Iterable<? extends JavaFileObject> sourceFileObjects = fileManager.getJavaFileObjects(sourceFiles);
    System.err.println("1- javac given java source JavaFileObjects " + sourceFileObjects);
    JavaCompiler.CompilationTask task = compiler.getTask(compilerOutputStream, fileManager, null, compileOptions, null, sourceFileObjects);
    JavacTask javacTask = (JavacTask) task;
    Iterable<? extends CompilationUnitTree> parsedTrees = javacTask.parse();
    Iterable<? extends Element> analyzedTrees = javacTask.analyze();
    Iterable<? extends JavaFileObject> generatedFiles = javacTask.generate();
    System.err.println("2- parsed:" + size(parsedTrees) + " analysed:" + size(analyzedTrees) + " generated:" + size(generatedFiles));
    System.err.print("3-");
    for (JavaFileObject f : generatedFiles) System.err.print(" " + f);
    System.err.println("");
    System.err.print("5-");
    for (File f : classesDir.listFiles()) System.err.print(" " + f);
    System.err.println("");
    System.err.println("----");
    System.err.println(compilerOutputStream.toString());
    if (size(generatedFiles) != size(parsedTrees)) {
        throw new Exception("wrong number of files generated: " + size(generatedFiles) + " expected: " + size(parsedTrees));
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) StringWriter(java.io.StringWriter) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) JavacTask(com.sun.source.util.JavacTask) File(java.io.File)

Aggregations

StandardJavaFileManager (javax.tools.StandardJavaFileManager)57 JavaCompiler (javax.tools.JavaCompiler)42 JavaFileObject (javax.tools.JavaFileObject)24 File (java.io.File)23 IOException (java.io.IOException)18 ArrayList (java.util.ArrayList)17 DiagnosticCollector (javax.tools.DiagnosticCollector)15 CompilationTask (javax.tools.JavaCompiler.CompilationTask)9 JavacTask (com.sun.source.util.JavacTask)7 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)4 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)4 JavacTool (com.sun.tools.javac.api.JavacTool)4 FileOutputStream (java.io.FileOutputStream)4 StringWriter (java.io.StringWriter)3 ZipFile (java.util.zip.ZipFile)3 Test (org.junit.Test)3 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)2 PrintWriter (java.io.PrintWriter)2 Method (java.lang.reflect.Method)2 URI (java.net.URI)2