Search in sources :

Example 41 with TreePath

use of com.sun.source.util.TreePath in project ceylon by eclipse.

the class T6598108 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;
    final JavacTask ct = (JavacTask) tool.getTask(null, null, null, Arrays.asList("-bootclasspath", bootPath), null, Arrays.asList(new MyFileObject()));
    CompilationUnitTree cut = ct.parse().iterator().next();
    TreePath tp = new TreePath(new TreePath(cut), cut.getTypeDecls().get(0));
    Scope s = Trees.instance(ct).getScope(tp);
    TypeElement type = ct.getElements().getTypeElement("com.sun.java.util.jar.pack.Package.File");
    if (Trees.instance(ct).isAccessible(s, type)) {
        // "false" would be expected here.
        throw new IllegalStateException("");
    }
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) TreePath(com.sun.source.util.TreePath) Scope(com.sun.source.tree.Scope) TypeElement(javax.lang.model.element.TypeElement) JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Example 42 with TreePath

use of com.sun.source.util.TreePath in project ceylon by eclipse.

the class T6852595 method main.

public static void main(String[] args) throws IOException {
    JavaFileObject sfo = new SimpleJavaFileObject(URI.create("myfo:/Test.java"), Kind.SOURCE) {

        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            return "class BadName { Object o = j; }";
        }
    };
    List<? extends JavaFileObject> files = Arrays.asList(sfo);
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTask ct = (JavacTask) tool.getTask(null, null, null, null, null, files);
    Iterable<? extends CompilationUnitTree> compUnits = ct.parse();
    CompilationUnitTree cu = compUnits.iterator().next();
    ClassTree cdef = (ClassTree) cu.getTypeDecls().get(0);
    JCVariableDecl vdef = (JCVariableDecl) cdef.getMembers().get(0);
    TreePath path = TreePath.getPath(cu, vdef.init);
    Trees.instance(ct).getScope(path);
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) TreePath(com.sun.source.util.TreePath) JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Example 43 with TreePath

use of com.sun.source.util.TreePath in project btrace by btraceio.

the class VerifierVisitor method visitReturn.

@Override
public Boolean visitReturn(ReturnTree node, Void v) {
    if (node.getExpression() != null) {
        TreePath tp = verifier.getTreeUtils().getPath(verifier.getCompilationUnit(), node);
        while (tp != null) {
            tp = tp.getParentPath();
            Tree leaf = tp.getLeaf();
            if (leaf.getKind() == Tree.Kind.METHOD) {
                if (isAnnotated((MethodTree) leaf)) {
                    return reportError("return.type.should.be.void", node);
                } else {
                    return super.visitReturn(node, v);
                }
            }
        }
    }
    return super.visitReturn(node, v);
}
Also used : TreePath(com.sun.source.util.TreePath) JCTree(com.sun.tools.javac.tree.JCTree)

Example 44 with TreePath

use of com.sun.source.util.TreePath in project btrace by btraceio.

the class VerifierVisitor method getElement.

private Element getElement(Tree t) {
    TreePath tp = verifier.getTreeUtils().getPath(verifier.getCompilationUnit(), t);
    Element e = verifier.getTreeUtils().getElement(tp);
    if (e == null) {
        // hack to make JDK 7 symbol resolution working
        if (t instanceof MethodInvocationTree) {
            JCTree.JCExpression jce = ((JCTree.JCMethodInvocation) t).meth;
            if (jce instanceof IdentifierTree) {
                e = ((JCTree.JCIdent) jce).sym;
            } else if (jce instanceof MemberSelectTree) {
                e = ((JCTree.JCFieldAccess) jce).sym;
            }
        } else if (t instanceof JCTree.JCIdent) {
            e = ((JCTree.JCIdent) t).sym;
        } else if (t instanceof JCTree.JCNewClass) {
            e = ((JCTree.JCIdent) ((JCTree.JCNewClass) t).clazz).sym;
        } else if (t instanceof JCTree.JCThrow) {
            e = ((JCTree.JCNewClass) ((JCTree.JCThrow) t).expr).type.tsym;
        }
    }
    return e;
}
Also used : VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) JCTree(com.sun.tools.javac.tree.JCTree) TreePath(com.sun.source.util.TreePath)

Example 45 with TreePath

use of com.sun.source.util.TreePath in project btrace by btraceio.

the class VerifierVisitor method checkSampling.

private boolean checkSampling(MethodTree node) {
    TreePath mPath = verifier.getTreeUtils().getPath(verifier.getCompilationUnit(), node);
    ExecutableElement ee = (ExecutableElement) verifier.getTreeUtils().getElement(mPath);
    Sampled s = ee.getAnnotation(Sampled.class);
    OnMethod om = ee.getAnnotation(OnMethod.class);
    if (s != null && om != null) {
        Kind k = om.location().value();
        switch(k) {
            case ENTRY:
            case RETURN:
            case ERROR:
            case CALL:
                {
                    return true;
                }
            default:
                {
                // noop
                }
        }
        reportError("sampler.invalid.location", node);
        return false;
    }
    return true;
}
Also used : TreePath(com.sun.source.util.TreePath) Sampled(com.sun.btrace.annotations.Sampled) OnMethod(com.sun.btrace.annotations.OnMethod) ExecutableElement(javax.lang.model.element.ExecutableElement) Kind(com.sun.btrace.annotations.Kind) ElementKind(javax.lang.model.element.ElementKind)

Aggregations

TreePath (com.sun.source.util.TreePath)151 ExpressionTree (com.sun.source.tree.ExpressionTree)60 Tree (com.sun.source.tree.Tree)60 VariableTree (com.sun.source.tree.VariableTree)50 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)46 MethodTree (com.sun.source.tree.MethodTree)46 ClassTree (com.sun.source.tree.ClassTree)45 MemberSelectTree (com.sun.source.tree.MemberSelectTree)39 IdentifierTree (com.sun.source.tree.IdentifierTree)37 BlockTree (com.sun.source.tree.BlockTree)36 NewClassTree (com.sun.source.tree.NewClassTree)32 StatementTree (com.sun.source.tree.StatementTree)32 JCTree (com.sun.tools.javac.tree.JCTree)31 AssignmentTree (com.sun.source.tree.AssignmentTree)27 BinaryTree (com.sun.source.tree.BinaryTree)27 TypeCastTree (com.sun.source.tree.TypeCastTree)26 ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)25 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)25 LiteralTree (com.sun.source.tree.LiteralTree)25 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)23