Search in sources :

Example 6 with MethodInvocation

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

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

the class Autoboxer method unbox.

/**
   * Convert a wrapper class instance to a specified primitive equivalent.
   */
private void unbox(Expression expr, PrimitiveType primitiveType) {
    TypeElement boxedClass = findBoxedSuperclass(expr.getTypeMirror());
    if (primitiveType == null && boxedClass != null) {
        primitiveType = typeUtil.unboxedType(boxedClass.asType());
    }
    if (primitiveType == null) {
        return;
    }
    ExecutableElement valueMethod = ElementUtil.findMethod(boxedClass, TypeUtil.getName(primitiveType) + VALUE_METHOD);
    assert valueMethod != null : "could not find value method for " + boxedClass;
    MethodInvocation invocation = new MethodInvocation(new ExecutablePair(valueMethod), null);
    expr.replaceWith(invocation);
    invocation.setExpression(expr);
}
Also used : ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Example 8 with MethodInvocation

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

the class ArrayRewriter method newSingleDimensionArrayInvocation.

private MethodInvocation newSingleDimensionArrayInvocation(ArrayType arrayType, Expression dimensionExpr, boolean retainedResult) {
    TypeMirror componentType = arrayType.getComponentType();
    TypeElement iosArrayElement = typeUtil.getIosArray(componentType);
    boolean isPrimitive = componentType.getKind().isPrimitive();
    String selector = (retainedResult ? "newArray" : "array") + "WithLength:" + (isPrimitive ? "" : "type:");
    GeneratedExecutableElement methodElement = GeneratedExecutableElement.newMethodWithSelector(selector, iosArrayElement.asType(), iosArrayElement).addModifiers(Modifier.PUBLIC, Modifier.STATIC);
    methodElement.addParameter(GeneratedVariableElement.newParameter("length", typeUtil.getInt(), methodElement));
    if (!isPrimitive) {
        methodElement.addParameter(GeneratedVariableElement.newParameter("type", TypeUtil.IOS_CLASS.asType(), methodElement));
    }
    MethodInvocation invocation = new MethodInvocation(new ExecutablePair(methodElement), arrayType, new SimpleName(iosArrayElement));
    // Add the array length argument.
    invocation.addArgument(dimensionExpr.copy());
    // Add the type argument for object arrays.
    if (!isPrimitive) {
        invocation.addArgument(new TypeLiteral(componentType, typeUtil));
    }
    return invocation;
}
Also used : GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) TypeElement(javax.lang.model.element.TypeElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Example 9 with MethodInvocation

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

the class Autoboxer method boxWithClass.

private void boxWithClass(Expression expr, TypeElement boxedClass) {
    PrimitiveType primitiveType = typeUtil.unboxedType(boxedClass.asType());
    assert primitiveType != null;
    ExecutableElement wrapperMethod = ElementUtil.findMethod(boxedClass, VALUEOF_METHOD, TypeUtil.getQualifiedName(primitiveType));
    assert wrapperMethod != null : "could not find valueOf method for " + boxedClass;
    MethodInvocation invocation = new MethodInvocation(new ExecutablePair(wrapperMethod), new SimpleName(boxedClass));
    expr.replaceWith(invocation);
    invocation.addArgument(expr);
}
Also used : ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) PrimitiveType(javax.lang.model.type.PrimitiveType) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Example 10 with MethodInvocation

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

the class TreeConverter method convertMethodInvocation.

private TreeNode convertMethodInvocation(JCTree.JCMethodInvocation node) {
    JCTree.JCExpression method = node.getMethodSelect();
    String methodName = getMemberName(method);
    ExecutableType type = (ExecutableType) method.type;
    Symbol.MethodSymbol sym = (Symbol.MethodSymbol) getMemberSymbol(method);
    JCTree.JCExpression target = method.getKind() == Kind.MEMBER_SELECT ? ((JCTree.JCFieldAccess) method).selected : null;
    if ("this".equals(methodName)) {
        ConstructorInvocation newNode = new ConstructorInvocation().setExecutablePair(new ExecutablePair(sym)).setVarargsType(node.varargsElement);
        for (JCTree.JCExpression arg : node.getArguments()) {
            newNode.addArgument((Expression) convert(arg));
        }
        return newNode;
    }
    if ("super".equals(methodName)) {
        SuperConstructorInvocation newNode = new SuperConstructorInvocation().setExecutablePair(new ExecutablePair(sym)).setVarargsType(node.varargsElement);
        if (target != null) {
            newNode.setExpression((Expression) convert(target));
        }
        for (JCTree.JCExpression arg : node.getArguments()) {
            newNode.addArgument((Expression) convert(arg));
        }
        return newNode;
    }
    if (target != null && "super".equals(getMemberName(target))) {
        SuperMethodInvocation newNode = new SuperMethodInvocation().setExecutablePair(new ExecutablePair(sym, type)).setVarargsType(node.varargsElement).setName(convertSimpleName(sym, type, getPosition(node)));
        if (target.getKind() == Kind.MEMBER_SELECT) {
            // foo.bar.MyClass.super.print(...):
            //   target: foo.bar.MyClass.super
            //   target.selected: foo.bar.MyClass
            newNode.setQualifier((Name) convert(((JCTree.JCFieldAccess) target).selected));
        }
        for (JCTree.JCExpression arg : node.getArguments()) {
            newNode.addArgument((Expression) convert(arg));
        }
        return newNode;
    }
    MethodInvocation newNode = new MethodInvocation();
    newNode.setName(convertSimpleName(sym, type, getPosition(method)));
    if (target != null) {
        newNode.setExpression((Expression) convert(target));
    }
    for (JCTree.JCExpression arg : node.getArguments()) {
        newNode.addArgument((Expression) convert(arg));
    }
    return newNode.setTypeMirror(node.type).setExecutablePair(new ExecutablePair(sym, type)).setVarargsType(node.varargsElement);
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) ConstructorInvocation(com.google.devtools.j2objc.ast.ConstructorInvocation) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) JCTree(com.sun.tools.javac.tree.JCTree) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation)

Aggregations

MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)17 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)14 SimpleName (com.google.devtools.j2objc.ast.SimpleName)9 TypeMirror (javax.lang.model.type.TypeMirror)9 Expression (com.google.devtools.j2objc.ast.Expression)8 SuperMethodInvocation (com.google.devtools.j2objc.ast.SuperMethodInvocation)8 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)8 ExecutableElement (javax.lang.model.element.ExecutableElement)7 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)5 TypeLiteral (com.google.devtools.j2objc.ast.TypeLiteral)5 TypeElement (javax.lang.model.element.TypeElement)5 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)4 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)4 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)4 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)3 VariableElement (javax.lang.model.element.VariableElement)3 ArrayInitializer (com.google.devtools.j2objc.ast.ArrayInitializer)2 Block (com.google.devtools.j2objc.ast.Block)2 CastExpression (com.google.devtools.j2objc.ast.CastExpression)2 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)2