use of com.google.devtools.j2objc.util.CodeReferenceMap in project j2objc by google.
the class UnusedCodeTrackerTest method testInputMethodsKept.
public void testInputMethodsKept() throws IOException {
String source = "class A {\n" + " private static void abc(String s) {}\n" + " private static void xyz(String s) {}\n" + " private static void foo(String s) {xyz(\"woo\");}\n" + "}\n";
Builder builder = CodeReferenceMap.builder();
builder.addMethod("A", "abc", "(Ljava/lang/String;)V");
builder.addMethod("A", "xyz", "(Ljava/lang/String;)V");
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);
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.j2objc.util.CodeReferenceMap in project j2objc by google.
the class UnusedCodeTrackerTest method testSuperConstructorInvocation.
public void testSuperConstructorInvocation() throws IOException {
String source = "class A {\n" + " public A() {this(true);}" + " public A(boolean b) {bar = 0;}" + " public A(int i) {bar = i;}" + " private int bar = 1;\n" + "}\n" + "class B extends A {\n" + " public B() {super(1);}\n" + " static {new B();}" + "}\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>", "(Z)V")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "<init>", "(I)V")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("B", "<init>", "()V")));
assertFalse(unusedCodeMap.containsClass("A"));
assertFalse(unusedCodeMap.containsMethod("B", "<init>", "()V"));
assertFalse(unusedCodeMap.containsMethod("A", "<init>", "(I)V"));
assertTrue(unusedCodeMap.containsMethod("A", "<init>", "(Z)V"));
assertTrue(unusedCodeMap.containsMethod("A", "<init>", "()V"));
}
Aggregations