use of com.sun.source.tree.IdentifierTree 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.IdentifierTree in project error-prone by google.
the class GuardedBySymbolResolver method resolveTypeLiteral.
@Override
public Symbol resolveTypeLiteral(ExpressionTree expr) {
checkGuardedBy(expr instanceof IdentifierTree, "bad type literal: %s", expr);
IdentifierTree ident = (IdentifierTree) expr;
Symbol type = resolveType(ident.getName().toString(), SearchSuperTypes.YES);
if (type instanceof Symbol.ClassSymbol) {
return type;
}
return null;
}
use of com.sun.source.tree.IdentifierTree in project error-prone by google.
the class ElementsCountedInLoop method matchEnhancedForLoop.
@Override
public Description matchEnhancedForLoop(EnhancedForLoopTree tree, VisitorState state) {
JCEnhancedForLoop enhancedForLoop = (JCEnhancedForLoop) tree;
IdentifierTree identifier = getIncrementedIdentifer(extractSingleStatement(enhancedForLoop.body));
if (identifier != null) {
ExpressionTree expression = tree.getExpression();
Fix fix;
if (isSubtypeOf("java.util.Collection").matches(expression, state)) {
String replacement = identifier + " += " + expression + ".size();";
fix = SuggestedFix.replace(tree, replacement);
} else if (isArrayType().matches(expression, state)) {
String replacement = identifier + " += " + expression + ".length;";
fix = SuggestedFix.replace(tree, replacement);
} else {
String replacement = identifier + " += Iterables.size(" + expression + ");";
fix = SuggestedFix.builder().replace(tree, replacement).addImport("com.google.common.collect.Iterables").build();
}
return describeMatch(tree, fix);
}
return Description.NO_MATCH;
}
use of com.sun.source.tree.IdentifierTree in project error-prone by google.
the class IsInstanceOfClass method classify.
static Operand classify(JCTree tree, VisitorState state) {
CharSequence source = state.getSourceForNode(tree);
if (tree instanceof MethodInvocationTree) {
// expr.getClass() -> "expr"
MethodInvocationTree receiverInvocation = (MethodInvocationTree) tree;
MethodSymbol sym = ASTHelpers.getSymbol(receiverInvocation);
if (sym != null) {
if (sym.getSimpleName().contentEquals("getClass") && sym.params().isEmpty()) {
if (receiverInvocation.getMethodSelect() instanceof IdentifierTree) {
// unqualified `getClass()`
return Operand.create(Kind.EXPR, state.getSourceForNode(tree), source);
}
return Operand.create(Kind.GET_CLASS, state.getSourceForNode((JCTree) ASTHelpers.getReceiver(receiverInvocation)), source);
}
}
} else if (tree instanceof MemberSelectTree) {
// Foo.class -> "Foo"
MemberSelectTree select = (MemberSelectTree) tree;
if (select.getIdentifier().contentEquals("class")) {
return Operand.create(Kind.LITERAL, state.getSourceForNode((JCTree) select.getExpression()), source);
}
}
return Operand.create(Kind.EXPR, source, source);
}
use of com.sun.source.tree.IdentifierTree in project error-prone by google.
the class UFreeIdent method defaultAction.
@Override
protected Choice<Unifier> defaultAction(Tree target, final Unifier unifier) {
if (target instanceof JCExpression) {
JCExpression expression = (JCExpression) target;
JCExpression currentBinding = unifier.getBinding(key());
// Check that the expression does not reference any template-local variables.
boolean isGood = trueOrNull(new TreeScanner<Boolean, Void>() {
@Override
public Boolean reduce(@Nullable Boolean left, @Nullable Boolean right) {
return trueOrNull(left) && trueOrNull(right);
}
@Override
public Boolean visitIdentifier(IdentifierTree ident, Void v) {
Symbol identSym = ASTHelpers.getSymbol(ident);
for (ULocalVarIdent.Key key : Iterables.filter(unifier.getBindings().keySet(), ULocalVarIdent.Key.class)) {
if (identSym == unifier.getBinding(key).getSymbol()) {
return false;
}
}
return true;
}
}.scan(expression, null));
if (!isGood) {
return Choice.none();
} else if (currentBinding == null) {
unifier.putBinding(key(), expression);
return Choice.of(unifier);
} else if (currentBinding.toString().equals(expression.toString())) {
// If it's the same code, treat it as the same expression.
return Choice.of(unifier);
}
}
return Choice.none();
}
Aggregations