use of com.google.devtools.j2objc.util.CodeReferenceMap in project j2objc by google.
the class DeadCodeEliminatorTest method testDeadEnum.
public void testDeadEnum() throws IOException {
CodeReferenceMap map = CodeReferenceMap.builder().addClass("A$Thing").build();
setDeadCodeMap(map);
String source = "class A {\n" + " private static void foo() {}\n" + " public enum Thing implements java.io.Serializable {\n" + " THING1(27),\n" + " THING2(89) { void bar() {} },\n" + " THING3 { void bar() { foo(); } };\n" + " private Thing(int x) {}\n" + " private Thing() {}\n" + " }\n" + "}\n";
String translation = translateSourceFile(source, "A", "A.m");
assertNotInTranslation(translation, "initWithInt");
assertNotInTranslation(translation, "THING1");
assertNotInTranslation(translation, "THING2");
assertNotInTranslation(translation, "THING3");
String header = getTranslatedFile("A.h");
assertNotInTranslation(header, "Serializable");
}
use of com.google.devtools.j2objc.util.CodeReferenceMap in project j2objc by google.
the class DeadCodeEliminatorTest method testDeadMethod_InnerClassConstructor.
public void testDeadMethod_InnerClassConstructor() throws IOException {
String source = "class A {\n" + " class B {\n" + " B(int i) {}\n" + " }\n" + "}\n";
CodeReferenceMap map = CodeReferenceMap.builder().addMethod("A$B", "A$B", "(LA;I)V").build();
setDeadCodeMap(map);
String translation = translateSourceFile(source, "A", "A.m");
assertNotInTranslation(translation, "withInt");
}
use of com.google.devtools.j2objc.util.CodeReferenceMap in project j2objc by google.
the class DeadCodeEliminatorTest method testTypeNarrowingMethodsNotShowingInDeadClasses.
public void testTypeNarrowingMethodsNotShowingInDeadClasses() throws IOException {
String source = "class Base<T> { \n" + " T someMethod() { return null; }\n" + "}\n" + "class Foo extends Base<String> {}";
CodeReferenceMap map = CodeReferenceMap.builder().addClass("Foo").build();
setDeadCodeMap(map);
String header = translateSourceFile(source, "Foo", "Foo.h");
assertNotInTranslation(header, "@interface Foo : Base");
assertTranslation(header, "@interface Foo : NSObject");
assertTranslation(header, "- (id)someMethod;");
assertNotInTranslation(header, "- (NSString *)someMethod;");
}
use of com.google.devtools.j2objc.util.CodeReferenceMap in project j2objc by google.
the class DeadCodeEliminatorTest method testDeadClass_FieldRemoval.
public void testDeadClass_FieldRemoval() throws IOException {
CodeReferenceMap map = CodeReferenceMap.builder().addClass("Foo").build();
setDeadCodeMap(map);
String source = "class Foo {\n" + " static final int x = f();\n" + " static final int y = 0;\n" + " static int f() { return 0; }\n" + "}\n";
String translation = translateSourceFile(source, "Foo", "Foo.h");
assertTranslation(translation, "#define Foo_y 0");
translation = getTranslatedFile("Foo.m");
assertNotInTranslation(translation, "jint Foo_x_");
assertNotInTranslation(translation, "Foo_x_ = Foo_f()");
assertNotInTranslation(translation, "+ (jint)f");
assertNotInTranslation(translation, "bar");
}
use of com.google.devtools.j2objc.util.CodeReferenceMap 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"));
}
Aggregations