Search in sources :

Example 16 with MethodInvocation

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

the class CastResolver method createCastCheck.

private FunctionInvocation createCastCheck(TypeMirror type, Expression expr) {
    type = typeUtil.erasure(type);
    TypeMirror idType = TypeUtil.ID_TYPE;
    if (TypeUtil.isInterface(type) || isObjectArray(type)) {
        // Interfaces and object arrays requre a isInstance call.
        FunctionElement element = new FunctionElement("cast_check", idType, null).addParameters(idType, TypeUtil.IOS_CLASS.asType());
        FunctionInvocation invocation = new FunctionInvocation(element, idType);
        invocation.addArgument(TreeUtil.remove(expr));
        invocation.addArgument(new TypeLiteral(type, typeUtil));
        return invocation;
    } else if (TypeUtil.isArray(type) || TypeUtil.isDeclaredType(type)) {
        // Primitive array and non-interface type casts are checked using Objective-C's
        // isKindOfClass:.
        TypeElement objcClass = typeUtil.getObjcClass(type);
        FunctionElement checkFunction = new FunctionElement("cast_chk", idType, null).addParameters(idType, idType);
        FunctionInvocation invocation = new FunctionInvocation(checkFunction, idType);
        invocation.addArgument(TreeUtil.remove(expr));
        ExecutableElement classElement = GeneratedExecutableElement.newMethodWithSelector("class", idType, objcClass).addModifiers(Modifier.STATIC);
        MethodInvocation classInvocation = new MethodInvocation(new ExecutablePair(classElement), new SimpleName(objcClass));
        invocation.addArgument(classInvocation);
        return invocation;
    }
    return null;
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) 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) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Example 17 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