Search in sources :

Example 21 with CodeReferenceMap

use of com.google.devtools.j2objc.util.CodeReferenceMap in project j2objc by google.

the class DeadCodeEliminatorTest method testDeadInitializer.

public void testDeadInitializer() throws IOException {
    CodeReferenceMap map = CodeReferenceMap.builder().addClass("A").build();
    setDeadCodeMap(map);
    String source = "class A {\n" + "  static final int baz = 9;\n" + "  static { System.out.println(\"foo\"); }\n" + "  { System.out.println(\"bar\"); }\n" + "}\n";
    String translation = translateSourceFile(source, "A", "A.h");
    assertTranslation(translation, "#define A_baz 9");
    translation = getTranslatedFile("A.m");
    assertNotInTranslation(translation, "println");
    assertNotInTranslation(translation, "initialize");
    assertNotInTranslation(translation, "foo");
    assertNotInTranslation(translation, "bar");
}
Also used : CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap)

Example 22 with CodeReferenceMap

use of com.google.devtools.j2objc.util.CodeReferenceMap in project j2objc by google.

the class DeadCodeEliminatorTest method testDeadClass_DeadStaticNestedClass.

public void testDeadClass_DeadStaticNestedClass() throws IOException {
    CodeReferenceMap map = CodeReferenceMap.builder().addClass("Foo").addClass("Foo$Bar").addMethod("Foo$Baz", "g", "()V").build();
    setDeadCodeMap(map);
    String source = "class Foo {\n" + "  static class Bar { void f() {} }\n" + "  static class Baz { void g() {} }\n" + "}\n";
    String translation = translateSourceFile(source, "Foo", "Foo.h");
    assertTranslation(translation, "@interface Foo_Bar");
    assertNotInTranslation(translation, "- (void)f");
    assertTranslation(translation, "@interface Foo_Baz");
    assertNotInTranslation(translation, "- (void)g");
    translation = getTranslatedFile("Foo.m");
    assertNotInTranslation(translation, "Foo_Bar_init");
    assertNotInTranslation(translation, "- (void)f");
    assertTranslation(translation, "Foo_Baz_init");
    assertNotInTranslation(translation, "- (void)g");
}
Also used : CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap)

Example 23 with CodeReferenceMap

use of com.google.devtools.j2objc.util.CodeReferenceMap 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 24 with CodeReferenceMap

use of com.google.devtools.j2objc.util.CodeReferenceMap in project j2objc by google.

the class TreeShaker method main.

public static void main(String[] args) {
    if (args.length == 0) {
        Options.help(true);
    }
    boolean treatWarningsAsErrors = false;
    try {
        Options options = Options.parse(args);
        treatWarningsAsErrors = options.treatWarningsAsErrors();
        TreeShaker finder = new TreeShaker(options);
        finder.testFileExistence();
        exitOnErrorsOrWarnings(treatWarningsAsErrors);
        CodeReferenceMap unusedCodeMap = finder.getUnusedCode(loadRootSetMap(options));
        writeToFile("tree-shaker-report.txt", unusedCodeMap);
    } catch (IOException e) {
        ErrorUtil.error(e.getMessage());
    }
    exitOnErrorsOrWarnings(treatWarningsAsErrors);
}
Also used : CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap) IOException(java.io.IOException)

Example 25 with CodeReferenceMap

use of com.google.devtools.j2objc.util.CodeReferenceMap in project j2objc by google.

the class TreeShakerTest method testUnusedCodeAcrossFiles.

public void testUnusedCodeAcrossFiles() throws IOException {
    addSourceFile("A.java", "class A { static { launch(); }\n" + "public static void launch() { new B().abc(\"zoo\"); } }");
    addSourceFile("B.java", "class B { public void abc(String s) {} }");
    addSourceFile("C.java", "class C { public void xyz(String s) {} }");
    CodeReferenceMap unusedCodeMap = getUnusedCode();
    assertFalse(unusedCodeMap.containsClass("A"));
    assertFalse(unusedCodeMap.containsClass("B"));
    assertFalse(unusedCodeMap.containsMethod("B", "abc", "(Ljava/lang/String;)V"));
    assertTrue(unusedCodeMap.containsClass("C"));
    assertTrue(unusedCodeMap.containsMethod("C", "xyz", "(Ljava/lang/String;)V"));
}
Also used : CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap)

Aggregations

CodeReferenceMap (com.google.devtools.j2objc.util.CodeReferenceMap)32 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)9 ReferenceNode (com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode)9 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)9 Set (java.util.Set)9 Builder (com.google.devtools.j2objc.util.CodeReferenceMap.Builder)3 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 File (java.io.File)1 IOException (java.io.IOException)1