Search in sources :

Example 1 with ClassInstanceCreation

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

the class TreeConverter method convertVariableDeclaration.

private TreeNode convertVariableDeclaration(JCTree.JCVariableDecl node) {
    VarSymbol var = node.sym;
    if (var.getKind() == ElementKind.FIELD) {
        FieldDeclaration newNode = new FieldDeclaration(var, (Expression) convert(node.getInitializer()));
        convertBodyDeclaration(node, node.getModifiers(), newNode, var);
        return newNode;
    }
    if (var.getKind() == ElementKind.LOCAL_VARIABLE) {
        return new VariableDeclarationStatement(var, (Expression) convert(node.getInitializer()));
    }
    if (var.getKind() == ElementKind.ENUM_CONSTANT) {
        EnumConstantDeclaration newNode = new EnumConstantDeclaration().setVariableElement(var);
        convertBodyDeclaration(node, node.getModifiers(), newNode, var);
        ClassInstanceCreation init = (ClassInstanceCreation) convert(node.getInitializer());
        TreeUtil.moveList(init.getArguments(), newNode.getArguments());
        if (init.getAnonymousClassDeclaration() != null) {
            newNode.setAnonymousClassDeclaration(TreeUtil.remove(init.getAnonymousClassDeclaration()));
        }
        return newNode.setExecutablePair(init.getExecutablePair()).setVarargsType(init.getVarargsType());
    }
    return convertSingleVariable(node);
}
Also used : ClassInstanceCreation(com.google.devtools.j2objc.ast.ClassInstanceCreation) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Example 2 with ClassInstanceCreation

use of com.google.devtools.j2objc.ast.ClassInstanceCreation 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 ClassInstanceCreation

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

the class OuterReferenceResolverTest method testNestedLocalClassesWithNestedCreations.

public void testNestedLocalClassesWithNestedCreations() {
    // This test is particularly tricky for OuterReferenceResolver because A captures variable i,
    // but that is not known until after A's creation. A's creation occurs within B, which requires
    // B to have an outer field in order to access A's capturing field for i. B's creation therefore
    // requires the outer field to be passed as an outer argument.
    // Because of the cascading effects of the statements in this test and the order in which they
    // occur, we would need to do three passes over the code to resolve B's creation successfuly.
    resolveSource("Test", "class Test { void test(int i) { class A { " + "void foo() { class B { void bar() { new B(); new A(); } } } " + "int other() { return i; } } } }");
    ClassInstanceCreation bCreate = (ClassInstanceCreation) nodesByType.get(Kind.CLASS_INSTANCE_CREATION).get(0);
    Expression outerArg = bCreate.getExpression();
    assertTrue(outerArg instanceof SimpleName);
    VariableElement var = TreeUtil.getVariableElement(outerArg);
    assertNotNull(var);
    assertEquals("this$0", ElementUtil.getName(var));
}
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)

Example 4 with ClassInstanceCreation

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

the class TreeConverter method convertClassInstanceCreation.

private static TreeNode convertClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation node) {
    ClassInstanceCreation newNode = new ClassInstanceCreation();
    convertExpression(node, newNode);
    for (Object argument : node.arguments()) {
        newNode.addArgument((Expression) TreeConverter.convert(argument));
    }
    IMethodBinding binding = node.resolveConstructorBinding();
    JdtExecutableElement element = (JdtExecutableElement) BindingConverter.getExecutableElement(binding);
    JdtExecutableType type = BindingConverter.getType(binding);
    newNode.setExecutablePair(new ExecutablePair(element, type)).setVarargsType(getVarargsType(binding, node.arguments())).setType((Type) TreeConverter.convert(node.getType()));
    Expression expression = (Expression) TreeConverter.convert(node.getExpression());
    org.eclipse.jdt.core.dom.AnonymousClassDeclaration anonymousClassDecl = node.getAnonymousClassDeclaration();
    if (anonymousClassDecl != null && expression != null) {
        VariableElement superOuterParam = GeneratedVariableElement.newParameter("superOuter$", expression.getTypeMirror(), element);
        element.setSuperOuterParam(superOuterParam);
        type.setSuperOuterParamType(superOuterParam.asType());
        newNode.addArgument(0, expression);
    } else {
        newNode.setExpression(expression);
    }
    if (anonymousClassDecl != null) {
        newNode.setAnonymousClassDeclaration(convertAnonymousClassDeclaration(anonymousClassDecl, element, binding));
    }
    return newNode;
}
Also used : ClassInstanceCreation(com.google.devtools.j2objc.ast.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionalExpression(com.google.devtools.j2objc.ast.FunctionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 5 with ClassInstanceCreation

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

the class EnumRewriter method addArcInitialization.

// ARC does not allow using "objc_constructInstance" so ARC code doesn't get
// the shared allocation optimization.
private void addArcInitialization(EnumDeclaration node) {
    List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
    int i = 0;
    for (EnumConstantDeclaration constant : node.getEnumConstants()) {
        VariableElement varElement = constant.getVariableElement();
        ClassInstanceCreation creation = new ClassInstanceCreation(constant.getExecutablePair());
        TreeUtil.copyList(constant.getArguments(), creation.getArguments());
        creation.addArgument(new StringLiteral(ElementUtil.getName(varElement), typeUtil));
        creation.addArgument(new NumberLiteral(i++, typeUtil));
        creation.setHasRetainedResult(true);
        stmts.add(new ExpressionStatement(new Assignment(new SimpleName(varElement), creation)));
    }
}
Also used : ClassInstanceCreation(com.google.devtools.j2objc.ast.ClassInstanceCreation) Assignment(com.google.devtools.j2objc.ast.Assignment) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) NumberLiteral(com.google.devtools.j2objc.ast.NumberLiteral)

Aggregations

ClassInstanceCreation (com.google.devtools.j2objc.ast.ClassInstanceCreation)6 VariableElement (javax.lang.model.element.VariableElement)5 Expression (com.google.devtools.j2objc.ast.Expression)4 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)4 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)4 SimpleName (com.google.devtools.j2objc.ast.SimpleName)4 EnumConstantDeclaration (com.google.devtools.j2objc.ast.EnumConstantDeclaration)2 TypeDeclaration (com.google.devtools.j2objc.ast.TypeDeclaration)2 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)2 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)2 Assignment (com.google.devtools.j2objc.ast.Assignment)1 CastExpression (com.google.devtools.j2objc.ast.CastExpression)1 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)1 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)1 FieldDeclaration (com.google.devtools.j2objc.ast.FieldDeclaration)1 ForStatement (com.google.devtools.j2objc.ast.ForStatement)1 FunctionalExpression (com.google.devtools.j2objc.ast.FunctionalExpression)1 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)1 LambdaExpression (com.google.devtools.j2objc.ast.LambdaExpression)1 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)1