Skip to content

Commit

Permalink
Fix an incorrect UnnecessaryAnonymousClass refactoring
Browse files Browse the repository at this point in the history
don't refactor anonymous classes that implement the FI via a superclass.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=250349768
  • Loading branch information
cushon authored and ronshapiro committed Jun 12, 2019
1 parent 221fde7 commit 0faf698
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePathScanner;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.tree.JCTree;
import java.util.Objects;
Expand Down Expand Up @@ -99,6 +100,18 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
if (type == null || !state.getTypes().isFunctionalInterface(type)) {
return NO_MATCH;
}
MethodSymbol methodSymbol = getSymbol(implementation);
if (methodSymbol == null) {
return NO_MATCH;
}
Symbol descriptorSymbol = state.getTypes().findDescriptorSymbol(type.tsym);
if (!methodSymbol.getSimpleName().contentEquals(descriptorSymbol.getSimpleName())) {
return NO_MATCH;
}
if (!methodSymbol.overrides(
descriptorSymbol, methodSymbol.owner.enclClass(), state.getTypes(), false)) {
return NO_MATCH;
}
if (state.isAndroidCompatible()) {
return NO_MATCH;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,29 @@ public void variable_static() {
"}")
.doTest();
}

@Test
public void abstractClass() {
testHelper
.addInputLines(
"Test.java",
"import java.util.function.Function;",
"class Test {",
" static abstract class Impl implements Function<String, String> {",
" public String apply(String input) {",
" return input;",
" }",
" public abstract void f(String input);",
" }",
" private final Function<String, String> camelCase = new Impl() {",
" public void f(String input) {}",
" };",
" void g() {",
" Function<String, String> f = camelCase;",
" System.err.println(camelCase.apply(\"world\"));",
" }",
"}")
.expectUnchanged()
.doTest();
}
}

0 comments on commit 0faf698

Please sign in to comment.