Add support and unit tests for method parameters. PiperOrigin-RevId: 362595259
diff --git a/tree_shaker/src/main/java/com/google/devtools/treeshaker/UsedCodeMarker.java b/tree_shaker/src/main/java/com/google/devtools/treeshaker/UsedCodeMarker.java index 4c3db31..a69c8c8 100644 --- a/tree_shaker/src/main/java/com/google/devtools/treeshaker/UsedCodeMarker.java +++ b/tree_shaker/src/main/java/com/google/devtools/treeshaker/UsedCodeMarker.java
@@ -33,6 +33,7 @@ import com.google.devtools.j2objc.ast.SuperConstructorInvocation; import com.google.devtools.j2objc.ast.SuperMethodInvocation; import com.google.devtools.j2objc.ast.TypeDeclaration; +import com.google.devtools.j2objc.ast.TypeMethodReference; import com.google.devtools.j2objc.ast.UnitTreeVisitor; import com.google.devtools.j2objc.ast.VariableDeclarationFragment; import com.google.devtools.j2objc.util.CodeReferenceMap; @@ -172,6 +173,18 @@ } @Override + public void endVisit(TypeMethodReference node) { + context.addMethodInvocation( + getMethodName(node.getExecutableElement()), + getDeclaringClassName(node.getExecutableElement())); + // A method expression implicitly constructs an instance of the interface that it implements. + context.addMethodInvocation( + getPseudoConstructorName(node.getTypeMirror().toString()), + node.getTypeMirror().toString()); + } + + + @Override public void endVisit(VariableDeclarationFragment node) { context.addReferencedType(node.getVariableElement().asType()); }
diff --git a/tree_shaker/src/test/java/com/google/devtools/treeshaker/TreeShakerTest.java b/tree_shaker/src/test/java/com/google/devtools/treeshaker/TreeShakerTest.java index c4306d0..1a66a70 100644 --- a/tree_shaker/src/test/java/com/google/devtools/treeshaker/TreeShakerTest.java +++ b/tree_shaker/src/test/java/com/google/devtools/treeshaker/TreeShakerTest.java
@@ -486,7 +486,7 @@ public void testLambdas() throws IOException { addTreeShakerRootsFile("A:\n main(java.lang.String[])"); addSourceFile("A.java", - "class A { static void main(String[] args) { B b = (x) -> x; b.op(5); } }"); + "class A { static void main(String[] args) { B b = x -> x; b.op(5); } }"); addSourceFile("B.java", "interface B { int op(int x); }"); CodeReferenceMap unused = findUnusedCode(); @@ -497,6 +497,45 @@ assertThat(getUnusedMethods(unused)).containsExactly(getMethodName("A", "A", "()V")); } + public void testLambdasUnused() throws IOException { + addTreeShakerRootsFile("A:\n main(java.lang.String[])"); + addSourceFile("A.java", "class A { static void main(String[] args) { B b = i -> i; } }"); + addSourceFile("B.java", "interface B { int op(int x); }"); + CodeReferenceMap unused = findUnusedCode(); + + assertThat(getUnusedClasses(unused)).containsNoneOf("A", "B"); + assertThat(getUnusedClasses(unused)).isEmpty(); + + // Note: B.op is marked unused, which is a non-issue since the generated iOS protocols + // don't contain any code. + assertThat(getUnusedMethods(unused)).containsExactly( + getMethodName("A", "A", "()V"), + getMethodName("B", "op", "(I)I")); + } + + public void testMethodValues() throws IOException { + addTreeShakerRootsFile("A:\n main(java.lang.String[])"); + addSourceFile("A.java", + "class A {", + " static void main(String[] args) {", + " execute(A::log);", + " }", + " static void execute (B b) { b.op(\"a\"); }", + " static void log (Object s) { }", + "}"); + addSourceFile("B.java", "interface B { void op(String s); }"); + CodeReferenceMap unused = findUnusedCode(); + + assertThat(getUnusedClasses(unused)).containsNoneOf("A", "B"); + assertThat(getUnusedClasses(unused)).isEmpty(); + + assertThat(getUnusedMethods(unused)).containsNoneOf( + getMethodName("A", "execute", "(B)V"), + getMethodName("A", "log", "(Ljava/lang/Object;)V"), + getMethodName("B", "op", "(Ljava/lang/String;)V")); + assertThat(getUnusedMethods(unused)).containsExactly(getMethodName("A", "A", "()V")); + } + public void testParametricTypes() throws IOException { addTreeShakerRootsFile("A:\n main(java.lang.String[])"); addSourceFile("A.java", "class A { static void main(String[] args) { B<C> b = new B<C>(); }}"); @@ -654,10 +693,14 @@ Files.asCharSink(treeShakerRoots, Charset.defaultCharset()).write(source); } - private void addSourceFile(String fileName, String source) throws IOException { + private void addSourceFile(String fileName, String... sources) throws IOException { File file = new File(tempDir, fileName); file.getParentFile().mkdirs(); - Files.write(source, file, Charset.defaultCharset()); + StringBuilder source = new StringBuilder(); + for (String s : sources) { + source.append(s); + } + Files.asCharSink(file, Charset.defaultCharset()).write(source); inputFiles.add(file.getAbsolutePath()); }