Search in sources :

Example 16 with ReferenceNode

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

the class ElementReferenceMapperTest method testInitializerReference.

//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 testFieldsReference() 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 int abc = 9;\n"
//      + "}\n";
//
//  CompilationUnit unit = compileType("test", source);
//  final HashMap<String, ReferenceNode> elementReferenceMap = new HashMap<>();
//  final HashMap<String, Set<String>> overrideMap = new HashMap<>();
//  final Set<String> staticSet = new HashSet<>();
//  ElementReferenceMapper mapper = new ElementReferenceMapper(unit, elementReferenceMap,
//  staticSet, overrideMap);
//  mapper.run();
//  Set<String> elementSet = elementReferenceMap.keySet();
//
//  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")));
//}
public void testInitializerReference() throws IOException {
    String source = "class A {\n" + "  static final int baz = 9;\n" + "  static { System.out.println(\"foo\"); }\n" + "  { System.out.println(\"bar\"); }\n" + "}\n";
    CompilationUnit unit = compileType("test", source);
    final HashMap<String, ReferenceNode> elementReferenceMap = new HashMap<>();
    final HashMap<String, Set<String>> overrideMap = new HashMap<>();
    final Set<String> staticSet = new HashSet<>();
    ElementReferenceMapper mapper = new ElementReferenceMapper(unit, elementReferenceMap, staticSet, overrideMap);
    mapper.run();
    Set<String> elementSet = elementReferenceMap.keySet();
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchClassIdentifier("A")));
//TODO(malvania): Enable testing for unused fields when ElementUtil glitch is fixed
//assertTrue(elementSet.contains(ElementReferenceMapper.stitchFieldIdentifier("A", "baz")));
}
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) MethodReferenceNode(com.google.devtools.treeshaker.ElementReferenceMapper.MethodReferenceNode) HashSet(java.util.HashSet)

Example 17 with ReferenceNode

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

the class ElementReferenceMapperTest method testMethodTraversal.

public void testMethodTraversal() 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 bar(String s) {foo(\"boo\");}\n" + "  static { bar(\"zoo\"); }\n" + "}\n";
    CompilationUnit unit = compileType("test", source);
    final HashMap<String, ReferenceNode> elementReferenceMap = new HashMap<>();
    final HashMap<String, Set<String>> overrideMap = new HashMap<>();
    final Set<String> staticSet = new HashSet<>();
    ElementReferenceMapper mapper = new ElementReferenceMapper(unit, elementReferenceMap, staticSet, overrideMap);
    mapper.run();
    Set<String> elementSet = elementReferenceMap.keySet();
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchClassIdentifier("A")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "abc", "(Ljava/lang/String;)V")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "xyz", "(Ljava/lang/String;)V")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "foo", "(Ljava/lang/String;)V")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "bar", "(Ljava/lang/String;)V")));
    assertFalse(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "falseCase", "(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) MethodReferenceNode(com.google.devtools.treeshaker.ElementReferenceMapper.MethodReferenceNode) HashSet(java.util.HashSet)

Example 18 with ReferenceNode

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

the class UnusedCodeTrackerTest method testChainedMethodCalls.

public void testChainedMethodCalls() throws IOException {
    String source = "class A {\n" + "  public static void foo() {}\n" + "  public static void launch() {new D().xyz().bar().abc().foo();\n" + "     new E();}\n" + "  static {launch();}" + "}\n" + "class B {\n" + "  public static A abc() {return new A();}\n" + "}\n" + "class C {\n" + "  public static B bar() {return new B();}\n" + "}\n" + "class D {\n" + "  public static C xyz() {return new C();}\n" + "}\n" + "class E {\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();
    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.stitchClassIdentifier("B")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchClassIdentifier("C")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchClassIdentifier("D")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchClassIdentifier("E")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "<init>", "()V")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("B", "<init>", "()V")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("C", "<init>", "()V")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("D", "<init>", "()V")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "foo", "()V")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("B", "abc", "()LA;")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("C", "bar", "()LB;")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("D", "xyz", "()LC;")));
}
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 19 with ReferenceNode

use of com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode 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 20 with ReferenceNode

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

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