Search in sources :

Example 46 with CompilationUnitTree

use of com.sun.source.tree.CompilationUnitTree in project ceylon by eclipse.

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)

Example 47 with CompilationUnitTree

use of com.sun.source.tree.CompilationUnitTree in project ceylon by eclipse.

the class GenStubs method run.

boolean run(String sourcepath, File outdir, List<String> classes) {
    // System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
    if (sourcepath == null)
        throw new IllegalArgumentException("sourcepath not set");
    if (outdir == null)
        throw new IllegalArgumentException("source output dir not set");
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    try {
        fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
        fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
        List<JavaFileObject> files = new ArrayList<JavaFileObject>();
        for (String c : classes) {
            JavaFileObject fo = fm.getJavaFileForInput(StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
            if (fo == null)
                error("class not found: " + c);
            else
                files.add(fo);
        }
        JavacTask t = tool.getTask(null, fm, null, null, null, files);
        Iterable<? extends CompilationUnitTree> trees = t.parse();
        for (CompilationUnitTree tree : trees) {
            makeStub(fm, tree);
        }
    } catch (IOException e) {
        error("IO error " + e, e);
    }
    return (errors == 0);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavacTool(com.sun.tools.javac.api.JavacTool) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask)

Example 48 with CompilationUnitTree

use of com.sun.source.tree.CompilationUnitTree 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 49 with CompilationUnitTree

use of com.sun.source.tree.CompilationUnitTree in project btrace by btraceio.

the class Verifier method verify.

// verify each BTrace class
private boolean verify(ClassTree ct, Element topElement) {
    currentClass = ct;
    CompilationUnitTree cut = getCompilationUnit();
    String className = ct.getSimpleName().toString();
    ExpressionTree pkgName = cut.getPackageName();
    if (pkgName != null) {
        className = pkgName + "." + className;
    }
    classNames.add(className);
    if (hasTrustedAnnotation(ct, topElement)) {
        return true;
    }
    Boolean value = ct.accept(new VerifierVisitor(this, topElement), null);
    return value == null ? true : value;
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 50 with CompilationUnitTree

use of com.sun.source.tree.CompilationUnitTree in project ceylon-compiler by ceylon.

the class T6345974 method main.

public static void main(String[] args) throws Exception {
    PrintWriter out = new PrintWriter(System.out, true);
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    File testSrc = new File(System.getProperty("test.src"));
    Iterable<? extends JavaFileObject> f = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "T6345974.java")));
    JavacTask task = tool.getTask(out, fm, null, null, null, f);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    out.flush();
    Scanner s = new Scanner();
    for (CompilationUnitTree t : trees) s.scan(t, null);
}
Also used : TreeScanner(com.sun.source.util.TreeScanner) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavacTool(com.sun.tools.javac.api.JavacTool) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) File(java.io.File) PrintWriter(java.io.PrintWriter)

Aggregations

CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)56 JavacTask (com.sun.source.util.JavacTask)24 JavaCompiler (javax.tools.JavaCompiler)21 JavaFileObject (javax.tools.JavaFileObject)16 Tree (com.sun.source.tree.Tree)15 ClassTree (com.sun.source.tree.ClassTree)14 VariableTree (com.sun.source.tree.VariableTree)10 JavacTool (com.sun.tools.javac.api.JavacTool)9 JCTree (com.sun.tools.javac.tree.JCTree)9 IOException (java.io.IOException)9 MethodTree (com.sun.source.tree.MethodTree)8 ArrayList (java.util.ArrayList)8 Trees (com.sun.source.util.Trees)7 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)6 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)6 StandardJavaFileManager (javax.tools.StandardJavaFileManager)6 BinaryTree (com.sun.source.tree.BinaryTree)5 ExpressionTree (com.sun.source.tree.ExpressionTree)5 IdentifierTree (com.sun.source.tree.IdentifierTree)5 TreePath (com.sun.source.util.TreePath)5