Search in sources :

Example 26 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 require an 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) && needsCastChk(expr, 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 27 with MethodInvocation

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

the class SwitchRewriter method fixEnumValue.

private void fixEnumValue(SwitchStatement node) {
    Expression expr = node.getExpression();
    TypeMirror type = expr.getTypeMirror();
    if (!TypeUtil.isEnum(type)) {
        return;
    }
    DeclaredType enumType = typeUtil.getSuperclass(type);
    ExecutablePair ordinalMethod = typeUtil.findMethod(enumType, "ordinal");
    MethodInvocation invocation = new MethodInvocation(ordinalMethod, TreeUtil.remove(expr));
    node.setExpression(invocation);
}
Also used : Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)27 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)23 ExecutableElement (javax.lang.model.element.ExecutableElement)16 Expression (com.google.devtools.j2objc.ast.Expression)14 TypeElement (javax.lang.model.element.TypeElement)12 SimpleName (com.google.devtools.j2objc.ast.SimpleName)11 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)11 TypeMirror (javax.lang.model.type.TypeMirror)11 SuperMethodInvocation (com.google.devtools.j2objc.ast.SuperMethodInvocation)10 Block (com.google.devtools.j2objc.ast.Block)6 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)6 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)6 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)5 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)5 DeclaredType (javax.lang.model.type.DeclaredType)5 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)4 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)4 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)4 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)4 Statement (com.google.devtools.j2objc.ast.Statement)4