use of com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode 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.treeshaker.ElementReferenceMapper.ReferenceNode in project j2objc by google.
the class UnusedCodeTracker method markParentClasses.
/**
* Mark all ancestor classes of (sub)class as used
*/
public void markParentClasses(TypeElement type) {
while (type != null) {
String typeID = ElementReferenceMapper.stitchClassIdentifier(type, env.elementUtil());
ReferenceNode node = elementReferenceMap.get(typeID);
if (node == null) {
ErrorUtil.warning("Encountered .class parent class while accessing: " + typeID);
return;
}
if (node.reachable) {
return;
}
node.reachable = true;
if (ElementUtil.isStatic(type)) {
return;
}
type = ElementUtil.getDeclaringClass(type);
}
}
use of com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode 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.treeshaker.ElementReferenceMapper.ReferenceNode 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.treeshaker.ElementReferenceMapper.ReferenceNode 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")));
}
Aggregations