Search in sources :

Example 51 with CompilationUnit

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

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

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

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

Example 55 with CompilationUnit

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

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