Search in sources :

Example 31 with IdentifierTree

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

the class QualifierDefaults method applyDefaults.

/**
 * Applies default annotations to a type. A {@link com.sun.source.tree.Tree} determines the
 * appropriate scope for defaults.
 *
 * <p>For instance, if the tree is associated with a declaration (e.g., it's the use of a field,
 * or a method invocation), defaults in the scope of the <i>declaration</i> are used; if the tree
 * is not associated with a declaration (e.g., a typecast), defaults in the scope of the tree are
 * used.
 *
 * @param tree the tree associated with the type
 * @param type the type to which defaults will be applied
 * @see #applyDefaultsElement(javax.lang.model.element.Element,
 *     org.checkerframework.framework.type.AnnotatedTypeMirror)
 */
private void applyDefaults(Tree tree, AnnotatedTypeMirror type) {
    // The location to take defaults from.
    Element elt;
    switch(tree.getKind()) {
        case MEMBER_SELECT:
            elt = TreeUtils.elementFromUse((MemberSelectTree) tree);
            break;
        case IDENTIFIER:
            elt = TreeUtils.elementFromUse((IdentifierTree) tree);
            if (ElementUtils.isTypeDeclaration(elt)) {
                // If the Idenitifer is a type, then use the scope of the tree.
                elt = nearestEnclosingExceptLocal(tree);
            }
            break;
        case METHOD_INVOCATION:
            elt = TreeUtils.elementFromUse((MethodInvocationTree) tree);
            break;
        default:
            // If no associated symbol was found, use the tree's (lexical) scope.
            elt = nearestEnclosingExceptLocal(tree);
    }
    // System.out.println("applyDefaults on tree " + tree +
    // " gives elt: " + elt + "(" + elt.getKind() + ")");
    boolean defaultTypeVarLocals = (atypeFactory instanceof GenericAnnotatedTypeFactory<?, ?, ?, ?>) && ((GenericAnnotatedTypeFactory<?, ?, ?, ?>) atypeFactory).getShouldDefaultTypeVarLocals();
    applyToTypeVar = defaultTypeVarLocals && elt != null && elt.getKind() == ElementKind.LOCAL_VARIABLE && type.getKind() == TypeKind.TYPEVAR;
    applyDefaultsElement(elt, type);
    applyToTypeVar = false;
}
Also used : GenericAnnotatedTypeFactory(org.checkerframework.framework.type.GenericAnnotatedTypeFactory) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) MemberSelectTree(com.sun.source.tree.MemberSelectTree) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 32 with IdentifierTree

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

the class JointJavacJavaParserVisitor method isDefaultSuperConstructorCall.

/**
 * Returns whether a javac statement represents a method call {@code super()}.
 *
 * @param statement the javac statement to check
 * @return true if statement is a method invocation named "super" with no arguments, false
 *     otherwise
 */
public static boolean isDefaultSuperConstructorCall(StatementTree statement) {
    if (statement.getKind() != Tree.Kind.EXPRESSION_STATEMENT) {
        return false;
    }
    ExpressionStatementTree expressionStatement = (ExpressionStatementTree) statement;
    if (expressionStatement.getExpression().getKind() != Tree.Kind.METHOD_INVOCATION) {
        return false;
    }
    MethodInvocationTree invocation = (MethodInvocationTree) expressionStatement.getExpression();
    if (invocation.getMethodSelect().getKind() != Tree.Kind.IDENTIFIER) {
        return false;
    }
    if (!((IdentifierTree) invocation.getMethodSelect()).getName().contentEquals("super")) {
        return false;
    }
    return invocation.getArguments().isEmpty();
}
Also used : MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 33 with IdentifierTree

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

the class JointJavacJavaParserVisitor method visitAnnotation.

@Override
public Void visitAnnotation(AnnotationTree javacTree, Node javaParserNode) {
    // as @MyAnno(value="myArg") which has a single element argument list with an assignment.
    if (javaParserNode instanceof MarkerAnnotationExpr) {
        processAnnotation(javacTree, (MarkerAnnotationExpr) javaParserNode);
    } else if (javaParserNode instanceof SingleMemberAnnotationExpr) {
        SingleMemberAnnotationExpr node = (SingleMemberAnnotationExpr) javaParserNode;
        processAnnotation(javacTree, node);
        assert javacTree.getArguments().size() == 1;
        ExpressionTree value = javacTree.getArguments().get(0);
        assert value instanceof AssignmentTree;
        AssignmentTree assignment = (AssignmentTree) value;
        assert assignment.getVariable().getKind() == Tree.Kind.IDENTIFIER;
        assert ((IdentifierTree) assignment.getVariable()).getName().contentEquals("value");
        assignment.getExpression().accept(this, node.getMemberValue());
    } else if (javaParserNode instanceof NormalAnnotationExpr) {
        NormalAnnotationExpr node = (NormalAnnotationExpr) javaParserNode;
        processAnnotation(javacTree, node);
        assert javacTree.getArguments().size() == node.getPairs().size();
        Iterator<MemberValuePair> argIter = node.getPairs().iterator();
        for (ExpressionTree arg : javacTree.getArguments()) {
            assert arg instanceof AssignmentTree;
            AssignmentTree assignment = (AssignmentTree) arg;
            IdentifierTree memberName = (IdentifierTree) assignment.getVariable();
            MemberValuePair javaParserArg = argIter.next();
            assert memberName.getName().contentEquals(javaParserArg.getNameAsString());
            assignment.getExpression().accept(this, javaParserArg.getValue());
        }
    } else {
        throwUnexpectedNodeType(javacTree, javaParserNode);
    }
    return null;
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AssignmentTree(com.sun.source.tree.AssignmentTree) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr)

Example 34 with IdentifierTree

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

the class ExpectedTreesVisitor method visitClass.

@Override
public Void visitClass(ClassTree tree, Void p) {
    defaultAction(tree);
    scan(tree.getModifiers(), p);
    scan(tree.getTypeParameters(), p);
    scan(tree.getExtendsClause(), p);
    scan(tree.getImplementsClause(), p);
    if (tree.getKind() == Tree.Kind.ENUM) {
        // instance of an enum.
        for (Tree member : tree.getMembers()) {
            member.accept(this, p);
            if (member.getKind() != Tree.Kind.VARIABLE) {
                continue;
            }
            VariableTree variable = (VariableTree) member;
            ExpressionTree initializer = variable.getInitializer();
            if (initializer == null || initializer.getKind() != Tree.Kind.NEW_CLASS) {
                continue;
            }
            NewClassTree constructor = (NewClassTree) initializer;
            if (constructor.getIdentifier().getKind() != Tree.Kind.IDENTIFIER) {
                continue;
            }
            IdentifierTree name = (IdentifierTree) constructor.getIdentifier();
            if (name.getName().contentEquals(tree.getSimpleName())) {
                trees.remove(variable.getType());
                trees.remove(constructor);
                trees.remove(constructor.getIdentifier());
            }
        }
    // RECORD was added in Java 14, so use string comparison to be JDK 8,11 compatible:
    } else if (tree.getKind().name().equals("RECORD")) {
        // A record like:
        // record MyRec(String myField) {}
        // will be expanded by javac to:
        // class MyRec {
        // MyRec(String myField) {
        // super();
        // }
        // private final String myField;
        // }
        // So the constructor and the field declarations have no matching trees in the JavaParser
        // node, and we must remove those trees (and their subtrees) from the `trees` field.
        TreeScannerWithDefaults removeAllVisitor = new TreeScannerWithDefaults() {

            @Override
            public void defaultAction(Tree node) {
                trees.remove(node);
            }
        };
        for (Tree member : tree.getMembers()) {
            scan(member, p);
            if (TreeUtils.isAutoGeneratedRecordMember(member)) {
                member.accept(removeAllVisitor, null);
            } else {
                // the parameters.  These trees also don't have a match:
                if (member.getKind() == Tree.Kind.METHOD) {
                    MethodTree methodTree = (MethodTree) member;
                    if (TreeUtils.isCompactCanonicalRecordConstructor(methodTree)) {
                        for (VariableTree canonicalParameter : methodTree.getParameters()) {
                            canonicalParameter.accept(removeAllVisitor, null);
                        }
                    }
                }
            }
        }
    } else {
        scan(tree.getMembers(), p);
    }
    return null;
}
Also used : MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) NewClassTree(com.sun.source.tree.NewClassTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewArrayTree(com.sun.source.tree.NewArrayTree) ImportTree(com.sun.source.tree.ImportTree) Tree(com.sun.source.tree.Tree) ForLoopTree(com.sun.source.tree.ForLoopTree) ClassTree(com.sun.source.tree.ClassTree) IfTree(com.sun.source.tree.IfTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) SwitchTree(com.sun.source.tree.SwitchTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) DoWhileLoopTree(com.sun.source.tree.DoWhileLoopTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) StatementTree(com.sun.source.tree.StatementTree) ModifiersTree(com.sun.source.tree.ModifiersTree) WhileLoopTree(com.sun.source.tree.WhileLoopTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewClassTree(com.sun.source.tree.NewClassTree)

Example 35 with IdentifierTree

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

the class LockVisitor method visitIdentifier.

@Override
// AST node comparison
@SuppressWarnings("interning:not.interned")
public Void visitIdentifier(IdentifierTree tree, Void p) {
    // (All other field accesses are checked in visitMemberSelect.)
    if (TreeUtils.isFieldAccess(tree)) {
        Tree parent = getCurrentPath().getParentPath().getLeaf();
        // then the field is accessed via an implicit this.
        if ((parent.getKind() != Tree.Kind.MEMBER_SELECT || ((MemberSelectTree) parent).getExpression() == tree) && !ElementUtils.isStatic(TreeUtils.elementFromUse(tree))) {
            AnnotationMirror guardedBy = atypeFactory.getSelfType(tree).getAnnotationInHierarchy(atypeFactory.GUARDEDBY);
            checkLockOfImplicitThis(tree, guardedBy);
        }
    }
    return super.visitIdentifier(tree, p);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) IdentifierTree(com.sun.source.tree.IdentifierTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) VariableTree(com.sun.source.tree.VariableTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Aggregations

IdentifierTree (com.sun.source.tree.IdentifierTree)82 ExpressionTree (com.sun.source.tree.ExpressionTree)41 MemberSelectTree (com.sun.source.tree.MemberSelectTree)36 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)28 Element (javax.lang.model.element.Element)24 Tree (com.sun.source.tree.Tree)21 ExecutableElement (javax.lang.model.element.ExecutableElement)18 VariableTree (com.sun.source.tree.VariableTree)17 TypeElement (javax.lang.model.element.TypeElement)16 MethodTree (com.sun.source.tree.MethodTree)13 VariableElement (javax.lang.model.element.VariableElement)13 ClassTree (com.sun.source.tree.ClassTree)12 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)11 ArrayAccessTree (com.sun.source.tree.ArrayAccessTree)10 NewClassTree (com.sun.source.tree.NewClassTree)10 AssignmentTree (com.sun.source.tree.AssignmentTree)9 BinaryTree (com.sun.source.tree.BinaryTree)9 LiteralTree (com.sun.source.tree.LiteralTree)8 StatementTree (com.sun.source.tree.StatementTree)8 Symbol (com.sun.tools.javac.code.Symbol)8