Search in sources :

Example 16 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class T6404194 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) {
            //      01234567890123456 7890 123456789012345
            return "@SuppressWarning(\"foo\") @Deprecated 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();
    ClassTree classTree = (ClassTree) toplevel.getTypeDecls().get(0);
    List<? extends Tree> annotations = classTree.getModifiers().getAnnotations();
    Tree tree1 = annotations.get(0);
    Tree tree2 = annotations.get(1);
    long pos = trees.getSourcePositions().getStartPosition(toplevel, tree1);
    if (pos != 0)
        throw new AssertionError(String.format("Start pos for %s is incorrect (%s)!", tree1, pos));
    pos = trees.getSourcePositions().getEndPosition(toplevel, tree1);
    if (pos != 23)
        throw new AssertionError(String.format("End pos for %s is incorrect (%s)!", tree1, pos));
    pos = trees.getSourcePositions().getStartPosition(toplevel, tree2);
    if (pos != 24)
        throw new AssertionError(String.format("Start pos for %s is incorrect (%s)!", tree2, pos));
    pos = trees.getSourcePositions().getEndPosition(toplevel, tree2);
    if (pos != 35)
        throw new AssertionError(String.format("End pos for %s is incorrect (%s)!", tree2, pos));
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) Trees(com.sun.source.util.Trees) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) ClassTree(com.sun.source.tree.ClassTree) JavaCompiler(javax.tools.JavaCompiler) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) 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 17 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class TestSuppression method test.

void test(String src, WarningKind wk, int gen) throws Exception {
    count++;
    System.err.println("Test " + count + ": wk:" + wk + " gen:" + gen + " src:" + src);
    File testDir = new File("test" + count);
    File srcDir = createDir(testDir, "src");
    File gensrcDir = createDir(testDir, "gensrc");
    File classesDir = createDir(testDir, "classes");
    File x = writeFile(new File(srcDir, "X.java"), src);
    DiagListener dl = new DiagListener();
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);
    fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(classesDir, new File(System.getProperty("test.classes"))));
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(classesDir));
    fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(gensrcDir));
    List<String> args = new ArrayList<String>();
    //        args.add("-XprintProcessorInfo");
    args.add("-XprintRounds");
    args.add("-Agen=" + gen);
    if (wk == WarningKind.YES)
        args.add("-Xlint:serial");
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(x);
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    JavacTask task = tool.getTask(pw, fm, dl, args, null, files);
    task.setProcessors(Arrays.asList(new AnnoProc()));
    boolean ok = task.call();
    pw.close();
    System.err.println("ok:" + ok + " diags:" + dl.counts);
    if (sw.toString().length() > 0) {
        System.err.println("output:\n" + sw.toString());
    }
    for (Diagnostic.Kind dk : Diagnostic.Kind.values()) {
        Integer v = dl.counts.get(dk);
        int found = (v == null) ? 0 : v;
        int expect = (dk == Diagnostic.Kind.WARNING && wk == WarningKind.YES) ? gen : 0;
        if (found != expect) {
            error("Unexpected value for " + dk + ": expected: " + expect + " found: " + found);
        }
    }
    System.err.println();
}
Also used : JavacTool(com.sun.tools.javac.api.JavacTool) JCDiagnostic(com.sun.tools.javac.util.JCDiagnostic) JavacTask(com.sun.source.util.JavacTask)

Example 18 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class ImplementationCacheTest method main.

public static void main(String[] args) throws IOException {
    List<? extends JavaFileObject> files = Arrays.asList(new SourceFile());
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTask ct = (JavacTask) tool.getTask(null, null, null, null, null, files);
    Context ctx = new Context();
    JavacFileManager.preRegister(ctx);
    checkImplementationCache(ct.analyze(), Types.instance(ctx));
}
Also used : Context(com.sun.tools.javac.util.Context) JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Example 19 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class CeylonDocToolTests method compileJavaModule.

private void compileJavaModule(String pathname, String... fileNames) throws Exception {
    CeyloncTool compiler = new CeyloncTool();
    List<String> options = Arrays.asList("-src", pathname, "-out", "build/ceylon-cars", "-cp", CompilerTests.getClassPathAsPath());
    JavacFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    List<String> qualifiedNames = new ArrayList<String>(fileNames.length);
    for (String name : fileNames) {
        qualifiedNames.add(pathname + File.separator + name);
    }
    Iterable<? extends JavaFileObject> fileObjects = fileManager.getJavaFileObjectsFromStrings(qualifiedNames);
    JavacTask task = compiler.getTask(null, null, null, options, null, fileObjects);
    Boolean ret = task.call();
    Assert.assertEquals("Compilation failed", Boolean.TRUE, ret);
}
Also used : JavacFileManager(com.sun.tools.javac.file.JavacFileManager) CeyloncTool(com.redhat.ceylon.compiler.java.tools.CeyloncTool) ArrayList(java.util.ArrayList) JavacTask(com.sun.source.util.JavacTask)

Example 20 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class CeylonDocToolTests method compile.

private void compile(String pathname, String destDir, String moduleName) throws Exception {
    CeyloncTool compiler = new CeyloncTool();
    List<String> options = Arrays.asList("-src", pathname, "-out", destDir, "-cp", CompilerTests.getClassPathAsPath());
    JavacTask task = compiler.getTask(null, null, null, options, Arrays.asList(moduleName), null);
    Boolean ret = task.call();
    Assert.assertEquals("Compilation failed", Boolean.TRUE, ret);
}
Also used : CeyloncTool(com.redhat.ceylon.compiler.java.tools.CeyloncTool) JavacTask(com.sun.source.util.JavacTask)

Aggregations

JavacTask (com.sun.source.util.JavacTask)46 JavaCompiler (javax.tools.JavaCompiler)20 JavacTool (com.sun.tools.javac.api.JavacTool)12 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)10 JavaFileObject (javax.tools.JavaFileObject)9 StandardJavaFileManager (javax.tools.StandardJavaFileManager)7 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)6 Context (com.sun.tools.javac.util.Context)5 File (java.io.File)5 IOException (java.io.IOException)5 PrintWriter (java.io.PrintWriter)5 StringWriter (java.io.StringWriter)5 JavacFileManager (com.sun.tools.javac.file.JavacFileManager)4 ClassTree (com.sun.source.tree.ClassTree)3 Trees (com.sun.source.util.Trees)3 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)3 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)3 CeyloncTool (com.redhat.ceylon.compiler.java.tools.CeyloncTool)2 Tree (com.sun.source.tree.Tree)2