Search in sources :

Example 26 with CodeReferenceMap

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

the class TreeShakerTest method getUnusedCode.

private CodeReferenceMap getUnusedCode(CodeReferenceMap rootSetMap) throws IOException {
    Options options = new Options();
    options.setSourceFiles(inputFiles);
    options.setClasspath(System.getProperty("java.class.path"));
    TreeShaker shaker = new TreeShaker(options);
    CodeReferenceMap map = shaker.getUnusedCode(rootSetMap);
    if (ErrorUtil.errorCount() > 0) {
        fail("TreeShaker failed with errors:\n" + Joiner.on("\n").join(ErrorUtil.getErrorMessages()));
    }
    return map;
}
Also used : CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap)

Example 27 with CodeReferenceMap

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

the class TreeShakerTest method testNoPublicRootSet.

public void testNoPublicRootSet() 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 unusedCodeMap = getUnusedCode();
    assertTrue(unusedCodeMap.containsClass("A"));
    assertTrue(unusedCodeMap.containsClass("B"));
    assertTrue(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)

Example 28 with CodeReferenceMap

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

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

the class UnusedCodeTrackerTest method testConstructorInvocation.

public void testConstructorInvocation() 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 {\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>", "(Z)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"));
    assertFalse(unusedCodeMap.containsMethod("A", "<init>", "(Z)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 30 with CodeReferenceMap

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

the class UnusedCodeTrackerTest method testUsedStaticlyCalledConstructor.

//TODO(malvania): Enable testing for unused fields when ElementUtil glitch is fixed and fields
//                are tracked again. (See ElementReferenceMapper line 183, fields comment.)
//public void testUnusedField() throws IOException {
//  String source = "import static java.lang.System.out;\n"
//      + "import static java.lang.System.in;\n"
//      + "class A {\n"
//      + "  private static final int foo = 1;\n"
//      + "  public static final String bar = \"bar\";\n"
//      + "  static final double pi = 3.2; // in Indiana only\n"
//      + "  final String baz = null, bah = \"123\";\n"
//      + "  private static int abc = 9;\n"
//      + "  private static void foo() {abc = 10;}"
//      + "  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();
//
//  assertTrue(elementSet.contains(ElementReferenceMapper.stitchClassIdentifier("A")));
//  assertTrue(elementSet.contains(ElementReferenceMapper.stitchFieldIdentifier("A", "foo")));
//  assertTrue(elementSet.contains(ElementReferenceMapper.stitchFieldIdentifier("A", "bar")));
//  assertTrue(elementSet.contains(ElementReferenceMapper.stitchFieldIdentifier("A", "pi")));
//  assertTrue(elementSet.contains(ElementReferenceMapper.stitchFieldIdentifier("A", "baz")));
//  assertTrue(elementSet.contains(ElementReferenceMapper.stitchFieldIdentifier("A", "bah")));
//  assertTrue(elementSet.contains(ElementReferenceMapper.stitchFieldIdentifier("A", "abc")));
//
//  //TODO(malvania): Add necessary checks after visiting FieldAccess to check used/unused fields
//}
public void testUsedStaticlyCalledConstructor() throws IOException {
    String source = "class A {\n" + "  public A() {bar = 2;}" + "  private int bar = 1;\n" + "}\n" + "class B {\n" + "  public static void foo(A a) {}\n" + "  static {foo(new A());}" + "}\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"));
    assertFalse(unusedCodeMap.containsMethod("B", "foo", "(LA;)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