Search in sources :

Example 1 with TypeDeclaration

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

the class Functionizer method endVisit.

@Override
public void endVisit(SuperConstructorInvocation node) {
    ExecutableElement element = node.getExecutableElement();
    AbstractTypeDeclaration typeDecl = TreeUtil.getEnclosingType(node);
    TypeElement type = typeDecl.getTypeElement();
    FunctionElement funcElement = newFunctionElement(element);
    FunctionInvocation invocation = new FunctionInvocation(funcElement, typeUtil.getVoid());
    List<Expression> args = invocation.getArguments();
    args.add(new ThisExpression(ElementUtil.getDeclaringClass(element).asType()));
    if (typeDecl instanceof TypeDeclaration) {
        TypeDeclaration typeDeclaration = (TypeDeclaration) typeDecl;
        if (captureInfo.needsOuterParam(ElementUtil.getSuperclass(type))) {
            Expression outerArg = TreeUtil.remove(node.getExpression());
            args.add(outerArg != null ? outerArg : typeDeclaration.getSuperOuter().copy());
        }
        TreeUtil.moveList(typeDeclaration.getSuperCaptureArgs(), args);
    }
    TreeUtil.moveList(node.getArguments(), args);
    if (ElementUtil.isEnum(type)) {
        for (VariableElement param : captureInfo.getImplicitEnumParams()) {
            args.add(new SimpleName(param));
        }
    }
    node.replaceWith(new ExpressionStatement(invocation));
    assert funcElement.getParameterTypes().size() == args.size();
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) Expression(com.google.devtools.j2objc.ast.Expression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) TypeElement(javax.lang.model.element.TypeElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) VariableElement(javax.lang.model.element.VariableElement) AnnotationTypeDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeDeclaration) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 2 with TypeDeclaration

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

the class OuterReferenceResolverTest method testAnonymousClassCreatesLocalClassWithCaptures.

public void testAnonymousClassCreatesLocalClassWithCaptures() {
    resolveSource("Test", "class Test { Runnable test(final Object o) { " + "class Local { public void foo() { o.toString(); } } " + "return new Runnable() { public void run() { new Local(); } }; } }");
    TypeDeclaration runnableNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(2);
    assertTrue(ElementUtil.isAnonymous(runnableNode.getTypeElement()));
    List<VariableElement> innerFields = Lists.newArrayList(captureInfo.getCaptureFields(runnableNode.getTypeElement()));
    assertEquals(1, innerFields.size());
    assertEquals("val$o", ElementUtil.getName(innerFields.get(0)));
    ClassInstanceCreation creationNode = (ClassInstanceCreation) nodesByType.get(Kind.CLASS_INSTANCE_CREATION).get(1);
    List<Expression> captureArgs = creationNode.getCaptureArgs();
    assertEquals(1, captureArgs.size());
    Expression captureArg = captureArgs.get(0);
    assertTrue(captureArg instanceof SimpleName);
    VariableElement captureVar = TreeUtil.getVariableElement(captureArg);
    assertNotNull(captureVar);
    assertEquals("val$o", ElementUtil.getName(captureVar));
}
Also used : ClassInstanceCreation(com.google.devtools.j2objc.ast.ClassInstanceCreation) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration)

Example 3 with TypeDeclaration

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

the class OuterReferenceResolverTest method testInheritedOuterMethod.

public void testInheritedOuterMethod() {
    resolveSource("Test", "class Test { class A { void foo() {} } class B extends A { " + "class Inner { void test() { foo(); } } } }");
    TypeDeclaration aNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(1);
    TypeDeclaration bNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(2);
    TypeDeclaration innerNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(3);
    assertFalse(captureInfo.needsOuterReference(aNode.getTypeElement()));
    assertFalse(captureInfo.needsOuterReference(bNode.getTypeElement()));
    assertTrue(captureInfo.needsOuterReference(innerNode.getTypeElement()));
    // B will need an outer reference to Test so it can initialize its
    // superclass A.
    Expression bSuperOuter = bNode.getSuperOuter();
    assertTrue(bSuperOuter instanceof SimpleName);
    assertEquals("outer$", ElementUtil.getName(TreeUtil.getVariableElement(bSuperOuter)));
    // foo() call will need to get to B's scope to call the inherited method.
    MethodInvocation fooCall = (MethodInvocation) nodesByType.get(Kind.METHOD_INVOCATION).get(0);
    Expression expr = fooCall.getExpression();
    assertTrue(expr instanceof SimpleName);
    VariableElement fooReceiver = TreeUtil.getVariableElement(expr);
    assertNotNull(fooReceiver);
    assertEquals("Test.B", fooReceiver.asType().toString());
}
Also used : PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) SimpleName(com.google.devtools.j2objc.ast.SimpleName) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) VariableElement(javax.lang.model.element.VariableElement) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration)

Example 4 with TypeDeclaration

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

the class OuterReferenceResolverTest method testCapturedWeakLocalVariable.

public void testCapturedWeakLocalVariable() {
    resolveSource("Test", "import com.google.j2objc.annotations.Weak;" + "class Test { void test(@Weak final int i) { Runnable r = new Runnable() { " + "public void run() { int i2 = i + 1; } }; } }");
    TypeDeclaration runnableNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(1);
    assertTrue(ElementUtil.isAnonymous(runnableNode.getTypeElement()));
    List<VariableElement> innerFields = Lists.newArrayList(captureInfo.getCaptureFields(runnableNode.getTypeElement()));
    assertEquals(1, innerFields.size());
    assertTrue(ElementUtil.isWeakReference(innerFields.get(0)));
}
Also used : VariableElement(javax.lang.model.element.VariableElement) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration)

Example 5 with TypeDeclaration

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

the class TreeConverter method convertClassDeclaration.

private TreeNode convertClassDeclaration(JCTree.JCClassDecl node) {
    // to support our different declaration nodes.
    if (node.sym.getKind() == ElementKind.ANNOTATION_TYPE) {
        throw new AssertionError("Annotation type declaration tree conversion not implemented");
    }
    TypeDeclaration newNode = (TypeDeclaration) convertAbstractTypeDeclaration(node, new TypeDeclaration());
    newNode.setInterface(node.getKind() == Kind.INTERFACE || node.getKind() == Kind.ANNOTATION_TYPE);
    if (node.sym.isAnonymous()) {
        newUnit.getEnv().elementUtil().mapElementType(node.sym, node.type);
    }
    return newNode;
}
Also used : AnnotationTypeDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeDeclaration) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Aggregations

TypeDeclaration (com.google.devtools.j2objc.ast.TypeDeclaration)15 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)6 VariableElement (javax.lang.model.element.VariableElement)6 Expression (com.google.devtools.j2objc.ast.Expression)5 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)4 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)4 SimpleName (com.google.devtools.j2objc.ast.SimpleName)4 AnnotationTypeDeclaration (com.google.devtools.j2objc.ast.AnnotationTypeDeclaration)3 TypeElement (javax.lang.model.element.TypeElement)3 ClassInstanceCreation (com.google.devtools.j2objc.ast.ClassInstanceCreation)2 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)2 Annotation (com.google.devtools.j2objc.ast.Annotation)1 Block (com.google.devtools.j2objc.ast.Block)1 EnumDeclaration (com.google.devtools.j2objc.ast.EnumDeclaration)1 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)1 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)1 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)1 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)1 PackageDeclaration (com.google.devtools.j2objc.ast.PackageDeclaration)1 QualifiedName (com.google.devtools.j2objc.ast.QualifiedName)1