Search in sources :

Example 1 with Builder

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

use of com.google.devtools.j2objc.util.CodeReferenceMap.Builder in project j2objc by google.

the class TreeShakerTest method testWithPublicRootSet.

public void testWithPublicRootSet() throws IOException {
    addSourceFile("A.java", "class A { public void launch() { new B().abc(\"zoo\"); } }");
    addSourceFile("B.java", "class B { public void abc(String s) {} }");
    addSourceFile("C.java", "class C { public void xyz(String s) {} }");
    CodeReferenceMap rootSet = new Builder().addClass("A").build();
    CodeReferenceMap unusedCodeMap = getUnusedCode(rootSet);
    assertFalse(unusedCodeMap.containsClass("A"));
    assertFalse(unusedCodeMap.containsClass("B"));
    assertFalse(unusedCodeMap.containsMethod("B", "abc", "(Ljava/lang/String;)V"));
    assertTrue(unusedCodeMap.containsClass("C"));
    assertTrue(unusedCodeMap.containsMethod("C", "xyz", "(Ljava/lang/String;)V"));
}
Also used : CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap) Builder(com.google.devtools.j2objc.util.CodeReferenceMap.Builder)

Example 3 with Builder

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

Aggregations

CodeReferenceMap (com.google.devtools.j2objc.util.CodeReferenceMap)3 Builder (com.google.devtools.j2objc.util.CodeReferenceMap.Builder)3 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)2 ReferenceNode (com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2