Skip to content

Commit 70fe32b

Browse files
j2objc-copybaracopybara-github
authored andcommitted
Tree_shaker fixes for enhanced for statement
PiperOrigin-RevId: 436492047
1 parent 024f7e0 commit 70fe32b

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

tree_shaker/src/main/java/com/google/devtools/treeshaker/UsedCodeMarker.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.devtools.j2objc.ast.CompilationUnit;
2727
import com.google.devtools.j2objc.ast.ConstructorInvocation;
2828
import com.google.devtools.j2objc.ast.CreationReference;
29+
import com.google.devtools.j2objc.ast.EnhancedForStatement;
2930
import com.google.devtools.j2objc.ast.EnumConstantDeclaration;
3031
import com.google.devtools.j2objc.ast.EnumDeclaration;
3132
import com.google.devtools.j2objc.ast.ExpressionMethodReference;
@@ -119,6 +120,12 @@ public boolean visit(CreationReference node) {
119120
return true;
120121
}
121122

123+
@Override
124+
public void endVisit(EnhancedForStatement node) {
125+
// Add a reference to the enhanced for statement's parameter type.
126+
addReferencedType(node.getParameter().getType().getTypeMirror());
127+
}
128+
122129
@Override
123130
public void endVisit(EnumConstantDeclaration node) {
124131
pushClinit();

tree_shaker/src/test/java/com/google/devtools/treeshaker/TreeShakerTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,52 @@ public void testLambdaExpressionWithReturn() throws IOException {
12251225
assertThat(output).isEmpty();
12261226
}
12271227

1228+
// Regression test for b/224984057
1229+
public void testEnhancedForStatement() throws IOException {
1230+
addTreeShakerRootsFile("EntryClass\n");
1231+
addSourceFile(
1232+
"EntryClass.java",
1233+
"public class EntryClass {\n"
1234+
+ " public void exportedMethod() {\n"
1235+
+ " new CoffeeMaker().getGrinderSettings(new Grinder());\n"
1236+
+ " }\n"
1237+
+ "}");
1238+
addSourceFile(
1239+
"CoffeeMaker.java",
1240+
"import java.util.ArrayList;"
1241+
+ "import java.util.List;"
1242+
+ "public class CoffeeMaker {"
1243+
+ " public List<String> getGrinderSettings(Grinder grinder) {"
1244+
+ " List<String> result = new ArrayList<>();"
1245+
+ " for (Grinder.Setting test : grinder.getSettings()) {"
1246+
+ " result.add(test.toString());"
1247+
+ " }"
1248+
+ " return result;"
1249+
+ " }"
1250+
+ "}");
1251+
addSourceFile(
1252+
"Grinder.java",
1253+
"import java.util.ArrayList;"
1254+
+ "import java.util.List;"
1255+
+ "public class Grinder {"
1256+
+ " public enum Setting {"
1257+
+ " ESPRESSO, FILTER;"
1258+
+ " }"
1259+
+ " public List<Setting> getSettings() {"
1260+
+ " return new ArrayList<>();"
1261+
+ " }"
1262+
+ "}");
1263+
String output = writeUnused(findUnusedCode());
1264+
1265+
// Verify Grinder$Setting type isn't dead, but its unused methods are.
1266+
assertThat(output)
1267+
.isEqualTo(
1268+
"Grinder$Setting:\n"
1269+
+ " Grinder$Setting[] values()\n"
1270+
+ "Grinder$Setting:\n"
1271+
+ " Grinder$Setting valueOf(java.lang.String)\n");
1272+
}
1273+
12281274
private static String writeUnused(CodeReferenceMap unused) {
12291275
StringBuilder result = new StringBuilder();
12301276
TreeShaker.writeUnused(unused, result::append);

0 commit comments

Comments
 (0)