Search in sources :

Example 21 with ReferenceNode

use of com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode 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);
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ReferenceNode(com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode) HashSet(java.util.HashSet)

Example 22 with ReferenceNode

use of com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode 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);
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) HashSet(java.util.HashSet) Set(java.util.Set) CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap) HashMap(java.util.HashMap) ReferenceNode(com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode) Builder(com.google.devtools.j2objc.util.CodeReferenceMap.Builder) HashSet(java.util.HashSet)

Example 23 with ReferenceNode

use of com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode 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"));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ReferenceNode(com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode) CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap) HashSet(java.util.HashSet)

Example 24 with ReferenceNode

use of com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode in project j2objc by google.

the class UnusedCodeTrackerTest method testUsedStaticNestedClasses.

public void testUsedStaticNestedClasses() 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();
    assertFalse(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);
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ReferenceNode(com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode) HashSet(java.util.HashSet)

Aggregations

ReferenceNode (com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode)24 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)23 HashMap (java.util.HashMap)23 HashSet (java.util.HashSet)23 Set (java.util.Set)23 CodeReferenceMap (com.google.devtools.j2objc.util.CodeReferenceMap)9 MethodReferenceNode (com.google.devtools.treeshaker.ElementReferenceMapper.MethodReferenceNode)8 Builder (com.google.devtools.j2objc.util.CodeReferenceMap.Builder)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 RegularInputFile (com.google.devtools.j2objc.file.RegularInputFile)1 Parser (com.google.devtools.j2objc.util.Parser)1 ProGuardUsageParser (com.google.devtools.j2objc.util.ProGuardUsageParser)1 ClassReferenceNode (com.google.devtools.treeshaker.ElementReferenceMapper.ClassReferenceNode)1 File (java.io.File)1