Search in sources :

Example 1 with MethodReferenceNode

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

the class ElementReferenceMapperTest method testMethodInvocation.

public void testMethodInvocation() throws IOException {
    String source = "class A {\n" + "  private static void foo(String s) {final int bro = 1;}\n" + "  private static void bar(String s) {foo(\"boo\");}\n" + "  static { bar(\"bro\"); }\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", "foo", "(Ljava/lang/String;)V")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "bar", "(Ljava/lang/String;)V")));
    assertTrue(((MethodReferenceNode) elementReferenceMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "bar", "(Ljava/lang/String;)V"))).invokedMethods.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "foo", "(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) MethodReferenceNode(com.google.devtools.treeshaker.ElementReferenceMapper.MethodReferenceNode) HashSet(java.util.HashSet)

Example 2 with MethodReferenceNode

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

the class UnusedCodeTracker method traverseMethod.

/**
   * Traverses the method invocation graph created by ElementReferenceMapper, and marks all methods
   * that are reachable from the inputRootSet. Also covers all methods that possibly override these
   * called methods.
   * @param methodID
   */
public void traverseMethod(String methodID) {
    MethodReferenceNode node = (MethodReferenceNode) elementReferenceMap.get(methodID);
    if (node == null) {
        //TODO(malvania): This might never be reached, because we create a node for every method,
        //                both invoked and declared.
        ErrorUtil.warning("Encountered .class method while accessing: " + methodID);
        return;
    }
    if (node.reachable) {
        return;
    }
    node.reachable = true;
    markParentClasses(ElementUtil.getDeclaringClass(node.methodElement));
    for (String invokedMethodID : node.invokedMethods) {
        traverseMethod(invokedMethodID);
    }
    for (String overrideMethodID : node.overridingMethods) {
        traverseMethod(overrideMethodID);
    }
}
Also used : MethodReferenceNode(com.google.devtools.treeshaker.ElementReferenceMapper.MethodReferenceNode)

Aggregations

MethodReferenceNode (com.google.devtools.treeshaker.ElementReferenceMapper.MethodReferenceNode)2 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)1 ReferenceNode (com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1