Search in sources :

Example 36 with Expression

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

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

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

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

the class TreeConverter method convertExpressionMethodReference.

private static TreeNode convertExpressionMethodReference(org.eclipse.jdt.core.dom.ExpressionMethodReference node) {
    IMethodBinding methodBinding = node.resolveMethodBinding();
    ExpressionMethodReference newNode = new ExpressionMethodReference();
    Expression expression = (Expression) convert(node.getExpression());
    boolean consumesFirstParam = !BindingUtil.isStatic(methodBinding) && expression instanceof Name && !ElementUtil.isVariable(((Name) expression).getElement());
    convertMethodReference(node, newNode, methodBinding, consumesFirstParam);
    return newNode.setName((SimpleName) TreeConverter.convert(node.getName())).setExpression(expression);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ExpressionMethodReference(com.google.devtools.j2objc.ast.ExpressionMethodReference) 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) SimpleName(com.google.devtools.j2objc.ast.SimpleName) SimpleName(com.google.devtools.j2objc.ast.SimpleName) Name(com.google.devtools.j2objc.ast.Name) QualifiedName(com.google.devtools.j2objc.ast.QualifiedName)

Example 40 with Expression

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

the class Autoboxer method endVisit.

@Override
public void endVisit(ConditionalExpression node) {
    Expression expr = node.getExpression();
    if (!expr.getTypeMirror().getKind().isPrimitive()) {
        unbox(expr);
    }
    boolean nodeIsPrimitive = node.getTypeMirror().getKind().isPrimitive();
    Expression thenExpr = node.getThenExpression();
    boolean thenIsPrimitive = thenExpr.getTypeMirror().getKind().isPrimitive();
    Expression elseExpr = node.getElseExpression();
    boolean elseIsPrimitive = elseExpr.getTypeMirror().getKind().isPrimitive();
    if (thenIsPrimitive && !nodeIsPrimitive) {
        box(thenExpr);
    } else if (!thenIsPrimitive && nodeIsPrimitive) {
        unbox(thenExpr);
    }
    if (elseIsPrimitive && !nodeIsPrimitive) {
        box(elseExpr);
    } else if (!elseIsPrimitive && nodeIsPrimitive) {
        unbox(elseExpr);
    }
}
Also used : CastExpression(com.google.devtools.j2objc.ast.CastExpression) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression)

Aggregations

Expression (com.google.devtools.j2objc.ast.Expression)106 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)80 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)68 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)60 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)57 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)50 CastExpression (com.google.devtools.j2objc.ast.CastExpression)46 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)45 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)42 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)32 TypeMirror (javax.lang.model.type.TypeMirror)32 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)30 VariableElement (javax.lang.model.element.VariableElement)30 LambdaExpression (com.google.devtools.j2objc.ast.LambdaExpression)26 SimpleName (com.google.devtools.j2objc.ast.SimpleName)25 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)19 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)19 FunctionalExpression (com.google.devtools.j2objc.ast.FunctionalExpression)15 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)14 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)14