use of com.google.devtools.j2objc.util.CodeReferenceMap.Builder 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.j2objc.util.CodeReferenceMap.Builder in project j2objc by google.
the class TreeShakerTest method testWithPublicRootSet.
public void testWithPublicRootSet() throws IOException {
addSourceFile("A.java", "class A { public void launch() { new B().abc(\"zoo\"); } }");
addSourceFile("B.java", "class B { public void abc(String s) {} }");
addSourceFile("C.java", "class C { public void xyz(String s) {} }");
CodeReferenceMap rootSet = new Builder().addClass("A").build();
CodeReferenceMap unusedCodeMap = getUnusedCode(rootSet);
assertFalse(unusedCodeMap.containsClass("A"));
assertFalse(unusedCodeMap.containsClass("B"));
assertFalse(unusedCodeMap.containsMethod("B", "abc", "(Ljava/lang/String;)V"));
assertTrue(unusedCodeMap.containsClass("C"));
assertTrue(unusedCodeMap.containsMethod("C", "xyz", "(Ljava/lang/String;)V"));
}
use of com.google.devtools.j2objc.util.CodeReferenceMap.Builder 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);
}
Aggregations