use of com.sun.source.tree.ExpressionTree in project bazel by bazelbuild.
the class TreeUtils method isSelfAccess.
/**
* Returns true if the tree is a tree that 'looks like' either an access
* of a field or an invocation of a method that are owned by the same
* accessing instance.
*
* It would only return true if the access tree is of the form:
* <pre>
* field
* this.field
*
* method()
* this.method()
* </pre>
*
* It does not perform any semantical check to differentiate between
* fields and local variables; local methods or imported static methods.
*
* @param tree expression tree representing an access to object member
* @return {@code true} iff the member is a member of {@code this} instance
*/
public static boolean isSelfAccess(final ExpressionTree tree) {
ExpressionTree tr = TreeUtils.skipParens(tree);
// If method invocation check the method select
if (tr.getKind() == Tree.Kind.ARRAY_ACCESS)
return false;
if (tree.getKind() == Tree.Kind.METHOD_INVOCATION) {
tr = ((MethodInvocationTree) tree).getMethodSelect();
}
tr = TreeUtils.skipParens(tr);
if (tr.getKind() == Tree.Kind.TYPE_CAST)
tr = ((TypeCastTree) tr).getExpression();
tr = TreeUtils.skipParens(tr);
if (tr.getKind() == Tree.Kind.IDENTIFIER)
return true;
if (tr.getKind() == Tree.Kind.MEMBER_SELECT) {
tr = ((MemberSelectTree) tr).getExpression();
if (tr.getKind() == Tree.Kind.IDENTIFIER) {
Name ident = ((IdentifierTree) tr).getName();
return ident.contentEquals("this") || ident.contentEquals("super");
}
}
return false;
}
use of com.sun.source.tree.ExpressionTree in project bazel by bazelbuild.
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.ExpressionTree in project error-prone by google.
the class AbstractCollectionIncompatibleTypeMatcher method matches.
@Nullable
public final MatchResult matches(MethodInvocationTree tree, VisitorState state) {
if (!methodMatcher().matches(tree, state)) {
return null;
}
ExpressionTree sourceTree = extractSourceTree(tree, state);
Type sourceType = extractSourceType(tree, state);
Type targetType = extractTargetType(tree, state);
if (sourceTree == null || sourceType == null || targetType == null) {
return null;
}
return MatchResult.create(extractSourceTree(tree, state), extractSourceType(tree, state), extractTargetType(tree, state), this);
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class SizeGreaterThanOrEqualsZero method isGreaterThanEqualToZero.
private ExpressionType isGreaterThanEqualToZero(BinaryTree tree) {
ExpressionTree literalOperand;
ExpressionType returnType;
switch(tree.getKind()) {
case GREATER_THAN_EQUAL:
literalOperand = tree.getRightOperand();
returnType = ExpressionType.GREATER_THAN_EQUAL;
break;
case LESS_THAN_EQUAL:
literalOperand = tree.getLeftOperand();
returnType = ExpressionType.LESS_THAN_EQUAL;
break;
default:
return ExpressionType.MISMATCH;
}
if (literalOperand.getKind() != Kind.INT_LITERAL) {
return ExpressionType.MISMATCH;
}
if (!((LiteralTree) literalOperand).getValue().equals(0)) {
return ExpressionType.MISMATCH;
}
return returnType;
}
use of com.sun.source.tree.ExpressionTree in project error-prone by google.
the class StringBuilderInitWithChar method matchNewClass.
@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
if (ASTHelpers.isSameType(state.getSymtab().stringBuilderType, ASTHelpers.getType(tree.getIdentifier()), state) && tree.getArguments().size() == 1) {
ExpressionTree argument = tree.getArguments().get(0);
Type type = ((JCTree) argument).type;
if (type.getKind() == TypeKind.CHAR) {
if (argument.getKind() == Kind.CHAR_LITERAL) {
char ch = (Character) ((LiteralTree) argument).getValue();
return describeMatch(tree, SuggestedFix.replace(argument, "\"" + Convert.quote(Character.toString(ch)) + "\""));
} else {
return describeMatch(tree, SuggestedFix.replace(tree, "new StringBuilder().append(" + state.getSourceForNode((JCTree) argument) + ")"));
}
}
}
return Description.NO_MATCH;
}
Aggregations