Search in sources :

Example 11 with ReferenceNode

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

the class TreeShaker method getUnusedCode.

public CodeReferenceMap getUnusedCode(CodeReferenceMap inputRootSet) throws IOException {
    Parser parser = createParser(options);
    final HashMap<String, ReferenceNode> elementReferenceMap = new HashMap<>();
    final Set<String> staticSet = new HashSet<>();
    final HashMap<String, Set<String>> overrideMap = new HashMap<>();
    List<String> sourceFiles = options.getSourceFiles();
    File strippedDir = stripIncompatible(sourceFiles, parser);
    Parser.Handler handler = new Parser.Handler() {

        @Override
        public void handleParsedUnit(String path, CompilationUnit unit) {
            if (env == null) {
                env = unit.getEnv();
            } else {
            //TODO(malvania): Assertion fails! Remove this once we're sure all env utils are the same.
            //assert(unit.getEnv() == env);
            }
            new ElementReferenceMapper(unit, elementReferenceMap, staticSet, overrideMap).run();
        }
    };
    parser.parseFiles(sourceFiles, handler, options.sourceVersion());
    FileUtil.deleteTempDir(strippedDir);
    if (ErrorUtil.errorCount() > 0) {
        return null;
    }
    UnusedCodeTracker tracker = new UnusedCodeTracker(env, elementReferenceMap, staticSet, overrideMap);
    tracker.mapOverridingMethods();
    tracker.markUsedElements(inputRootSet);
    CodeReferenceMap codeMap = tracker.buildTreeShakerMap();
    return codeMap;
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ReferenceNode(com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode) CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap) ProGuardUsageParser(com.google.devtools.j2objc.util.ProGuardUsageParser) Parser(com.google.devtools.j2objc.util.Parser) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile) File(java.io.File) HashSet(java.util.HashSet)

Example 12 with ReferenceNode

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

the class UnusedCodeTracker method markParentClasses.

/**
   * Mark all ancestor classes of (sub)class as used
   */
public void markParentClasses(TypeElement type) {
    while (type != null) {
        String typeID = ElementReferenceMapper.stitchClassIdentifier(type, env.elementUtil());
        ReferenceNode node = elementReferenceMap.get(typeID);
        if (node == null) {
            ErrorUtil.warning("Encountered .class parent class while accessing: " + typeID);
            return;
        }
        if (node.reachable) {
            return;
        }
        node.reachable = true;
        if (ElementUtil.isStatic(type)) {
            return;
        }
        type = ElementUtil.getDeclaringClass(type);
    }
}
Also used : ReferenceNode(com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode) ClassReferenceNode(com.google.devtools.treeshaker.ElementReferenceMapper.ClassReferenceNode) MethodReferenceNode(com.google.devtools.treeshaker.ElementReferenceMapper.MethodReferenceNode)

Example 13 with ReferenceNode

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

the class ElementReferenceMapperTest method testEnumReference.

public void testEnumReference() throws IOException {
    String source = "class A {\n" + "  private static void foo() {}\n" + "  public enum Thing implements java.io.Serializable {\n" + "    THING1(27),\n" + "    THING2(89) { void bar() {} },\n" + "    THING3 { void bar() { foo(); } };\n" + "    private Thing(int x) {}\n" + "    private Thing() {}\n" + "  }\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.stitchClassIdentifier("A$Thing")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "foo", "()V")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A$Thing", "A$Thing", "()V")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A$Thing", "A$Thing", "(I)V")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A$Thing$1", "bar", "()V")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A$Thing$2", "bar", "()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 14 with ReferenceNode

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

the class ElementReferenceMapperTest method testMethodReference.

public void testMethodReference() throws IOException {
    String source = "class A {\n" + "  private static interface B {\n" + "    String bar();\n" + "  }\n" + "  private void baz() {\n" + "    // nothing\n" + "  }\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.stitchClassIdentifier("A$B")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A$B", "bar", "()Ljava/lang/String;")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "baz", "()V")));
    assertFalse(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "abc", "()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 15 with ReferenceNode

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

the class ElementReferenceMapperTest method testMethod_InnerClassConstructorReference.

public void testMethod_InnerClassConstructorReference() throws IOException {
    String source = "class A {\n" + "  class B {\n" + "    B(int i) {}\n" + "  }\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.stitchClassIdentifier("A$B")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A$B", "A$B", "(LA;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) MethodReferenceNode(com.google.devtools.treeshaker.ElementReferenceMapper.MethodReferenceNode) 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