Search in sources :

Example 1 with CodeReferenceMap

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

the class DeadCodeEliminatorTest method testDeadEnum.

public void testDeadEnum() throws IOException {
    CodeReferenceMap map = CodeReferenceMap.builder().addClass("A$Thing").build();
    setDeadCodeMap(map);
    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";
    String translation = translateSourceFile(source, "A", "A.m");
    assertNotInTranslation(translation, "initWithInt");
    assertNotInTranslation(translation, "THING1");
    assertNotInTranslation(translation, "THING2");
    assertNotInTranslation(translation, "THING3");
    String header = getTranslatedFile("A.h");
    assertNotInTranslation(header, "Serializable");
}
Also used : CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap)

Example 2 with CodeReferenceMap

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

the class DeadCodeEliminatorTest method testDeadMethod_InnerClassConstructor.

public void testDeadMethod_InnerClassConstructor() throws IOException {
    String source = "class A {\n" + "  class B {\n" + "    B(int i) {}\n" + "  }\n" + "}\n";
    CodeReferenceMap map = CodeReferenceMap.builder().addMethod("A$B", "A$B", "(LA;I)V").build();
    setDeadCodeMap(map);
    String translation = translateSourceFile(source, "A", "A.m");
    assertNotInTranslation(translation, "withInt");
}
Also used : CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap)

Example 3 with CodeReferenceMap

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

the class DeadCodeEliminatorTest method testTypeNarrowingMethodsNotShowingInDeadClasses.

public void testTypeNarrowingMethodsNotShowingInDeadClasses() throws IOException {
    String source = "class Base<T> { \n" + "  T someMethod() { return null; }\n" + "}\n" + "class Foo extends Base<String> {}";
    CodeReferenceMap map = CodeReferenceMap.builder().addClass("Foo").build();
    setDeadCodeMap(map);
    String header = translateSourceFile(source, "Foo", "Foo.h");
    assertNotInTranslation(header, "@interface Foo : Base");
    assertTranslation(header, "@interface Foo : NSObject");
    assertTranslation(header, "- (id)someMethod;");
    assertNotInTranslation(header, "- (NSString *)someMethod;");
}
Also used : CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap)

Example 4 with CodeReferenceMap

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

the class DeadCodeEliminatorTest method testDeadClass_FieldRemoval.

public void testDeadClass_FieldRemoval() throws IOException {
    CodeReferenceMap map = CodeReferenceMap.builder().addClass("Foo").build();
    setDeadCodeMap(map);
    String source = "class Foo {\n" + "  static final int x = f();\n" + "  static final int y = 0;\n" + "  static int f() { return 0; }\n" + "}\n";
    String translation = translateSourceFile(source, "Foo", "Foo.h");
    assertTranslation(translation, "#define Foo_y 0");
    translation = getTranslatedFile("Foo.m");
    assertNotInTranslation(translation, "jint Foo_x_");
    assertNotInTranslation(translation, "Foo_x_ = Foo_f()");
    assertNotInTranslation(translation, "+ (jint)f");
    assertNotInTranslation(translation, "bar");
}
Also used : CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap)

Example 5 with CodeReferenceMap

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

the class UnusedCodeTrackerTest method testUnusedConstructor.

public void testUnusedConstructor() throws IOException {
    String source = "class A {\n" + "  public A() {bar = 2;}" + "  public A(int i) {bar = i;}" + "  private int bar = 1;\n" + "}\n" + "class B {\n" + "  public static void foo() {new A();}\n" + "  static {foo();}" + "}\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>", "(I)V")));
    assertTrue(elementSet.contains(ElementReferenceMapper.stitchMethodIdentifier("B", "foo", "()V")));
    assertFalse(unusedCodeMap.containsClass("A"));
    assertFalse(unusedCodeMap.containsMethod("B", "foo", "()V"));
    assertFalse(unusedCodeMap.containsMethod("A", "<init>", "()V"));
    assertTrue(unusedCodeMap.containsMethod("A", "<init>", "(I)V"));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ReferenceNode(com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode) CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap) HashSet(java.util.HashSet)

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