Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ResourceIdentifierGetEqualsUsage #2830

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
WIP
  • Loading branch information
schlosna committed Nov 8, 2024
commit bbc4a202b6ec65d83f14c305b40cd7fd2670c0ed
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.Matchers;
import com.google.errorprone.matchers.method.MethodMatchers;
import com.google.errorprone.util.ASTHelpers;
import com.google.errorprone.util.ErrorProneToken;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Type;
import java.util.List;
import java.util.regex.Pattern;
import javax.lang.model.element.ElementKind;

@AutoService(BugChecker.class)
@BugPattern(
Expand All @@ -50,14 +57,17 @@ public final class ResourceIdentifierGetEqualsUsage extends BugChecker
.namedAnyOf("getInstance", "getLocator", "getService", "getType");
private static final Pattern SOURCE_GET_EQUALS_PATTERN =
Pattern.compile("get(Instance|Locator|Service|Type)\\(\\)\\.equals\\(");
private static final Pattern SOURCE_EQUALS_GET_PATTERN =
Pattern.compile("(.*)\\.equals\\((.*)\\.get(Instance|Locator|Service|Type)\\(\\)");

private static final Matcher<MethodInvocationTree> INVOCATION_TREE_MATCHER = Matchers.anyOf(
Matchers.allOf(EQUALS_MATCHER, Matchers.receiverOfInvocation(GET_MATCHER)),
EQUALS_MATCHER,
Matchers.allOf(Matchers.receiverOfInvocation(GET_MATCHER), EQUALS_MATCHER));

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!EQUALS_MATCHER.matches(tree, state)) {
return Description.NO_MATCH;
}
ExpressionTree receiverTree = ASTHelpers.getReceiver(tree);
if (receiverTree == null || !GET_MATCHER.matches(receiverTree, state)) {
if (!INVOCATION_TREE_MATCHER.matches(tree, state)) {
return Description.NO_MATCH;
}

Expand All @@ -66,9 +76,53 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
return Description.NO_MATCH;
}

String replacement = SOURCE_GET_EQUALS_PATTERN.matcher(source).replaceAll("has$1(");
return buildDescription(tree)
.addFix(SuggestedFix.replace(tree, replacement))
.build();
ExpressionTree receiverTree = ASTHelpers.getReceiver(tree);
if (receiverTree == null) {
return Description.NO_MATCH;
}

MethodSymbol methodSymbol = ASTHelpers.getSymbol(tree);
List<ErrorProneToken> errorProneTokens = state.getOffsetTokensForNode(tree);
int receiverEnd = state.getEndPosition(receiverTree);
int treeEnd = state.getEndPosition(tree);

if (EQUALS_MATCHER.matches(tree, state)) {
if (GET_MATCHER.matches(receiverTree, state)) {
Type receiverType = ASTHelpers.getReceiverType(receiverTree);
Symbol receiverSymbol = ASTHelpers.getSymbol(receiverTree);
ElementKind receiverKind = receiverSymbol.getKind();
System.out.printf(
"%n%nState" + "tree: %s%n"
+ "method: %s%n" + "receiver: %s %s %s%n" + "tokens: %s%n"
+ "positions: rec %d tree %d%n%n%n",
tree,
methodSymbol,
receiverKind,
receiverType,
receiverSymbol,
errorProneTokens,
receiverEnd,
treeEnd);
System.out.flush();

String replacement = SOURCE_GET_EQUALS_PATTERN.matcher(source).replaceAll("has$1(");
return buildDescription(tree)
.addFix(SuggestedFix.builder()
.replace(tree, replacement)
.build())
.build();
}

System.out.printf(
"%n%nState" + "tree: %s%n" + "method: %s%n" + "tokens: %s%n" + "positions: rec %d tree %d%n%n%n",
tree, methodSymbol, errorProneTokens, receiverEnd, treeEnd);
System.out.flush();
String replacement = SOURCE_EQUALS_GET_PATTERN.matcher(source).replaceAll("$2.has$3($1");
SuggestedFix fix = SuggestedFix.builder().replace(tree, replacement).build();

return buildDescription(tree).addFix(fix).build();
}

return Description.NO_MATCH;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ void testHasInstance() {
" }",
"}")
.doTest();
}

@Test
void testHasInstanceFlipped() {
fix().addInputLines(
"Test.java",
"import com.palantir.ri.ResourceIdentifier;",
Expand Down