Search in sources :

Example 61 with ClassTree

use of com.sun.source.tree.ClassTree in project error-prone by google.

the class PrivateConstructorForUtilityClass method isInPrivateScope.

private static boolean isInPrivateScope(VisitorState state) {
    TreePath treePath = state.getPath();
    do {
        Tree currentLeaf = treePath.getLeaf();
        if (ClassTree.class.isInstance(currentLeaf)) {
            ClassTree currentClassTree = (ClassTree) currentLeaf;
            if (currentClassTree.getModifiers().getFlags().contains(PRIVATE)) {
                return true;
            }
        }
        treePath = treePath.getParentPath();
    } while (treePath != null);
    return false;
}
Also used : TreePath(com.sun.source.util.TreePath) ClassTree(com.sun.source.tree.ClassTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) BlockTree(com.sun.source.tree.BlockTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree)

Example 62 with ClassTree

use of com.sun.source.tree.ClassTree in project error-prone by google.

the class IterableAndIterator method matchAnySuperType.

private boolean matchAnySuperType(ClassTree tree, VisitorState state) {
    List<Tree> superTypes = Lists.<Tree>newArrayList(tree.getImplementsClause());
    Tree superClass = tree.getExtendsClause();
    if (superClass != null) {
        superTypes.add(superClass);
    }
    /* NOTE: at "Eight day", use Java 8 feature below
    return superTypes.stream()
        .anyMatch(superType -> ITERABLE_AND_ITERATOR_MATCHER.matches(superType, state));
     */
    for (Tree superType : superTypes) {
        if (ITERABLE_AND_ITERATOR_MATCHER.matches(superType, state)) {
            return true;
        }
    }
    return false;
}
Also used : Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree)

Example 63 with ClassTree

use of com.sun.source.tree.ClassTree in project checker-framework by typetools.

the class AnnotatedTypeFactory method getSelfType.

/**
 * Returns the type of {@code this} in the current location, which can be used if {@code this}
 * has a special semantics (e.g. {@code this} is non-null).
 *
 * <p>The parameter is an arbitrary tree and does not have to mention "this", neither explicitly
 * nor implicitly. This method should be overridden for type-system specific behavior.
 *
 * <p>TODO: in 1.8.2, handle all receiver type annotations. TODO: handle enclosing classes
 * correctly.
 */
public AnnotatedDeclaredType getSelfType(Tree tree) {
    TreePath path = getPath(tree);
    ClassTree enclosingClass = TreeUtils.enclosingClass(path);
    if (enclosingClass == null) {
        // I hope this only happens when tree is a fake tree that
        // we created, e.g. when desugaring enhanced-for-loops.
        enclosingClass = getCurrentClassTree(tree);
    }
    AnnotatedDeclaredType type = getAnnotatedType(enclosingClass);
    MethodTree enclosingMethod = TreeUtils.enclosingMethod(path);
    if (enclosingClass.getSimpleName().length() != 0 && enclosingMethod != null) {
        AnnotatedDeclaredType methodReceiver;
        if (TreeUtils.isConstructor(enclosingMethod)) {
            methodReceiver = (AnnotatedDeclaredType) getAnnotatedType(enclosingMethod).getReturnType();
        } else {
            methodReceiver = getAnnotatedType(enclosingMethod).getReceiverType();
        }
        if (shouldTakeFromReceiver(methodReceiver)) {
            // TODO  what about all annotations on the receiver?
            // Code is also duplicated above.
            type.clearAnnotations();
            type.addAnnotations(methodReceiver.getAnnotations());
        }
    }
    return type;
}
Also used : TreePath(com.sun.source.util.TreePath) MethodTree(com.sun.source.tree.MethodTree) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree)

Example 64 with ClassTree

use of com.sun.source.tree.ClassTree in project checker-framework by typetools.

the class AnnotatedTypeFactory method isMostEnclosingThisDeref.

/**
 * Determine whether the tree dereferences the most enclosing "this" object. That is, we have an
 * expression like "f.g" and want to know whether it is an access "this.f.g" or whether e.g. f
 * is a field of an outer class or e.g. f is a local variable.
 *
 * @param tree the tree to check
 * @return true, iff the tree is an explicit or implicit reference to the most enclosing "this"
 */
public final boolean isMostEnclosingThisDeref(ExpressionTree tree) {
    if (!isAnyEnclosingThisDeref(tree)) {
        return false;
    }
    Element element = TreeUtils.elementFromUse(tree);
    TypeElement typeElt = ElementUtils.enclosingClass(element);
    ClassTree enclosingClass = getCurrentClassTree(tree);
    if (enclosingClass != null && isSubtype(TreeUtils.elementFromDeclaration(enclosingClass), typeElt)) {
        return true;
    }
    // ran out of options
    return false;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree)

Example 65 with ClassTree

use of com.sun.source.tree.ClassTree in project checker-framework by typetools.

the class InitializationTransfer method initializedFieldsAfterCall.

/**
 * Returns the fields that can safely be considered initialized after the method call {@code
 * node}.
 */
protected List<VariableElement> initializedFieldsAfterCall(MethodInvocationNode node, ConditionalTransferResult<V, S> transferResult) {
    List<VariableElement> result = new ArrayList<>();
    MethodInvocationTree tree = node.getTree();
    ExecutableElement method = TreeUtils.elementFromUse(tree);
    boolean isConstructor = method.getSimpleName().contentEquals("<init>");
    Node receiver = node.getTarget().getReceiver();
    String methodString = tree.getMethodSelect().toString();
    // invariant fields are guaranteed to be initialized.
    if (isConstructor && receiver instanceof ThisLiteralNode && methodString.equals("this")) {
        ClassTree clazz = TreeUtils.enclosingClass(analysis.getTypeFactory().getPath(tree));
        TypeElement clazzElem = TreeUtils.elementFromDeclaration(clazz);
        markInvariantFieldsAsInitialized(result, clazzElem);
    }
    // invariant fields of any super class are guaranteed to be initialized.
    if (isConstructor && receiver instanceof ThisLiteralNode && methodString.equals("super")) {
        ClassTree clazz = TreeUtils.enclosingClass(analysis.getTypeFactory().getPath(tree));
        TypeElement clazzElem = TreeUtils.elementFromDeclaration(clazz);
        TypeMirror superClass = clazzElem.getSuperclass();
        while (superClass != null && superClass.getKind() != TypeKind.NONE) {
            clazzElem = (TypeElement) analysis.getTypes().asElement(superClass);
            superClass = clazzElem.getSuperclass();
            markInvariantFieldsAsInitialized(result, clazzElem);
        }
    }
    return result;
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ThisLiteralNode(org.checkerframework.dataflow.cfg.node.ThisLiteralNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) Node(org.checkerframework.dataflow.cfg.node.Node) ThisLiteralNode(org.checkerframework.dataflow.cfg.node.ThisLiteralNode) ArrayList(java.util.ArrayList) ClassTree(com.sun.source.tree.ClassTree) VariableElement(javax.lang.model.element.VariableElement)

Aggregations

ClassTree (com.sun.source.tree.ClassTree)119 Tree (com.sun.source.tree.Tree)76 MethodTree (com.sun.source.tree.MethodTree)66 VariableTree (com.sun.source.tree.VariableTree)59 NewClassTree (com.sun.source.tree.NewClassTree)48 ExpressionTree (com.sun.source.tree.ExpressionTree)45 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)40 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)31 AnnotationTree (com.sun.source.tree.AnnotationTree)29 BlockTree (com.sun.source.tree.BlockTree)28 IdentifierTree (com.sun.source.tree.IdentifierTree)27 NewArrayTree (com.sun.source.tree.NewArrayTree)26 AssignmentTree (com.sun.source.tree.AssignmentTree)25 MemberSelectTree (com.sun.source.tree.MemberSelectTree)25 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)24 TypeElement (javax.lang.model.element.TypeElement)24 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)23 ArrayList (java.util.ArrayList)23 ReturnTree (com.sun.source.tree.ReturnTree)22 TreePath (com.sun.source.util.TreePath)22