Search in sources :

Example 51 with IdentifierTree

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

the class FlowExpressions method internalReprOf.

/**
 * We ignore operations such as widening and narrowing when computing the internal
 * representation.
 *
 * @return the internal representation (as {@link Receiver}) of any {@link ExpressionTree}.
 *     Might contain {@link Unknown}.
 */
public static Receiver internalReprOf(AnnotationProvider provider, ExpressionTree receiverTree, boolean allowNonDeterministic) {
    Receiver receiver;
    switch(receiverTree.getKind()) {
        case ARRAY_ACCESS:
            ArrayAccessTree a = (ArrayAccessTree) receiverTree;
            Receiver arrayAccessExpression = internalReprOf(provider, a.getExpression());
            Receiver index = internalReprOf(provider, a.getIndex());
            receiver = new ArrayAccess(TreeUtils.typeOf(a), arrayAccessExpression, index);
            break;
        case BOOLEAN_LITERAL:
        case CHAR_LITERAL:
        case DOUBLE_LITERAL:
        case FLOAT_LITERAL:
        case INT_LITERAL:
        case LONG_LITERAL:
        case NULL_LITERAL:
        case STRING_LITERAL:
            LiteralTree vn = (LiteralTree) receiverTree;
            receiver = new ValueLiteral(TreeUtils.typeOf(receiverTree), vn.getValue());
            break;
        case NEW_ARRAY:
            NewArrayTree newArrayTree = (NewArrayTree) receiverTree;
            List<Receiver> dimensions = new ArrayList<>();
            if (newArrayTree.getDimensions() != null) {
                for (ExpressionTree dimension : newArrayTree.getDimensions()) {
                    dimensions.add(internalReprOf(provider, dimension, allowNonDeterministic));
                }
            }
            List<Receiver> initializers = new ArrayList<>();
            if (newArrayTree.getInitializers() != null) {
                for (ExpressionTree initializer : newArrayTree.getInitializers()) {
                    initializers.add(internalReprOf(provider, initializer, allowNonDeterministic));
                }
            }
            receiver = new ArrayCreation(TreeUtils.typeOf(receiverTree), dimensions, initializers);
            break;
        case METHOD_INVOCATION:
            MethodInvocationTree mn = (MethodInvocationTree) receiverTree;
            ExecutableElement invokedMethod = TreeUtils.elementFromUse(mn);
            if (PurityUtils.isDeterministic(provider, invokedMethod) || allowNonDeterministic) {
                List<Receiver> parameters = new ArrayList<>();
                for (ExpressionTree p : mn.getArguments()) {
                    parameters.add(internalReprOf(provider, p));
                }
                Receiver methodReceiver;
                if (ElementUtils.isStatic(invokedMethod)) {
                    methodReceiver = new ClassName(TreeUtils.typeOf(mn.getMethodSelect()));
                } else {
                    ExpressionTree methodReceiverTree = TreeUtils.getReceiverTree(mn);
                    if (methodReceiverTree != null) {
                        methodReceiver = internalReprOf(provider, methodReceiverTree);
                    } else {
                        methodReceiver = internalReprOfImplicitReceiver(invokedMethod);
                    }
                }
                TypeMirror type = TreeUtils.typeOf(mn);
                receiver = new MethodCall(type, invokedMethod, methodReceiver, parameters);
            } else {
                receiver = null;
            }
            break;
        case MEMBER_SELECT:
            receiver = internalReprOfMemberSelect(provider, (MemberSelectTree) receiverTree);
            break;
        case IDENTIFIER:
            IdentifierTree identifierTree = (IdentifierTree) receiverTree;
            TypeMirror typeOfId = TreeUtils.typeOf(identifierTree);
            if (identifierTree.getName().contentEquals("this") || identifierTree.getName().contentEquals("super")) {
                receiver = new ThisReference(typeOfId);
                break;
            }
            Element ele = TreeUtils.elementFromUse(identifierTree);
            switch(ele.getKind()) {
                case LOCAL_VARIABLE:
                case RESOURCE_VARIABLE:
                case EXCEPTION_PARAMETER:
                case PARAMETER:
                    receiver = new LocalVariable(ele);
                    break;
                case FIELD:
                    // Implicit access expression, such as "this" or a class name
                    Receiver fieldAccessExpression;
                    TypeMirror enclosingType = ElementUtils.enclosingClass(ele).asType();
                    if (ElementUtils.isStatic(ele)) {
                        fieldAccessExpression = new ClassName(enclosingType);
                    } else {
                        fieldAccessExpression = new ThisReference(enclosingType);
                    }
                    receiver = new FieldAccess(fieldAccessExpression, typeOfId, (VariableElement) ele);
                    break;
                case CLASS:
                case ENUM:
                case ANNOTATION_TYPE:
                case INTERFACE:
                    receiver = new ClassName(ele.asType());
                    break;
                default:
                    receiver = null;
            }
            break;
        default:
            receiver = null;
    }
    if (receiver == null) {
        receiver = new Unknown(TreeUtils.typeOf(receiverTree));
    }
    return receiver;
}
Also used : ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) ExecutableElement(javax.lang.model.element.ExecutableElement) MemberSelectTree(com.sun.source.tree.MemberSelectTree) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) IdentifierTree(com.sun.source.tree.IdentifierTree) VariableElement(javax.lang.model.element.VariableElement) LiteralTree(com.sun.source.tree.LiteralTree) NewArrayTree(com.sun.source.tree.NewArrayTree) TypeMirror(javax.lang.model.type.TypeMirror) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 52 with IdentifierTree

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

the class TreeUtils method methodName.

/**
 * @return the name of the invoked method
 */
public static final Name methodName(MethodInvocationTree node) {
    ExpressionTree expr = node.getMethodSelect();
    if (expr.getKind() == Tree.Kind.IDENTIFIER) {
        return ((IdentifierTree) expr).getName();
    } else if (expr.getKind() == Tree.Kind.MEMBER_SELECT) {
        return ((MemberSelectTree) expr).getIdentifier();
    }
    ErrorReporter.errorAbort("TreeUtils.methodName: cannot be here: " + node);
    // dead code
    return null;
}
Also used : ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 53 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 54 with IdentifierTree

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

the class TreeParserTest method parsesIndex.

@Test
public void parsesIndex() {
    String value = "array[2]";
    ExpressionTree parsed = parser.parseTree(value);
    Assert.assertTrue(parsed instanceof ArrayAccessTree);
    ArrayAccessTree access = (ArrayAccessTree) parsed;
    Assert.assertEquals(2, ((LiteralTree) access.getIndex()).getValue());
    Assert.assertEquals("array", ((IdentifierTree) access.getExpression()).getName().toString());
}
Also used : ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Test(org.junit.Test)

Example 55 with IdentifierTree

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

the class DependentTypesHelper method standardizeVariable.

public void standardizeVariable(Tree node, AnnotatedTypeMirror type, Element ele) {
    if (!hasDependentType(type)) {
        return;
    }
    TreePath path = factory.getPath(node);
    if (path == null) {
        return;
    }
    switch(ele.getKind()) {
        case PARAMETER:
            Tree enclTree = TreeUtils.enclosingOfKind(path, new HashSet<>(Arrays.asList(Kind.METHOD, Kind.LAMBDA_EXPRESSION)));
            if (enclTree.getKind() == Kind.METHOD) {
                // If the most enclosing tree is a method, the parameter is a method parameter
                MethodTree methodTree = (MethodTree) enclTree;
                TypeMirror enclosingType = ElementUtils.enclosingClass(ele).asType();
                FlowExpressionContext parameterContext = FlowExpressionContext.buildContextForMethodDeclaration(methodTree, enclosingType, factory.getContext());
                standardizeDoNotUseLocals(parameterContext, path, type);
            } else {
                // Otherwise, the parameter is a lambda parameter
                LambdaExpressionTree lambdaTree = (LambdaExpressionTree) enclTree;
                FlowExpressionContext parameterContext = FlowExpressionContext.buildContextForLambda(lambdaTree, path, factory.getContext());
                // TODO: test this.
                // TODO: use path.getParentPath to prevent a StackOverflowError, see Issue
                // #1027.
                standardizeUseLocals(parameterContext, path.getParentPath(), type);
            }
            break;
        case LOCAL_VARIABLE:
        case RESOURCE_VARIABLE:
        case EXCEPTION_PARAMETER:
            TypeMirror enclosingType = ElementUtils.enclosingClass(ele).asType();
            FlowExpressions.Receiver receiver = FlowExpressions.internalReprOfPseudoReceiver(path, enclosingType);
            List<Receiver> params = FlowExpressions.getParametersOfEnclosingMethod(factory, path);
            FlowExpressionContext localContext = new FlowExpressionContext(receiver, params, factory.getContext());
            standardizeUseLocals(localContext, path, type);
            break;
        case FIELD:
            FlowExpressions.Receiver receiverF;
            if (node.getKind() == Tree.Kind.IDENTIFIER) {
                FlowExpressions.Receiver r = FlowExpressions.internalReprOf(factory, (IdentifierTree) node);
                receiverF = r instanceof FlowExpressions.FieldAccess ? ((FlowExpressions.FieldAccess) r).getReceiver() : r;
            } else {
                receiverF = FlowExpressions.internalReprOfImplicitReceiver(ele);
            }
            FlowExpressionContext fieldContext = new FlowExpressionContext(receiverF, null, factory.getContext());
            standardizeDoNotUseLocals(fieldContext, path, type);
            break;
        default:
    }
}
Also used : LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) TreePath(com.sun.source.util.TreePath) MethodTree(com.sun.source.tree.MethodTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) FlowExpressionContext(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ModifiersTree(com.sun.source.tree.ModifiersTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) FlowExpressions(org.checkerframework.dataflow.analysis.FlowExpressions) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)

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