use of com.sun.source.tree.IdentifierTree in project bazel by bazelbuild.
the class TreeUtils method isFieldAccess.
/**
* Determine whether <code>tree</code> is a field access expressions, such
* as
*
* <pre>
* <em>f</em>
* <em>obj</em> . <em>f</em>
* </pre>
*
* @return true iff if tree is a field access expression (implicit or
* explicit).
*/
public static boolean isFieldAccess(Tree tree) {
if (tree.getKind().equals(Tree.Kind.MEMBER_SELECT)) {
// explicit field access
MemberSelectTree memberSelect = (MemberSelectTree) tree;
Element el = TreeUtils.elementFromUse(memberSelect);
return el.getKind().isField();
} else if (tree.getKind().equals(Tree.Kind.IDENTIFIER)) {
// implicit field access
IdentifierTree ident = (IdentifierTree) tree;
Element el = TreeUtils.elementFromUse(ident);
return el.getKind().isField() && !ident.getName().contentEquals("this") && !ident.getName().contentEquals("super");
}
return false;
}
use of com.sun.source.tree.IdentifierTree in project bazel by bazelbuild.
the class TreeUtils method isMethodAccess.
/**
* Determine whether <code>tree</code> refers to a method element, such
* as
*
* <pre>
* <em>m</em>(...)
* <em>obj</em> . <em>m</em>(...)
* </pre>
*
* @return true iff if tree is a method access expression (implicit or
* explicit).
*/
public static boolean isMethodAccess(Tree tree) {
if (tree.getKind().equals(Tree.Kind.MEMBER_SELECT)) {
// explicit method access
MemberSelectTree memberSelect = (MemberSelectTree) tree;
Element el = TreeUtils.elementFromUse(memberSelect);
return el.getKind() == ElementKind.METHOD || el.getKind() == ElementKind.CONSTRUCTOR;
} else if (tree.getKind().equals(Tree.Kind.IDENTIFIER)) {
// implicit method access
IdentifierTree ident = (IdentifierTree) tree;
// The field "super" and "this" are also legal methods
if (ident.getName().contentEquals("super") || ident.getName().contentEquals("this")) {
return true;
}
Element el = TreeUtils.elementFromUse(ident);
return el.getKind() == ElementKind.METHOD || el.getKind() == ElementKind.CONSTRUCTOR;
}
return false;
}
use of com.sun.source.tree.IdentifierTree in project error-prone by google.
the class SimpleDateFormatConstant method threadLocalFix.
private static Fix threadLocalFix(VariableTree tree, VisitorState state, final VarSymbol sym) {
SuggestedFix.Builder fix = SuggestedFix.builder().replace(tree.getType(), String.format("ThreadLocal<%s>", state.getSourceForNode(tree.getType()))).prefixWith(tree.getInitializer(), "ThreadLocal.withInitial(() -> ").postfixWith(tree.getInitializer(), ")");
CompilationUnitTree unit = state.getPath().getCompilationUnit();
unit.accept(new TreeScanner<Void, Void>() {
@Override
public Void visitIdentifier(IdentifierTree tree, Void unused) {
if (Objects.equals(ASTHelpers.getSymbol(tree), sym)) {
fix.postfixWith(tree, ".get()");
}
return null;
}
}, null);
return fix.build();
}
use of com.sun.source.tree.IdentifierTree in project error-prone by google.
the class GuardedBySymbolResolver method resolveEnclosingClass.
@Override
public Symbol resolveEnclosingClass(ExpressionTree expr) {
checkGuardedBy(expr instanceof IdentifierTree, "bad type literal: %s", expr);
IdentifierTree ident = (IdentifierTree) expr;
Symbol type = resolveType(ident.getName().toString(), SearchSuperTypes.NO);
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 matchWhileLoop.
@Override
public Description matchWhileLoop(WhileLoopTree tree, VisitorState state) {
JCWhileLoop whileLoop = (JCWhileLoop) tree;
JCExpression whileExpression = ((JCParens) whileLoop.getCondition()).getExpression();
if (whileExpression instanceof MethodInvocationTree) {
MethodInvocationTree methodInvocation = (MethodInvocationTree) whileExpression;
if (instanceMethod().onDescendantOf("java.util.Iterator").withSignature("hasNext()").matches(methodInvocation, state)) {
IdentifierTree identifier = getIncrementedIdentifer(extractSingleStatement(whileLoop.body));
if (identifier != null) {
return describeMatch(tree);
}
}
}
return Description.NO_MATCH;
}
Aggregations