Search in sources :

Example 86 with JavaCompiler

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

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

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

Example 89 with JavaCompiler

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

the class T6993301 method testExceptionParameterCorrectKind.

public void testExceptionParameterCorrectKind() throws IOException {
    final String bootPath = System.getProperty("sun.boot.class.path");
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    assert tool != null;
    String code = "package test; public class Test { { try { } catch (NullPointerException ex) {} } }";
    final JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, Arrays.asList("-bootclasspath", bootPath), null, Arrays.asList(new MyFileObject(code)));
    CompilationUnitTree cut = ct.parse().iterator().next();
    ct.analyze();
    new TreePathScanner<Void, Void>() {

        @Override
        public Void visitVariable(VariableTree node, Void p) {
            Element el = Trees.instance(ct).getElement(getCurrentPath());
            assertNotNull(el);
            assertEquals(ElementKind.EXCEPTION_PARAMETER, el.getKind());
            return super.visitVariable(node, p);
        }
    }.scan(cut, null);
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) Element(javax.lang.model.element.Element) VariableTree(com.sun.source.tree.VariableTree) JavaCompiler(javax.tools.JavaCompiler)

Example 90 with JavaCompiler

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

the class T6654037 method main.

public static void main(String[] args) throws Exception {
    //NOI18N
    final String bootPath = System.getProperty("sun.boot.class.path");
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    assert tool != null;
    String code = "package test; public class Test {private void test() {Object o = null; boolean b = o != null && o instanceof String;} private Test() {}}";
    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, Arrays.asList("-bootclasspath", bootPath, "-Xjcov"), null, Arrays.asList(new MyFileObject(code)));
    CompilationUnitTree cut = ct.parse().iterator().next();
    ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
    MethodTree method = (MethodTree) clazz.getMembers().get(0);
    VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1);
    BinaryTree cond = (BinaryTree) condSt.getInitializer();
    JCTree condJC = (JCTree) cond;
    if (condJC.pos != 93)
        throw new IllegalStateException("Unexpected position=" + condJC.pos);
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) MethodTree(com.sun.source.tree.MethodTree) ClassTree(com.sun.source.tree.ClassTree) VariableTree(com.sun.source.tree.VariableTree) BinaryTree(com.sun.source.tree.BinaryTree) JavaCompiler(javax.tools.JavaCompiler) JCTree(com.sun.tools.javac.tree.JCTree)

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