use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class UnusedCodeTrackerTest method testInputMethodsKept.
public void testInputMethodsKept() throws IOException {
String source = "class A {\n" + " private static void abc(String s) {}\n" + " private static void xyz(String s) {}\n" + " private static void foo(String s) {xyz(\"woo\");}\n" + "}\n";
Builder builder = CodeReferenceMap.builder();
builder.addMethod("A", "abc", "(Ljava/lang/String;)V");
builder.addMethod("A", "xyz", "(Ljava/lang/String;)V");
CodeReferenceMap inputCodeMap = builder.build();
CompilationUnit unit = compileType("test", source);
final HashMap<String, ReferenceNode> elementMap = new HashMap<>();
final HashMap<String, Set<String>> overrideMap = new HashMap<>();
final Set<String> staticSet = new HashSet<>();
ElementReferenceMapper mapper = new ElementReferenceMapper(unit, elementMap, staticSet, overrideMap);
mapper.run();
UnusedCodeTracker tracker = new UnusedCodeTracker(unit.getEnv(), elementMap, staticSet, overrideMap);
tracker.markUsedElements(inputCodeMap);
assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "abc", "(Ljava/lang/String;)V")).reachable);
assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "xyz", "(Ljava/lang/String;)V")).reachable);
assertFalse(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A", "foo", "(Ljava/lang/String;)V")).reachable);
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class UnusedCodeTrackerTest method testSuperConstructorInvocation.
public void testSuperConstructorInvocation() throws IOException {
String source = "class A {\n" + " public A() {this(true);}" + " public A(boolean b) {bar = 0;}" + " public A(int i) {bar = i;}" + " private int bar = 1;\n" + "}\n" + "class B extends A {\n" + " public B() {super(1);}\n" + " static {new B();}" + "}\n";
CompilationUnit unit = compileType("test", source);
final HashMap<String, ReferenceNode> elementMap = new HashMap<>();
final HashMap<String, Set<String>> overrideMap = new HashMap<>();
final Set<String> staticSet = new HashSet<>();
ElementReferenceMapper mapper = new ElementReferenceMapper(unit, elementMap, staticSet, overrideMap);
mapper.run();
Set<String> elementSet = elementMap.keySet();
UnusedCodeTracker tracker = new UnusedCodeTracker(unit.getEnv(), elementMap, staticSet, overrideMap);
tracker.markUsedElements();
CodeReferenceMap unusedCodeMap = tracker.buildTreeShakerMap();
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "<init>", "()V")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "<init>", "(Z)V")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("A", "<init>", "(I)V")));
assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("B", "<init>", "()V")));
assertFalse(unusedCodeMap.containsClass("A"));
assertFalse(unusedCodeMap.containsMethod("B", "<init>", "()V"));
assertFalse(unusedCodeMap.containsMethod("A", "<init>", "(I)V"));
assertTrue(unusedCodeMap.containsMethod("A", "<init>", "(Z)V"));
assertTrue(unusedCodeMap.containsMethod("A", "<init>", "()V"));
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class UnusedCodeTrackerTest method testUsedStaticNestedClasses.
public void testUsedStaticNestedClasses() throws IOException {
String source = "class A {\n" + " static { B.abc(\"zoo\"); }\n" + " static class B {\n" + " public static void abc(String s) {}\n" + " }\n" + " class C {\n" + " public void xyz(String s) {}\n" + " }\n" + "}\n";
CompilationUnit unit = compileType("test", source);
final HashMap<String, ReferenceNode> elementMap = new HashMap<>();
final HashMap<String, Set<String>> overrideMap = new HashMap<>();
final Set<String> staticSet = new HashSet<>();
ElementReferenceMapper mapper = new ElementReferenceMapper(unit, elementMap, staticSet, overrideMap);
mapper.run();
UnusedCodeTracker tracker = new UnusedCodeTracker(unit.getEnv(), elementMap, staticSet, overrideMap);
tracker.markUsedElements();
assertFalse(elementMap.get(ElementReferenceMapper.stitchClassIdentifier("A")).reachable);
assertTrue(elementMap.get(ElementReferenceMapper.stitchClassIdentifier("A$B")).reachable);
assertFalse(elementMap.get(ElementReferenceMapper.stitchClassIdentifier("A$C")).reachable);
assertTrue(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A$B", "abc", "(Ljava/lang/String;)V")).reachable);
assertFalse(elementMap.get(ElementReferenceMapper.stitchMethodIdentifier("A$C", "xyz", "(Ljava/lang/String;)V")).reachable);
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class ElementUtilTest method testHasAnnotation.
public void testHasAnnotation() throws IOException {
CompilationUnit unit = translateType("Example", "@com.google.j2objc.annotations.ObjectiveCName(\"E\") class Example {}");
AbstractTypeDeclaration decl = unit.getTypes().get(0);
TypeElement element = decl.getTypeElement();
assertTrue(ElementUtil.hasAnnotation(element, ObjectiveCName.class));
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class NameTableTest method testGetFullNameNoPackage.
// Verify class name without package is unchanged.
public void testGetFullNameNoPackage() {
String source = "public class SomeClass {}";
CompilationUnit unit = translateType("SomeClass", source);
NameTable nameTable = unit.getEnv().nameTable();
AbstractTypeDeclaration decl = unit.getTypes().get(0);
assertEquals("SomeClass", nameTable.getFullName(decl.getTypeElement()));
}
Aggregations