Skip to content

Commit

Permalink
handle explicit this
Browse files Browse the repository at this point in the history
  • Loading branch information
forozco committed May 29, 2020
1 parent c7fa4b0 commit 9ac79db
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private Description convertSuppliersAndVariableInstanceMethods(
}

ExpressionTree receiver = ASTHelpers.getReceiver(methodInvocation);
boolean isLocal = receiver == null;
boolean isLocal = isLocal(methodInvocation);
if (!isLocal && !(receiver instanceof IdentifierTree)) {
return Description.NO_MATCH;
}
Expand All @@ -145,10 +145,9 @@ private Description convertMethodInvocations(
return Description.NO_MATCH;
}

boolean isLocal = ASTHelpers.getReceiver(methodInvocation) == null;
return buildDescription(root)
.setMessage(MESSAGE)
.addFix(buildFix(methodSymbol, methodInvocation, root, state, isLocal))
.addFix(buildFix(methodSymbol, methodInvocation, root, state, isLocal(methodInvocation)))
.build();
}

Expand Down Expand Up @@ -197,4 +196,11 @@ private static Optional<String> toMethodReference(String qualifiedMethodName) {
}
return Optional.empty();
}

private static boolean isLocal(MethodInvocationTree methodInvocationTree) {
ExpressionTree receiver = ASTHelpers.getReceiver(methodInvocationTree);
return receiver == null
|| (receiver instanceof IdentifierTree
&& "this".equals(((IdentifierTree) receiver).getName().toString()));

This comment has been minimized.

Copy link
@carterkozak

carterkozak May 29, 2020

Contributor

nit: Name.contentEquals("this") to avoid toString

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,34 @@ void testAutoFix_localInstanceMethod() {
.doTest();
}

@Test
void testAutoFix_localInstanceMethod_explicitThis() {
refactoringValidator
.addInputLines(
"Test.java",
"import " + Optional.class.getName() + ';',
"class Test {",
" public Optional<String> foo(Optional<String> optional) {",
" return optional.map(v -> this.bar(v));",
" }",
" private String bar(String v) {",
" return v;",
" }",
"}")
.addOutputLines(
"Test.java",
"import " + Optional.class.getName() + ';',
"class Test {",
" public Optional<String> foo(Optional<String> optional) {",
" return optional.map(this::bar);",
" }",
" private String bar(String v) {",
" return v;",
" }",
"}")
.doTest();
}

@Test
void testAutoFix_localStaticMethod() {
refactoringValidator
Expand Down

0 comments on commit 9ac79db

Please sign in to comment.