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;
}
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;
}
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);
}
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());
}
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:
}
}
Aggregations