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");
}
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");
}
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;
}
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);
}
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"));
}
Aggregations