use of com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode in project j2objc by google.
the class UnusedCodeTrackerTest method testUnusedConstructor.
public void testUnusedConstructor() throws IOException {
String source = "class A {\n" + " public A() {bar = 2;}" + " public A(int i) {bar = i;}" + " private int bar = 1;\n" + "}\n" + "class B {\n" + " public static void foo() {new A();}\n" + " static {foo();}" + "}\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();
Set<String> elementSet = elementMap.keySet();
UnusedCodeTracker tracker = new UnusedCodeTracker(unit.getEnv(), elementMap, staticSet, overrideMap);
tracker.markUsedElements();
CodeReferenceMap unusedCodeMap = tracker.buildTreeShakerMap();
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "<init>", "()V")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "<init>", "(I)V")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("B", "foo", "()V")));
assertFalse(unusedCodeMap.containsClass("A"));
assertFalse(unusedCodeMap.containsMethod("B", "foo", "()V"));
assertFalse(unusedCodeMap.containsMethod("A", "<init>", "()V"));
assertTrue(unusedCodeMap.containsMethod("A", "<init>", "(I)V"));
}
use of com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode in project j2objc by google.
the class UnusedCodeTrackerTest method testPublicMethodsInInputClasses.
public void testPublicMethodsInInputClasses() throws IOException {
String source = "class A {\n" + " public static void abc(String s) {}\n" + " public static void xyz(String s) {}\n" + " private static void foo(String s) {xyz(\"woo\");}\n" + "}\n";
Builder builder = CodeReferenceMap.builder();
builder.addClass("A");
CodeReferenceMap inputCodeMap = builder.build();
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(inputCodeMap);
CodeReferenceMap unusedCodeMap = tracker.buildTreeShakerMap();
assertFalse(unusedCodeMap.containsClass("A"));
assertFalse(unusedCodeMap.containsMethod("A", "abc", "(Ljava/lang/String;)V"));
assertFalse(unusedCodeMap.containsMethod("A", "xyz", "(Ljava/lang/String;)V"));
assertTrue(unusedCodeMap.containsMethod("A", "foo", "(Ljava/lang/String;)V"));
assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "abc", "(Ljava/lang/String;)V")).reachable);
assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "xyz", "(Ljava/lang/String;)V")).reachable);
assertFalse(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "foo", "(Ljava/lang/String;)V")).reachable);
}
use of com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode in project j2objc by google.
the class UnusedCodeTrackerTest method testUnusedClass.
//Check for assumptions: In this case, a static block's class does not get marked as true
//TODO(malvania): Add static block used test
public void testUnusedClass() throws IOException {
String source = "class A {\n" + " public static void abc(String s) {}\n" + "}" + "class B {\n" + " static { A.abc(\"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.stitchClassIdentifier("A")).reachable);
assertFalse(elementMap.get(ElementReferenceMapper.stitchClassIdentifier("B")).reachable);
assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "abc", "(Ljava/lang/String;)V")).reachable);
}
use of com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode in project j2objc by google.
the class UnusedCodeTrackerTest method testUnusedMethod.
public void testUnusedMethod() 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 unused(String s) {foo(\"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);
}
use of com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode 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"));
}
Aggregations