use of com.google.devtools.j2objc.util.CodeReferenceMap 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 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.util.CodeReferenceMap 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.util.CodeReferenceMap in project j2objc by google.
the class DeadCodeEliminatorTest method testDeadClass_SupertypeRemoval.
public void testDeadClass_SupertypeRemoval() throws IOException {
CodeReferenceMap map = CodeReferenceMap.builder().addClass("Foo").build();
setDeadCodeMap(map);
addSourceFile("class SuperClass {}", "SuperClass.java");
addSourceFile("interface SuperI { void f(); }", "SuperI.java");
String source = "class Foo extends SuperClass implements SuperI {\n" + " public void f() {}\n" + "}\n";
String translation = translateSourceFile(source, "Foo", "Foo.h");
assertNotInTranslation(translation, "SuperClass");
assertNotInTranslation(translation, "SuperI");
}
use of com.google.devtools.j2objc.util.CodeReferenceMap in project j2objc by google.
the class DeadCodeEliminatorTest method testConstructorGeneration.
public void testConstructorGeneration() throws IOException {
CodeReferenceMap map = CodeReferenceMap.builder().addClass("A").addMethod("C", "<init>", "(I)V").build();
setDeadCodeMap(map);
addSourceFile("class B {\n" + " public B(int x, boolean y, String z, java.util.List w) {}\n" + "}", "B.java");
String translation = translateSourceFile("class A extends B {\n" + " public A(int i) { super(i, true, \"foo\", new java.util.ArrayList()); }\n" + "}\n", "A", "A.m");
assertNotInTranslation(translation, "initWithInt");
assertNotInTranslation(translation, "B_init");
translation = translateSourceFile("class C extends B {\n" + " public C(int i) { super(i, true, \"foo\", new java.util.ArrayList()); }\n" + "}\n", "C", "C.m");
assertNotInTranslation(translation, "initWithInt");
assertNotInTranslation(translation, "B_init");
}
Aggregations