Search in sources :

Example 91 with JavaCompiler

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

the class T6665791 method main.

public static void main(String[] args) throws Exception {
    write(test_java, test);
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager manager = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> units = manager.getJavaFileObjects(test_java);
    final StringWriter sw = new StringWriter();
    JavacTask task = (JavacTask) compiler.getTask(sw, manager, null, null, null, units);
    new TreeScanner<Boolean, Void>() {

        @Override
        public Boolean visitClass(ClassTree arg0, Void arg1) {
            sw.write(arg0.toString());
            return super.visitClass(arg0, arg1);
        }
    }.scan(task.parse(), null);
    System.out.println("output:");
    System.out.println(sw.toString());
    String found = sw.toString().replaceAll("\\s+", " ").trim();
    String expect = test.replaceAll("\\s+", " ").trim();
    if (!expect.equals(found)) {
        System.out.println("expect: " + expect);
        System.out.println("found:  " + found);
        throw new Exception("unexpected output");
    }
}
Also used : StringWriter(java.io.StringWriter) ClassTree(com.sun.source.tree.ClassTree) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) IOException(java.io.IOException)

Example 92 with JavaCompiler

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

the class InterruptedExceptionTest 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 (XlintOption xlint : XlintOption.values()) {
        for (SuppressLevel suppress_decl : SuppressLevel.values()) {
            for (SuppressLevel suppress_use : SuppressLevel.values()) {
                for (ClassKind ck : ClassKind.values()) {
                    for (ExceptionKind ek_decl : ExceptionKind.values()) {
                        for (ExceptionKind ek_use : ExceptionKind.values()) {
                            new InterruptedExceptionTest(xlint, suppress_decl, suppress_use, ck, ek_decl, ek_use).run(comp, fm);
                        }
                    }
                }
            }
        }
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager)

Example 93 with JavaCompiler

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

the class UnusedResourcesTest method test.

static void test(XlintOption xlint, SuppressLevel suppressLevel, ResourceUsage usage1, ResourceUsage usage2, ResourceUsage usage3) throws Exception {
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavaSource source = new JavaSource(suppressLevel, usage1, usage2, usage3);
    DiagnosticChecker dc = new DiagnosticChecker();
    JavacTask ct = (JavacTask) tool.getTask(null, fm, dc, Arrays.asList(xlint.getXlintOption()), null, Arrays.asList(source));
    ct.analyze();
    check(source, xlint, suppressLevel, usage1, usage2, usage3, dc);
}
Also used : JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Example 94 with JavaCompiler

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

the class T6402077 method main.

public static void main(String... args) throws IOException {
    class MyFileObject extends SimpleJavaFileObject {

        MyFileObject() {
            super(URI.create("myfo:///Test.java"), SOURCE);
        }

        @Override
        public String getCharContent(boolean ignoreEncodingErrors) {
            //      0123456789012345678901234
            return "class Test { Test() { } }";
        }
    }
    JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
    List<JavaFileObject> compilationUnits = Collections.<JavaFileObject>singletonList(new MyFileObject());
    JavacTask task = (JavacTask) javac.getTask(null, null, null, null, null, compilationUnits);
    Trees trees = Trees.instance(task);
    CompilationUnitTree toplevel = task.parse().iterator().next();
    Tree tree = ((ClassTree) toplevel.getTypeDecls().get(0)).getMembers().get(0);
    long pos = trees.getSourcePositions().getStartPosition(toplevel, tree);
    if (pos != 13)
        throw new AssertionError(String.format("Start pos for %s is incorrect (%s)!", tree, pos));
    pos = trees.getSourcePositions().getEndPosition(toplevel, tree);
    if (pos != 23)
        throw new AssertionError(String.format("End pos for %s is incorrect (%s)!", tree, pos));
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) Trees(com.sun.source.util.Trees) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavaCompiler(javax.tools.JavaCompiler) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) JavacTask(com.sun.source.util.JavacTask)

Example 95 with JavaCompiler

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

the class DisjunctiveTypeWellFormednessTest 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 (Arity arity : Arity.values()) {
        for (Alternative a1 : Alternative.values()) {
            if (arity == Arity.ONE) {
                new DisjunctiveTypeWellFormednessTest(a1).run(comp, fm);
                continue;
            }
            for (Alternative a2 : Alternative.values()) {
                if (arity == Arity.TWO) {
                    new DisjunctiveTypeWellFormednessTest(a1, a2).run(comp, fm);
                    continue;
                }
                for (Alternative a3 : Alternative.values()) {
                    if (arity == Arity.THREE) {
                        new DisjunctiveTypeWellFormednessTest(a1, a2, a3).run(comp, fm);
                        continue;
                    }
                    for (Alternative a4 : Alternative.values()) {
                        if (arity == Arity.FOUR) {
                            new DisjunctiveTypeWellFormednessTest(a1, a2, a3, a4).run(comp, fm);
                            continue;
                        }
                        for (Alternative a5 : Alternative.values()) {
                            new DisjunctiveTypeWellFormednessTest(a1, a2, a3, a4, a5).run(comp, fm);
                        }
                    }
                }
            }
        }
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager)

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