use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class ElementReferenceMapperTest method testMethodTraversal.
public void testMethodTraversal() 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 bar(String s) {foo(\"boo\");}\n" + " static { bar(\"zoo\"); }\n" + "}\n";
CompilationUnit unit = compileType("test", source);
final HashMap<String, ReferenceNode> elementReferenceMap = new HashMap<>();
final HashMap<String, Set<String>> overrideMap = new HashMap<>();
final Set<String> staticSet = new HashSet<>();
ElementReferenceMapper mapper = new ElementReferenceMapper(unit, elementReferenceMap, staticSet, overrideMap);
mapper.run();
Set<String> elementSet = elementReferenceMap.keySet();
assertTrue(elementSet.contains(ElementReferenceMapper.stitchClassIdentifier("A")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "abc", "(Ljava/lang/String;)V")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "xyz", "(Ljava/lang/String;)V")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "foo", "(Ljava/lang/String;)V")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "bar", "(Ljava/lang/String;)V")));
assertFalse(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "falseCase", "(Ljava/lang/String;)V")));
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class UnusedCodeTrackerTest method testChainedMethodCalls.
public void testChainedMethodCalls() throws IOException {
String source = "class A {\n" + " public static void foo() {}\n" + " public static void launch() {new D().xyz().bar().abc().foo();\n" + " new E();}\n" + " static {launch();}" + "}\n" + "class B {\n" + " public static A abc() {return new A();}\n" + "}\n" + "class C {\n" + " public static B bar() {return new B();}\n" + "}\n" + "class D {\n" + " public static C xyz() {return new C();}\n" + "}\n" + "class E {\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();
Set<String> elementSet = elementMap.keySet();
UnusedCodeTracker tracker = new UnusedCodeTracker(unit.getEnv(), elementMap, staticSet, overrideMap);
tracker.markUsedElements();
assertTrue(elementSet.contains(ElementReferenceMapper.stitchClassIdentifier("A")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchClassIdentifier("B")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchClassIdentifier("C")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchClassIdentifier("D")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchClassIdentifier("E")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "<init>", "()V")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("B", "<init>", "()V")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("C", "<init>", "()V")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("D", "<init>", "()V")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "foo", "()V")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("B", "abc", "()LA;")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("C", "bar", "()LB;")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("D", "xyz", "()LC;")));
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class UnusedCodeTrackerTest method testConstructorInvocation.
public void testConstructorInvocation() 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 {\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>", "(Z)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"));
assertFalse(unusedCodeMap.containsMethod("A", "<init>", "(Z)V"));
assertTrue(unusedCodeMap.containsMethod("A", "<init>", "(I)V"));
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class UnusedCodeTrackerTest method testUsedStaticlyCalledConstructor.
//TODO(malvania): Enable testing for unused fields when ElementUtil glitch is fixed and fields
// are tracked again. (See ElementReferenceMapper line 183, fields comment.)
//public void testUnusedField() throws IOException {
// String source = "import static java.lang.System.out;\n"
// + "import static java.lang.System.in;\n"
// + "class A {\n"
// + " private static final int foo = 1;\n"
// + " public static final String bar = \"bar\";\n"
// + " static final double pi = 3.2; // in Indiana only\n"
// + " final String baz = null, bah = \"123\";\n"
// + " private static int abc = 9;\n"
// + " private static void foo() {abc = 10;}"
// + " 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();
//
// assertTrue(elementSet.contains(ElementReferenceMapper.stitchClassIdentifier("A")));
// assertTrue(elementSet.contains(ElementReferenceMapper.stitchFieldIdentifier("A", "foo")));
// assertTrue(elementSet.contains(ElementReferenceMapper.stitchFieldIdentifier("A", "bar")));
// assertTrue(elementSet.contains(ElementReferenceMapper.stitchFieldIdentifier("A", "pi")));
// assertTrue(elementSet.contains(ElementReferenceMapper.stitchFieldIdentifier("A", "baz")));
// assertTrue(elementSet.contains(ElementReferenceMapper.stitchFieldIdentifier("A", "bah")));
// assertTrue(elementSet.contains(ElementReferenceMapper.stitchFieldIdentifier("A", "abc")));
//
// //TODO(malvania): Add necessary checks after visiting FieldAccess to check used/unused fields
//}
public void testUsedStaticlyCalledConstructor() throws IOException {
String source = "class A {\n" + " public A() {bar = 2;}" + " private int bar = 1;\n" + "}\n" + "class B {\n" + " public static void foo(A a) {}\n" + " static {foo(new A());}" + "}\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"));
assertFalse(unusedCodeMap.containsMethod("B", "foo", "(LA;)V"));
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class UnusedCodeTrackerTest method testUsedNonStaticNestedClasses.
public void testUsedNonStaticNestedClasses() throws IOException {
String source = "class A {\n" + " static { new A().new B().abc(\"zoo\"); }\n" + " class B {\n" + " public 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();
assertTrue(elementMap.get(ElementReferenceMapper.stitchClassIdentifier("A")).reachable);
assertTrue(elementMap.get(ElementReferenceMapper.stitchClassIdentifier("A$B")).reachable);
assertFalse(elementMap.get(ElementReferenceMapper.stitchClassIdentifier("A$C")).reachable);
assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A$B", "abc", "(Ljava/lang/String;)V")).reachable);
assertFalse(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A$C", "xyz", "(Ljava/lang/String;)V")).reachable);
}
Aggregations