use of com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode 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.treeshaker.ElementReferenceMapper.ReferenceNode 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.treeshaker.ElementReferenceMapper.ReferenceNode 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.treeshaker.ElementReferenceMapper.ReferenceNode 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")));
}
use of com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode in project j2objc by google.
the class ElementReferenceMapperTest method testMethodInvocation.
public void testMethodInvocation() throws IOException {
String source = "class A {\n" + " private static void foo(String s) {final int bro = 1;}\n" + " private static void bar(String s) {foo(\"boo\");}\n" + " static { bar(\"bro\"); }\n" + "}\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.stitchMethodIdentifier("A", "foo", "(Ljava/lang/String;)V")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "bar", "(Ljava/lang/String;)V")));
assertTrue(((MethodReferenceNode) elementReferenceMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "bar", "(Ljava/lang/String;)V"))).invokedMethods.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "foo", "(Ljava/lang/String;)V")));
}
Aggregations