use of com.sun.source.tree.MemberSelectTree 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.MemberSelectTree in project error-prone by google.
the class FilesLinesLeak method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!MATCHER.matches(tree, state)) {
return NO_MATCH;
}
if (inTWR(state)) {
return NO_MATCH;
}
Description.Builder description = buildDescription(tree);
Tree parent = state.getPath().getParentPath().getLeaf();
if (parent instanceof MemberSelectTree) {
MemberSelectTree select = (MemberSelectTree) parent;
StatementTree statement = state.findEnclosing(StatementTree.class);
SuggestedFix.Builder fix = SuggestedFix.builder();
if (statement instanceof VariableTree) {
VariableTree var = (VariableTree) statement;
int pos = ((JCTree) var).getStartPosition();
int initPos = ((JCTree) var.getInitializer()).getStartPosition();
int eqPos = pos + state.getSourceForNode(var).substring(0, initPos - pos).lastIndexOf('=');
fix.replace(eqPos, initPos, String.format(";\ntry (Stream<String> stream = %s) {\n%s =", state.getSourceForNode(tree), var.getName().toString()));
} else {
fix.prefixWith(statement, String.format("try (Stream<String> stream = %s) {\n", state.getSourceForNode(tree)));
fix.replace(select.getExpression(), "stream");
}
fix.replace(tree, "stream");
fix.postfixWith(statement, "}");
fix.addImport("java.util.stream.Stream");
description.addFix(fix.build());
}
return description.build();
}
use of com.sun.source.tree.MemberSelectTree in project error-prone by google.
the class SelfAssignment method matchVariable.
@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
ExpressionTree initializer = stripCheckNotNull(tree.getInitializer(), state);
Tree parent = state.getPath().getParentPath().getLeaf();
// must be a static class variable with member select initializer
if (initializer == null || initializer.getKind() != MEMBER_SELECT || parent.getKind() != CLASS || !tree.getModifiers().getFlags().contains(STATIC)) {
return Description.NO_MATCH;
}
MemberSelectTree rhs = (MemberSelectTree) initializer;
Symbol rhsClass = ASTHelpers.getSymbol(rhs.getExpression());
Symbol lhsClass = ASTHelpers.getSymbol(parent);
if (rhsClass != null && lhsClass != null && rhsClass.equals(lhsClass) && rhs.getIdentifier().contentEquals(tree.getName())) {
return describeForVarDecl(tree, state);
}
return Description.NO_MATCH;
}
use of com.sun.source.tree.MemberSelectTree 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.MemberSelectTree in project error-prone by google.
the class ASTHelpers method hasSimpleName.
private static boolean hasSimpleName(AnnotationTree annotation, String name) {
Tree annotationType = annotation.getAnnotationType();
javax.lang.model.element.Name simpleName;
if (annotationType instanceof IdentifierTree) {
simpleName = ((IdentifierTree) annotationType).getName();
} else if (annotationType instanceof MemberSelectTree) {
simpleName = ((MemberSelectTree) annotationType).getIdentifier();
} else {
return false;
}
return simpleName.contentEquals(name);
}
Aggregations