Search in sources :

Example 6 with CodeReferenceMap

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

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

Example 8 with CodeReferenceMap

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

the class UnusedCodeTrackerTest method testUnusedSubclassInCodeReferenceMap.

public void testUnusedSubclassInCodeReferenceMap() 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();
    CodeReferenceMap unusedCodeMap = tracker.buildTreeShakerMap();
    assertTrue(unusedCodeMap.containsClass("A$C"));
    assertTrue(unusedCodeMap.containsMethod("A$C", "xyz", "(Ljava/lang/String;)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 9 with CodeReferenceMap

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

the class DeadCodeEliminatorTest method testDeadClass_SupertypeRemoval.

public void testDeadClass_SupertypeRemoval() throws IOException {
    CodeReferenceMap map = CodeReferenceMap.builder().addClass("Foo").build();
    setDeadCodeMap(map);
    addSourceFile("class SuperClass {}", "SuperClass.java");
    addSourceFile("interface SuperI { void f(); }", "SuperI.java");
    String source = "class Foo extends SuperClass implements SuperI {\n" + "  public void f() {}\n" + "}\n";
    String translation = translateSourceFile(source, "Foo", "Foo.h");
    assertNotInTranslation(translation, "SuperClass");
    assertNotInTranslation(translation, "SuperI");
}
Also used : CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap)

Example 10 with CodeReferenceMap

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

the class DeadCodeEliminatorTest method testConstructorGeneration.

public void testConstructorGeneration() throws IOException {
    CodeReferenceMap map = CodeReferenceMap.builder().addClass("A").addMethod("C", "<init>", "(I)V").build();
    setDeadCodeMap(map);
    addSourceFile("class B {\n" + "  public B(int x, boolean y, String z, java.util.List w) {}\n" + "}", "B.java");
    String translation = translateSourceFile("class A extends B {\n" + "  public A(int i) { super(i, true, \"foo\", new java.util.ArrayList()); }\n" + "}\n", "A", "A.m");
    assertNotInTranslation(translation, "initWithInt");
    assertNotInTranslation(translation, "B_init");
    translation = translateSourceFile("class C extends B {\n" + "  public C(int i) { super(i, true, \"foo\", new java.util.ArrayList()); }\n" + "}\n", "C", "C.m");
    assertNotInTranslation(translation, "initWithInt");
    assertNotInTranslation(translation, "B_init");
}
Also used : CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap)

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