Search in sources :

Example 11 with ExecutablePair

use of com.google.devtools.j2objc.types.ExecutablePair 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 12 with ExecutablePair

use of com.google.devtools.j2objc.types.ExecutablePair in project j2objc by google.

the class TreeConverter method createAnonymousConstructor.

private static MethodDeclaration createAnonymousConstructor(JdtExecutableElement constructorElem, IMethodBinding constructorBinding) {
    MethodDeclaration constructor = new MethodDeclaration(constructorElem);
    Block body = new Block();
    constructor.setBody(body);
    IMethodBinding superConstructorBinding = findSuperConstructor(constructorBinding);
    ExecutablePair superConstructor = new ExecutablePair(BindingConverter.getExecutableElement(superConstructorBinding), BindingConverter.getType(superConstructorBinding));
    SuperConstructorInvocation superCall = new SuperConstructorInvocation(superConstructor);
    body.addStatement(superCall);
    Iterator<? extends VariableElement> params = constructorElem.getParameters().iterator();
    if (constructorElem.hasSuperOuter()) {
        VariableElement param = params.next();
        constructor.addParameter(new SingleVariableDeclaration(param));
        superCall.setExpression(new SimpleName(param));
    }
    while (params.hasNext()) {
        VariableElement param = params.next();
        constructor.addParameter(new SingleVariableDeclaration(param));
        superCall.addArgument(new SimpleName(param));
    }
    assert constructor.getParameters().size() == constructorElem.getParameters().size();
    return constructor;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) SimpleName(com.google.devtools.j2objc.ast.SimpleName) Block(com.google.devtools.j2objc.ast.Block) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 13 with ExecutablePair

use of com.google.devtools.j2objc.types.ExecutablePair 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 14 with ExecutablePair

use of com.google.devtools.j2objc.types.ExecutablePair 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 15 with ExecutablePair

use of com.google.devtools.j2objc.types.ExecutablePair in project j2objc by google.

the class ArrayRewriter method endVisit.

@Override
public void endVisit(InstanceofExpression node) {
    TypeMirror type = node.getRightOperand().getTypeMirror();
    if (!TypeUtil.isArray(type) || ((ArrayType) type).getComponentType().getKind().isPrimitive()) {
        return;
    }
    GeneratedExecutableElement element = GeneratedExecutableElement.newMethodWithSelector("isInstance", typeUtil.getBoolean(), TypeUtil.IOS_CLASS);
    element.addParameter(GeneratedVariableElement.newParameter("object", TypeUtil.ID_TYPE, element));
    MethodInvocation invocation = new MethodInvocation(new ExecutablePair(element), new TypeLiteral(type, typeUtil));
    invocation.addArgument(TreeUtil.remove(node.getLeftOperand()));
    node.replaceWith(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) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Aggregations

ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)25 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)14 SimpleName (com.google.devtools.j2objc.ast.SimpleName)12 ExecutableElement (javax.lang.model.element.ExecutableElement)11 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)10 TypeMirror (javax.lang.model.type.TypeMirror)10 SuperMethodInvocation (com.google.devtools.j2objc.ast.SuperMethodInvocation)9 TypeElement (javax.lang.model.element.TypeElement)9 Expression (com.google.devtools.j2objc.ast.Expression)8 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)7 Block (com.google.devtools.j2objc.ast.Block)5 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)5 SuperConstructorInvocation (com.google.devtools.j2objc.ast.SuperConstructorInvocation)5 TypeLiteral (com.google.devtools.j2objc.ast.TypeLiteral)5 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)5 VariableElement (javax.lang.model.element.VariableElement)5 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)4 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)4 DeclaredType (javax.lang.model.type.DeclaredType)4 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)3