Search in sources :

Example 36 with IdentifierTree

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

the class TreeParserTest method parsesIdentifiers.

@Test
public void parsesIdentifiers() {
    String value = "id";
    ExpressionTree parsed = parser.parseTree(value);
    Assert.assertTrue(parsed instanceof IdentifierTree);
}
Also used : ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Test(org.junit.Test)

Example 37 with IdentifierTree

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

the class InterningVisitor method isReferenceEqualityImplementation.

/**
 * Returns true if the given equals() method implements reference equality.
 *
 * @param equalsMethod an overriding implementation of Object.equals()
 * @return true if the given equals() method implements reference equality
 */
private boolean isReferenceEqualityImplementation(MethodTree equalsMethod) {
    BlockTree body = equalsMethod.getBody();
    List<? extends StatementTree> bodyStatements = body.getStatements();
    if (bodyStatements.size() == 1) {
        StatementTree bodyStatement = bodyStatements.get(0);
        if (bodyStatement.getKind() == Tree.Kind.RETURN) {
            ExpressionTree returnExpr = TreeUtils.withoutParens(((ReturnTree) bodyStatement).getExpression());
            if (returnExpr.getKind() == Tree.Kind.EQUAL_TO) {
                BinaryTree bt = (BinaryTree) returnExpr;
                ExpressionTree lhsTree = bt.getLeftOperand();
                ExpressionTree rhsTree = bt.getRightOperand();
                if (lhsTree.getKind() == Tree.Kind.IDENTIFIER && rhsTree.getKind() == Tree.Kind.IDENTIFIER) {
                    Name leftName = ((IdentifierTree) lhsTree).getName();
                    Name rightName = ((IdentifierTree) rhsTree).getName();
                    Name paramName = equalsMethod.getParameters().get(0).getName();
                    if ((leftName.contentEquals("this") && rightName == paramName) || (leftName == paramName && rightName.contentEquals("this"))) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : StatementTree(com.sun.source.tree.StatementTree) BinaryTree(com.sun.source.tree.BinaryTree) BlockTree(com.sun.source.tree.BlockTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) CanonicalName(org.checkerframework.checker.signature.qual.CanonicalName) Name(javax.lang.model.element.Name)

Example 38 with IdentifierTree

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

the class CFGTranslationPhaseOne method extendWithAssignmentForConditionalExpr.

/**
 * Extend the CFG with an assignment for either the true or false case of a conditional
 * expression, assigning the value of the expression for the case to the synthetic variable for
 * the conditional expression
 *
 * @param condExprVarTree tree for synthetic variable for conditional expression
 * @param caseExprTree expression tree for the case
 * @param caseExprNode node for the case
 */
private void extendWithAssignmentForConditionalExpr(VariableTree condExprVarTree, ExpressionTree caseExprTree, Node caseExprNode) {
    Pair<IdentifierTree, LocalVariableNode> treeAndLocalVarNode = extendWithVarUseNode(condExprVarTree);
    AssignmentTree assign = treeBuilder.buildAssignment(treeAndLocalVarNode.first, caseExprTree);
    handleArtificialTree(assign);
    AssignmentNode assignmentNode = new AssignmentNode(assign, treeAndLocalVarNode.second, caseExprNode);
    assignmentNode.setInSource(false);
    extendWithNode(assignmentNode);
}
Also used : AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) StringConcatenateAssignmentNode(org.checkerframework.dataflow.cfg.node.StringConcatenateAssignmentNode) IdentifierTree(com.sun.source.tree.IdentifierTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AssignmentTree(com.sun.source.tree.AssignmentTree) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode)

Example 39 with IdentifierTree

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

the class CFGTranslationPhaseOne method extendWithVarUseNode.

/**
 * Extend the CFG with a {@link LocalVariableNode} representing a use of some variable
 *
 * @param varTree tree for the variable
 * @return a pair whose first element is the synthetic {@link IdentifierTree} for the use, and
 *     whose second element is the {@link LocalVariableNode} representing the use
 */
private Pair<IdentifierTree, LocalVariableNode> extendWithVarUseNode(VariableTree varTree) {
    IdentifierTree condExprVarUseTree = treeBuilder.buildVariableUse(varTree);
    handleArtificialTree(condExprVarUseTree);
    LocalVariableNode condExprVarUseNode = new LocalVariableNode(condExprVarUseTree);
    condExprVarUseNode.setInSource(false);
    extendWithNode(condExprVarUseNode);
    return Pair.of(condExprVarUseTree, condExprVarUseNode);
}
Also used : IdentifierTree(com.sun.source.tree.IdentifierTree) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode)

Example 40 with IdentifierTree

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

the class CFGTranslationPhaseOne method box.

/**
 * If the input node is an unboxed primitive type, insert a call to the appropriate valueOf
 * method, otherwise leave it alone.
 *
 * @param node in input node
 * @return a Node representing the boxed version of the input, which may simply be the input node
 */
protected Node box(Node node) {
    // For boxing conversion, see JLS 5.1.7
    if (TypesUtils.isPrimitive(node.getType())) {
        PrimitiveType primitive = types.getPrimitiveType(node.getType().getKind());
        TypeMirror boxedType = types.getDeclaredType(types.boxedClass(primitive));
        TypeElement boxedElement = (TypeElement) ((DeclaredType) boxedType).asElement();
        IdentifierTree classTree = treeBuilder.buildClassUse(boxedElement);
        handleArtificialTree(classTree);
        // No need to handle possible errors from evaluating a class literal here
        // since this is synthetic code that can't fail.
        ClassNameNode className = new ClassNameNode(classTree);
        className.setInSource(false);
        insertNodeAfter(className, node);
        MemberSelectTree valueOfSelect = treeBuilder.buildValueOfMethodAccess(classTree);
        handleArtificialTree(valueOfSelect);
        MethodAccessNode valueOfAccess = new MethodAccessNode(valueOfSelect, className);
        valueOfAccess.setInSource(false);
        insertNodeAfter(valueOfAccess, className);
        MethodInvocationTree valueOfCall = treeBuilder.buildMethodInvocation(valueOfSelect, (ExpressionTree) node.getTree());
        handleArtificialTree(valueOfCall);
        Node boxed = new MethodInvocationNode(valueOfCall, valueOfAccess, Collections.singletonList(node), getCurrentPath());
        boxed.setInSource(false);
        // Add Throwable to account for unchecked exceptions
        addToConvertedLookupMap(node.getTree(), boxed);
        insertNodeWithExceptionsAfter(boxed, uncheckedExceptionTypes, valueOfAccess);
        return boxed;
    } else {
        return node;
    }
}
Also used : MethodAccessNode(org.checkerframework.dataflow.cfg.node.MethodAccessNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) NumericalMultiplicationNode(org.checkerframework.dataflow.cfg.node.NumericalMultiplicationNode) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) ValueLiteralNode(org.checkerframework.dataflow.cfg.node.ValueLiteralNode) StringConversionNode(org.checkerframework.dataflow.cfg.node.StringConversionNode) UnsignedRightShiftNode(org.checkerframework.dataflow.cfg.node.UnsignedRightShiftNode) LeftShiftNode(org.checkerframework.dataflow.cfg.node.LeftShiftNode) PrimitiveTypeNode(org.checkerframework.dataflow.cfg.node.PrimitiveTypeNode) FloatLiteralNode(org.checkerframework.dataflow.cfg.node.FloatLiteralNode) LessThanNode(org.checkerframework.dataflow.cfg.node.LessThanNode) BitwiseOrNode(org.checkerframework.dataflow.cfg.node.BitwiseOrNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) NarrowingConversionNode(org.checkerframework.dataflow.cfg.node.NarrowingConversionNode) EqualToNode(org.checkerframework.dataflow.cfg.node.EqualToNode) NumericalPlusNode(org.checkerframework.dataflow.cfg.node.NumericalPlusNode) ConditionalAndNode(org.checkerframework.dataflow.cfg.node.ConditionalAndNode) VariableDeclarationNode(org.checkerframework.dataflow.cfg.node.VariableDeclarationNode) ClassDeclarationNode(org.checkerframework.dataflow.cfg.node.ClassDeclarationNode) IntegerDivisionNode(org.checkerframework.dataflow.cfg.node.IntegerDivisionNode) AssertionErrorNode(org.checkerframework.dataflow.cfg.node.AssertionErrorNode) InstanceOfNode(org.checkerframework.dataflow.cfg.node.InstanceOfNode) BooleanLiteralNode(org.checkerframework.dataflow.cfg.node.BooleanLiteralNode) ThisNode(org.checkerframework.dataflow.cfg.node.ThisNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) NullLiteralNode(org.checkerframework.dataflow.cfg.node.NullLiteralNode) ArrayTypeNode(org.checkerframework.dataflow.cfg.node.ArrayTypeNode) LambdaResultExpressionNode(org.checkerframework.dataflow.cfg.node.LambdaResultExpressionNode) IntegerRemainderNode(org.checkerframework.dataflow.cfg.node.IntegerRemainderNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) ConditionalOrNode(org.checkerframework.dataflow.cfg.node.ConditionalOrNode) NotEqualNode(org.checkerframework.dataflow.cfg.node.NotEqualNode) BitwiseXorNode(org.checkerframework.dataflow.cfg.node.BitwiseXorNode) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) ExplicitThisNode(org.checkerframework.dataflow.cfg.node.ExplicitThisNode) StringConcatenateNode(org.checkerframework.dataflow.cfg.node.StringConcatenateNode) NullChkNode(org.checkerframework.dataflow.cfg.node.NullChkNode) CharacterLiteralNode(org.checkerframework.dataflow.cfg.node.CharacterLiteralNode) FloatingDivisionNode(org.checkerframework.dataflow.cfg.node.FloatingDivisionNode) FunctionalInterfaceNode(org.checkerframework.dataflow.cfg.node.FunctionalInterfaceNode) StringConcatenateAssignmentNode(org.checkerframework.dataflow.cfg.node.StringConcatenateAssignmentNode) TypeCastNode(org.checkerframework.dataflow.cfg.node.TypeCastNode) MethodAccessNode(org.checkerframework.dataflow.cfg.node.MethodAccessNode) WideningConversionNode(org.checkerframework.dataflow.cfg.node.WideningConversionNode) LongLiteralNode(org.checkerframework.dataflow.cfg.node.LongLiteralNode) MarkerNode(org.checkerframework.dataflow.cfg.node.MarkerNode) ImplicitThisNode(org.checkerframework.dataflow.cfg.node.ImplicitThisNode) FloatingRemainderNode(org.checkerframework.dataflow.cfg.node.FloatingRemainderNode) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) ObjectCreationNode(org.checkerframework.dataflow.cfg.node.ObjectCreationNode) PackageNameNode(org.checkerframework.dataflow.cfg.node.PackageNameNode) DoubleLiteralNode(org.checkerframework.dataflow.cfg.node.DoubleLiteralNode) SuperNode(org.checkerframework.dataflow.cfg.node.SuperNode) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode) SignedRightShiftNode(org.checkerframework.dataflow.cfg.node.SignedRightShiftNode) ThrowNode(org.checkerframework.dataflow.cfg.node.ThrowNode) GreaterThanOrEqualNode(org.checkerframework.dataflow.cfg.node.GreaterThanOrEqualNode) StringLiteralNode(org.checkerframework.dataflow.cfg.node.StringLiteralNode) TernaryExpressionNode(org.checkerframework.dataflow.cfg.node.TernaryExpressionNode) BitwiseAndNode(org.checkerframework.dataflow.cfg.node.BitwiseAndNode) ParameterizedTypeNode(org.checkerframework.dataflow.cfg.node.ParameterizedTypeNode) CaseNode(org.checkerframework.dataflow.cfg.node.CaseNode) SwitchExpressionNode(org.checkerframework.dataflow.cfg.node.SwitchExpressionNode) NumericalAdditionNode(org.checkerframework.dataflow.cfg.node.NumericalAdditionNode) NumericalSubtractionNode(org.checkerframework.dataflow.cfg.node.NumericalSubtractionNode) BitwiseComplementNode(org.checkerframework.dataflow.cfg.node.BitwiseComplementNode) ConditionalNotNode(org.checkerframework.dataflow.cfg.node.ConditionalNotNode) NumericalMinusNode(org.checkerframework.dataflow.cfg.node.NumericalMinusNode) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) GreaterThanNode(org.checkerframework.dataflow.cfg.node.GreaterThanNode) LessThanOrEqualNode(org.checkerframework.dataflow.cfg.node.LessThanOrEqualNode) SynchronizedNode(org.checkerframework.dataflow.cfg.node.SynchronizedNode) Node(org.checkerframework.dataflow.cfg.node.Node) IdentifierTree(com.sun.source.tree.IdentifierTree) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) PrimitiveType(javax.lang.model.type.PrimitiveType)

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