Search in sources :

Example 6 with AbstractTypeDeclaration

use of com.google.devtools.j2objc.ast.AbstractTypeDeclaration in project j2objc by google.

the class AnonymousClassConverterTest method testMethodVarInAnonymousClass.

public void testMethodVarInAnonymousClass() throws IOException {
    String source = "class Test { " + "  boolean debug;" + "  void foo() { " + "    if (true) {" + "      if (debug) {" + "        final Integer i = 1;" + "        Runnable r = new Runnable() { " + "          public void run() { int j = i + 1; } }; }}}}";
    // Verify method var in r1.run() isn't mistakenly made a field in r1.
    CompilationUnit unit = translateType("Test", source);
    NameTable nameTable = unit.getEnv().nameTable();
    List<AbstractTypeDeclaration> types = unit.getTypes();
    AbstractTypeDeclaration r1 = types.get(1);
    assertEquals("Test_1", nameTable.getFullName(r1.getTypeElement()));
    boolean found = false;
    for (VariableDeclarationFragment var : TreeUtil.getAllFields(r1)) {
        if (ElementUtil.getName(var.getVariableElement()).equals("val$i")) {
            found = true;
        }
    }
    assertTrue("required field not found", found);
    // Verify method var is passed to constructor.
    String translation = generateFromUnit(unit, "Test.m");
    assertTranslation(translation, "r = create_Test_1_initWithJavaLangInteger_(i)");
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) NameTable(com.google.devtools.j2objc.util.NameTable) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 7 with AbstractTypeDeclaration

use of com.google.devtools.j2objc.ast.AbstractTypeDeclaration in project j2objc by google.

the class ElementUtilTest method testGetAnnotationValue.

public void testGetAnnotationValue() throws IOException {
    CompilationUnit unit = translateType("Example", "@com.google.j2objc.annotations.ObjectiveCName(\"E\") class Example {}");
    AbstractTypeDeclaration decl = unit.getTypes().get(0);
    TypeElement element = decl.getTypeElement();
    AnnotationMirror annotation = ElementUtil.getAnnotation(element, ObjectiveCName.class);
    Object value = ElementUtil.getAnnotationValue(annotation, "value");
    assertEquals("E", value);
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 8 with AbstractTypeDeclaration

use of com.google.devtools.j2objc.ast.AbstractTypeDeclaration in project j2objc by google.

the class NameTableTest method testGetFullNameWithLocalClass.

// Verify local class name.
public void testGetFullNameWithLocalClass() {
    String source = "package foo.bar; class SomeClass { void test() { " + // This matches JVM naming, once '$' is substituted for the '_' characters.
    "{ class Foo {}} { class Foo {}}}}";
    CompilationUnit unit = translateType("SomeClass", source);
    NameTable nameTable = unit.getEnv().nameTable();
    AbstractTypeDeclaration decl = unit.getTypes().get(1);
    assertEquals("FooBarSomeClass_1Foo", nameTable.getFullName(decl.getTypeElement()));
    decl = unit.getTypes().get(2);
    assertEquals("FooBarSomeClass_2Foo", nameTable.getFullName(decl.getTypeElement()));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 9 with AbstractTypeDeclaration

use of com.google.devtools.j2objc.ast.AbstractTypeDeclaration in project j2objc by google.

the class NameTableTest method testGetFullNameWithPackage.

// Verify class name with package is camel-cased.
public void testGetFullNameWithPackage() {
    String source = "package foo.bar; public class SomeClass {}";
    CompilationUnit unit = translateType("SomeClass", source);
    NameTable nameTable = unit.getEnv().nameTable();
    AbstractTypeDeclaration decl = unit.getTypes().get(0);
    assertEquals("FooBarSomeClass", nameTable.getFullName(decl.getTypeElement()));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 10 with AbstractTypeDeclaration

use of com.google.devtools.j2objc.ast.AbstractTypeDeclaration in project j2objc by google.

the class PackagePrefixesTest method testGetFullNameWithPrefix.

// Verify class name with prefix.
public void testGetFullNameWithPrefix() {
    String source = "package foo.bar; public class SomeClass {}";
    options.getPackagePrefixes().addPrefix("foo.bar", "FB");
    CompilationUnit unit = translateType("SomeClass", source);
    NameTable nameTable = unit.getEnv().nameTable();
    AbstractTypeDeclaration decl = unit.getTypes().get(0);
    assertEquals("FBSomeClass", nameTable.getFullName(decl.getTypeElement()));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Aggregations

AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)31 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)23 TypeElement (javax.lang.model.element.TypeElement)6 TypeMirror (javax.lang.model.type.TypeMirror)4 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)3 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)3 NameTable (com.google.devtools.j2objc.util.NameTable)3 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)2 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)2 TreeNode (com.google.devtools.j2objc.ast.TreeNode)2 TypeDeclaration (com.google.devtools.j2objc.ast.TypeDeclaration)2 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 Annotation (com.google.devtools.j2objc.ast.Annotation)1 AnnotationTypeDeclaration (com.google.devtools.j2objc.ast.AnnotationTypeDeclaration)1 Block (com.google.devtools.j2objc.ast.Block)1 Comment (com.google.devtools.j2objc.ast.Comment)1 EnumDeclaration (com.google.devtools.j2objc.ast.EnumDeclaration)1 Expression (com.google.devtools.j2objc.ast.Expression)1 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)1