Search in sources :

Example 41 with CompilationUnit

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

the class TypeDeclarationGeneratorTest method testEmptyStatementsIgnored.

// Verify that an empty statement following a type declaration is ignored.
// The JDT parser discards them, while javac includes them in the compilation unit.
public void testEmptyStatementsIgnored() throws IOException {
    String source = "public interface A { void bar(); };";
    CompilationUnit unit = translateType("A", source);
    assertEquals(1, unit.getTypes().size());
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit)

Example 42 with CompilationUnit

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

the class InnerClassExtractorTest method testMultipleThisReferencesWithPreviousReference.

/**
   * This test differs from the last one only in the addition of another
   * 'this' reference before the anonymous class creation.
   */
public void testMultipleThisReferencesWithPreviousReference() throws IOException {
    String source = "class A { private int x = 0; " + "  interface Foo { void doSomething(); } " + "  class Inner { private int x = 1; " + "    public void blah() { " + "      A.this.x = 2; " + "      new Foo() { public void doSomething() { " + "      Inner.this.x = 3; A.this.x = 4; }}; }}}";
    CompilationUnit unit = translateType("A", source);
    List<AbstractTypeDeclaration> types = unit.getTypes();
    assertEquals(4, types.size());
    String translation = translateSourceFile(source, "A", "A.m");
    // Anonymous class constructor in Inner.blah()
    assertTranslation(translation, "create_A_Inner_1_initWithA_Inner_(self)");
    // A.x referred to in A.Inner.
    assertTranslation(translation, "this$0_->x_ = 2");
    // A.Inner.x referred to in anonymous Foo.
    assertTranslation(translation, "this$0_->x_ = 3");
    // A.x referred to in anonymous Foo
    assertTranslation(translation, "this$0_->this$0_->x_ = 4");
    // A.Inner init in anonymous Foo's constructor
    assertTranslation(translation, "JreStrongAssign(&self->this$0_, outer$)");
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 43 with CompilationUnit

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

the class InnerClassExtractorTest method testMultipleThisReferences.

public void testMultipleThisReferences() throws IOException {
    String source = "class A { private int x = 0; " + "  interface Foo { void doSomething(); } " + "  class Inner { private int x = 1; " + "    public void blah() { " + "      new Foo() { public void doSomething() { " + "      Inner.this.x = 2; A.this.x = 3; }}; }}}";
    CompilationUnit unit = translateType("A", source);
    List<AbstractTypeDeclaration> types = unit.getTypes();
    assertEquals(4, types.size());
    String translation = translateSourceFile(source, "A", "A.m");
    // Anonymous class constructor in Inner.blah()
    assertTranslation(translation, "create_A_Inner_1_initWithA_Inner_(self)");
    // A.Inner.x referred to in anonymous Foo
    assertTranslation(translation, "this$0_->x_ = 2");
    // A.x referred to in anonymous Foo
    assertTranslation(translation, "this$0_->this$0_->x_ = 3");
    // A.Inner init in anonymous Foo's constructor
    assertTranslation(translation, "JreStrongAssign(&self->this$0_, outer$)");
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 44 with CompilationUnit

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

the class InnerClassExtractor method endHandleType.

private void endHandleType(AbstractTypeDeclaration node) {
    int insertIdx = typeOrderStack.remove(typeOrderStack.size() - 1);
    TreeNode parentNode = node.getParent();
    if (!(parentNode instanceof CompilationUnit)) {
        // Remove this type declaration from its current location.
        node.remove();
        if (parentNode instanceof TypeDeclarationStatement) {
            parentNode.remove();
        }
        addCaptureFields(node);
        // Make this node non-private, if necessary, and add it to the unit's type
        // list.
        node.removeModifiers(Modifier.PRIVATE);
        unitTypes.add(insertIdx, node);
        // Check for erroneous WeakOuter annotation on static inner class.
        TypeElement type = node.getTypeElement();
        if (ElementUtil.isStatic(type) && ElementUtil.hasAnnotation(type, WeakOuter.class)) {
            ErrorUtil.warning("static class " + type.getQualifiedName() + " has WeakOuter annotation");
        }
    }
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TreeNode(com.google.devtools.j2objc.ast.TreeNode) TypeDeclarationStatement(com.google.devtools.j2objc.ast.TypeDeclarationStatement) TypeElement(javax.lang.model.element.TypeElement) WeakOuter(com.google.j2objc.annotations.WeakOuter)

Example 45 with CompilationUnit

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

the class GeneratedType method fromTypeDeclaration.

public static GeneratedType fromTypeDeclaration(AbstractTypeDeclaration typeNode) {
    TypeElement typeElement = typeNode.getTypeElement();
    CompilationUnit unit = TreeUtil.getCompilationUnit(typeNode);
    NameTable nameTable = unit.getEnv().nameTable();
    boolean emitLineDirectives = unit.getEnv().options().emitLineDirectives();
    ImmutableList.Builder<String> superTypes = ImmutableList.builder();
    TypeElement superclass = ElementUtil.getSuperclass(typeElement);
    if (superclass != null) {
        superTypes.add(nameTable.getFullName(superclass));
    }
    for (TypeElement superInterface : ElementUtil.getInterfaces(typeElement)) {
        superTypes.add(nameTable.getFullName(superInterface));
    }
    HeaderImportCollector headerCollector = new HeaderImportCollector(unit, HeaderImportCollector.Filter.PUBLIC_ONLY);
    typeNode.accept(headerCollector);
    HeaderImportCollector privateDeclarationCollector = new HeaderImportCollector(unit, HeaderImportCollector.Filter.PRIVATE_ONLY);
    typeNode.accept(privateDeclarationCollector);
    ImplementationImportCollector importCollector = new ImplementationImportCollector(unit);
    typeNode.accept(importCollector);
    SourceBuilder builder = new SourceBuilder(emitLineDirectives);
    TypeDeclarationGenerator.generate(builder, typeNode);
    String publicDeclarationCode = builder.toString();
    builder = new SourceBuilder(emitLineDirectives);
    TypePrivateDeclarationGenerator.generate(builder, typeNode);
    String privateDeclarationCode;
    String implementationCode;
    Options options = unit.getEnv().options();
    if (unit.getEnv().translationUtil().generateImplementation(typeElement)) {
        builder = new SourceBuilder(options.emitLineDirectives());
        TypePrivateDeclarationGenerator.generate(builder, typeNode);
        privateDeclarationCode = builder.toString();
        builder = new SourceBuilder(options.emitLineDirectives());
        TypeImplementationGenerator.generate(builder, typeNode);
        implementationCode = builder.toString();
    } else {
        privateDeclarationCode = "";
        implementationCode = String.format("// Implementation not generated because %s is on the bootclasspath.\n", ElementUtil.getQualifiedName(typeElement));
    }
    ImmutableSet.Builder<Import> implementationIncludes = ImmutableSet.builder();
    implementationIncludes.addAll(privateDeclarationCollector.getSuperTypes());
    implementationIncludes.addAll(importCollector.getImports());
    return new GeneratedType(nameTable.getFullName(typeElement), typeNode.hasPrivateDeclaration(), superTypes.build(), ImmutableSet.copyOf(headerCollector.getForwardDeclarations()), ImmutableSet.copyOf(headerCollector.getSuperTypes()), ImmutableSet.copyOf(privateDeclarationCollector.getForwardDeclarations()), implementationIncludes.build(), publicDeclarationCode, privateDeclarationCode, implementationCode);
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) Options(com.google.devtools.j2objc.Options) Import(com.google.devtools.j2objc.types.Import) TypeElement(javax.lang.model.element.TypeElement) ImmutableList(com.google.common.collect.ImmutableList) HeaderImportCollector(com.google.devtools.j2objc.types.HeaderImportCollector) ImplementationImportCollector(com.google.devtools.j2objc.types.ImplementationImportCollector) ImmutableSet(com.google.common.collect.ImmutableSet) NameTable(com.google.devtools.j2objc.util.NameTable)

Aggregations

CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)71 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)24 HashSet (java.util.HashSet)24 ReferenceNode (com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode)23 HashMap (java.util.HashMap)23 Set (java.util.Set)23 CodeReferenceMap (com.google.devtools.j2objc.util.CodeReferenceMap)9 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)7 MethodReferenceNode (com.google.devtools.treeshaker.ElementReferenceMapper.MethodReferenceNode)7 TypeElement (javax.lang.model.element.TypeElement)5 TreeVisitor (com.google.devtools.j2objc.ast.TreeVisitor)4 NameTable (com.google.devtools.j2objc.util.NameTable)4 TreeNode (com.google.devtools.j2objc.ast.TreeNode)3 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)3 RegularInputFile (com.google.devtools.j2objc.file.RegularInputFile)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 TypeMirror (javax.lang.model.type.TypeMirror)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)2 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)2