diff --git a/check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java b/check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java index c4c5d9f9b37..6b2e28d3562 100644 --- a/check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java +++ b/check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java @@ -67,6 +67,7 @@ import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.IdentifierTree; import com.sun.source.tree.InstanceOfTree; +import com.sun.source.tree.MemberReferenceTree; import com.sun.source.tree.MemberSelectTree; import com.sun.source.tree.MethodInvocationTree; import com.sun.source.tree.MethodTree; @@ -785,6 +786,17 @@ public Void visitMemberSelect(MemberSelectTree tree, Void unused) { } return super.visitMemberSelect(tree, null); } + + @Override + public Void visitMemberReference(MemberReferenceTree tree, Void unused) { + if (sym.equals(getSymbol(tree))) { + fix.replace( + state.getEndPosition(tree.getQualifierExpression()), + state.getEndPosition(tree), + "::" + replacement); + } + return super.visitMemberReference(tree, unused); + } }.scan(state.getPath().getCompilationUnit(), null); return fix.build(); } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/MemberNameTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/MemberNameTest.java index abec4f53d28..6e58cfdb57a 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/MemberNameTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/MemberNameTest.java @@ -445,4 +445,22 @@ public void lambdaExpressionParameterInsideOverridingMethod() { "}") .doTest(); } + + @Test + public void methodReference() { + refactoringHelper + .addInputLines( + "Test.java", + "class Test {", + " private void foo_bar() {}", + " private Runnable r = this::foo_bar;", + "}") + .addOutputLines( + "Test.java", + "class Test {", + " private void fooBar() {}", + " private Runnable r = this::fooBar;", + "}") + .doTest(); + } }