Search in sources :

Example 11 with CompilationUnit

use of com.google.devtools.j2objc.ast.CompilationUnit 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 12 with CompilationUnit

use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.

the class UnusedCodeTrackerTest method testUnusedMethodCallingMethods.

public void testUnusedMethodCallingMethods() 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 unused2(String s) {foo(\"boo\");}\n" + "  private static void unused(String s) {unused2(\"boo\");}\n" + "  static { foo(\"zoo\"); }\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();
    assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "foo", "(Ljava/lang/String;)V")).reachable);
    assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "xyz", "(Ljava/lang/String;)V")).reachable);
    assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "abc", "(Ljava/lang/String;)V")).reachable);
    assertFalse(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "unused", "(Ljava/lang/String;)V")).reachable);
    assertFalse(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "unused2", "(Ljava/lang/String;)V")).reachable);
}
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 13 with CompilationUnit

use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.

the class UnusedCodeTrackerTest method testUnusedMethodsCycle.

public void testUnusedMethodsCycle() 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 unused2(String s) {unused(\"boo\");}\n" + "  private static void unused(String s) {unused2(\"boo\");}\n" + "  static { foo(\"zoo\"); }\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();
    assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "foo", "(Ljava/lang/String;)V")).reachable);
    assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "xyz", "(Ljava/lang/String;)V")).reachable);
    assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "abc", "(Ljava/lang/String;)V")).reachable);
    assertFalse(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "unused", "(Ljava/lang/String;)V")).reachable);
    assertFalse(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "unused2", "(Ljava/lang/String;)V")).reachable);
}
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 14 with CompilationUnit

use of com.google.devtools.j2objc.ast.CompilationUnit 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 15 with CompilationUnit

use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.

the class ElementReferenceMapperTest method testMethod_AnonymousClassMemberReference.

public void testMethod_AnonymousClassMemberReference() throws IOException {
    String source = "abstract class B { public void foo() {} }\n" + "class A {\n" + "  public void launch() {" + "    final B b = new B() {\n" + "      @Override" + "      public void foo() {}\n" + "    };\n" + "    b.foo();\n" + "  }\n" + "  static { new A().launch(); }" + "}\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.stitchClassIdentifier("B")));
    //TODO(malvania): Enable testing for unused fields when ElementUtil glitch is fixed
    //assertTrue(elementSet.contains(ElementReferenceMapper.stitchFieldIdentifier("A", "b")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A$1", "foo", "()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)

Aggregations

CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)71 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)24 HashSet (java.util.HashSet)24 ReferenceNode (com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode)23 HashMap (java.util.HashMap)23 Set (java.util.Set)23 CodeReferenceMap (com.google.devtools.j2objc.util.CodeReferenceMap)9 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)7 MethodReferenceNode (com.google.devtools.treeshaker.ElementReferenceMapper.MethodReferenceNode)7 TypeElement (javax.lang.model.element.TypeElement)5 TreeVisitor (com.google.devtools.j2objc.ast.TreeVisitor)4 NameTable (com.google.devtools.j2objc.util.NameTable)4 TreeNode (com.google.devtools.j2objc.ast.TreeNode)3 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)3 RegularInputFile (com.google.devtools.j2objc.file.RegularInputFile)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 TypeMirror (javax.lang.model.type.TypeMirror)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)2 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)2