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