Search in sources :

Example 1 with ReferenceNode

use of com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode 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"));
}
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 2 with ReferenceNode

use of com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode 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);
}
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 3 with ReferenceNode

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

the class UnusedCodeTrackerTest method testUnusedClass.

//Check for assumptions: In this case, a static block's class does not get marked as true
//TODO(malvania): Add static block used test
public void testUnusedClass() throws IOException {
    String source = "class A {\n" + "  public static void abc(String s) {}\n" + "}" + "class B {\n" + "  static { A.abc(\"zoo\"); }\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);
    assertFalse(elementMap.get(ElementReferenceMapper.stitchClassIdentifier("B")).reachable);
    assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "abc", "(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 4 with ReferenceNode

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

the class UnusedCodeTrackerTest method testUnusedMethod.

public void testUnusedMethod() 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 unused(String s) {foo(\"boo\");}\n" + "  static { foo(\"zoo\"); }\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.stitchMethodIdentifier("A", "foo", "(Ljava/lang/String;)V")).reachable);
    assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "xyz", "(Ljava/lang/String;)V")).reachable);
    assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "abc", "(Ljava/lang/String;)V")).reachable);
    assertFalse(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "unused", "(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 5 with ReferenceNode

use of com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode 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"));
}
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)

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