use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class UnusedCodeTrackerTest method testPublicFieldInUnusedClass.
public void testPublicFieldInUnusedClass() throws IOException {
String source = "class A {\n" + " public int bar = 1;\n" + "}\n" + "class B {\n" + " private int baz = 2;\n" + "}\n";
CompilationUnit unit = compileType("test", source);
final HashMap<String, ReferenceNode> elementMap = new HashMap<>();
final HashMap<String, Set<String>> overrideMap = new HashMap<>();
final Set<String> staticSet = new HashSet<>();
ElementReferenceMapper mapper = new ElementReferenceMapper(unit, elementMap, staticSet, overrideMap);
mapper.run();
UnusedCodeTracker tracker = new UnusedCodeTracker(unit.getEnv(), elementMap, staticSet, overrideMap);
tracker.markUsedElements();
CodeReferenceMap unusedCodeMap = tracker.buildTreeShakerMap();
assertFalse(unusedCodeMap.containsClass("A"));
assertTrue(unusedCodeMap.containsClass("B"));
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class UnusedCodeTrackerTest method testUnusedMethodCallingMethods.
public void testUnusedMethodCallingMethods() throws IOException {
String source = "class A {\n" + " private static void abc(String s) {}\n" + " private static void xyz(String s) {abc(\"goo\");}\n" + " private static void foo(String s) {xyz(\"woo\");}\n" + " private static void unused2(String s) {foo(\"boo\");}\n" + " private static void unused(String s) {unused2(\"boo\");}\n" + " static { foo(\"zoo\"); }\n" + "}\n";
CompilationUnit unit = compileType("test", source);
final HashMap<String, ReferenceNode> elementMap = new HashMap<>();
final HashMap<String, Set<String>> overrideMap = new HashMap<>();
final Set<String> staticSet = new HashSet<>();
ElementReferenceMapper mapper = new ElementReferenceMapper(unit, elementMap, staticSet, overrideMap);
mapper.run();
UnusedCodeTracker tracker = new UnusedCodeTracker(unit.getEnv(), elementMap, staticSet, overrideMap);
tracker.markUsedElements();
assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "foo", "(Ljava/lang/String;)V")).reachable);
assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "xyz", "(Ljava/lang/String;)V")).reachable);
assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "abc", "(Ljava/lang/String;)V")).reachable);
assertFalse(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "unused", "(Ljava/lang/String;)V")).reachable);
assertFalse(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "unused2", "(Ljava/lang/String;)V")).reachable);
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class UnusedCodeTrackerTest method testUnusedMethodsCycle.
public void testUnusedMethodsCycle() throws IOException {
String source = "class A {\n" + " private static void abc(String s) {}\n" + " private static void xyz(String s) {abc(\"goo\");}\n" + " private static void foo(String s) {xyz(\"woo\");}\n" + " private static void unused2(String s) {unused(\"boo\");}\n" + " private static void unused(String s) {unused2(\"boo\");}\n" + " static { foo(\"zoo\"); }\n" + "}\n";
CompilationUnit unit = compileType("test", source);
final HashMap<String, ReferenceNode> elementMap = new HashMap<>();
final HashMap<String, Set<String>> overrideMap = new HashMap<>();
final Set<String> staticSet = new HashSet<>();
ElementReferenceMapper mapper = new ElementReferenceMapper(unit, elementMap, staticSet, overrideMap);
mapper.run();
UnusedCodeTracker tracker = new UnusedCodeTracker(unit.getEnv(), elementMap, staticSet, overrideMap);
tracker.markUsedElements();
assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "foo", "(Ljava/lang/String;)V")).reachable);
assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "xyz", "(Ljava/lang/String;)V")).reachable);
assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "abc", "(Ljava/lang/String;)V")).reachable);
assertFalse(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "unused", "(Ljava/lang/String;)V")).reachable);
assertFalse(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "unused2", "(Ljava/lang/String;)V")).reachable);
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class UnusedCodeTrackerTest method testUnusedSubclassInCodeReferenceMap.
public void testUnusedSubclassInCodeReferenceMap() throws IOException {
String source = "class A {\n" + " static { B.abc(\"zoo\"); }\n" + " static class B {\n" + " public static void abc(String s) {}\n" + " }\n" + " class C {\n" + " public void xyz(String s) {}\n" + " }\n" + "}\n";
CompilationUnit unit = compileType("test", source);
final HashMap<String, ReferenceNode> elementMap = new HashMap<>();
final HashMap<String, Set<String>> overrideMap = new HashMap<>();
final Set<String> staticSet = new HashSet<>();
ElementReferenceMapper mapper = new ElementReferenceMapper(unit, elementMap, staticSet, overrideMap);
mapper.run();
UnusedCodeTracker tracker = new UnusedCodeTracker(unit.getEnv(), elementMap, staticSet, overrideMap);
tracker.markUsedElements();
CodeReferenceMap unusedCodeMap = tracker.buildTreeShakerMap();
assertTrue(unusedCodeMap.containsClass("A$C"));
assertTrue(unusedCodeMap.containsMethod("A$C", "xyz", "(Ljava/lang/String;)V"));
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class ElementReferenceMapperTest method testMethod_AnonymousClassMemberReference.
public void testMethod_AnonymousClassMemberReference() throws IOException {
String source = "abstract class B { public void foo() {} }\n" + "class A {\n" + " public void launch() {" + " final B b = new B() {\n" + " @Override" + " public void foo() {}\n" + " };\n" + " b.foo();\n" + " }\n" + " static { new A().launch(); }" + "}\n";
CompilationUnit unit = compileType("test", source);
final HashMap<String, ReferenceNode> elementReferenceMap = new HashMap<>();
final HashMap<String, Set<String>> overrideMap = new HashMap<>();
final Set<String> staticSet = new HashSet<>();
ElementReferenceMapper mapper = new ElementReferenceMapper(unit, elementReferenceMap, staticSet, overrideMap);
mapper.run();
Set<String> elementSet = elementReferenceMap.keySet();
assertTrue(elementSet.contains(ElementReferenceMapper.stitchClassIdentifier("A")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchClassIdentifier("B")));
//TODO(malvania): Enable testing for unused fields when ElementUtil glitch is fixed
//assertTrue(elementSet.contains(ElementReferenceMapper.stitchFieldIdentifier("A", "b")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A$1", "foo", "()V")));
}
Aggregations