Search in sources :

Example 31 with CodeReferenceMap

use of com.google.devtools.j2objc.util.CodeReferenceMap 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 32 with CodeReferenceMap

use of com.google.devtools.j2objc.util.CodeReferenceMap 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)

Aggregations

CodeReferenceMap (com.google.devtools.j2objc.util.CodeReferenceMap)32 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)9 ReferenceNode (com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode)9 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)9 Set (java.util.Set)9 Builder (com.google.devtools.j2objc.util.CodeReferenceMap.Builder)3 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 File (java.io.File)1 IOException (java.io.IOException)1