Search in sources :

Example 21 with SimpleName

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

the class TreeConverter method convertAbstractTypeDeclaration.

private static TreeNode convertAbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration node, AbstractTypeDeclaration newNode) {
    convertBodyDeclaration(node, newNode);
    ITypeBinding typeBinding = node.resolveBinding();
    Set<IMethodBinding> declaredInAst = new HashSet<>();
    for (Object bodyDecl : node.bodyDeclarations()) {
        if (bodyDecl instanceof org.eclipse.jdt.core.dom.MethodDeclaration) {
            declaredInAst.add(((org.eclipse.jdt.core.dom.MethodDeclaration) bodyDecl).resolveBinding());
        }
        newNode.addBodyDeclaration((BodyDeclaration) convert(bodyDecl));
    }
    for (IMethodBinding method : typeBinding.getDeclaredMethods()) {
        if (method.isConstructor() && method.getParameterTypes().length == 0 && !declaredInAst.contains(method)) {
            MethodDeclaration defaultConstructor = new MethodDeclaration(BindingConverter.getExecutableElement(method)).setBody(new Block());
            addImplicitSuperCall(defaultConstructor);
            newNode.addBodyDeclaration(0, defaultConstructor);
            break;
        }
    }
    return newNode.setName((SimpleName) convert(node.getName())).setTypeElement(BindingConverter.getTypeElement(typeBinding));
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Block(com.google.devtools.j2objc.ast.Block) HashSet(java.util.HashSet)

Example 22 with SimpleName

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

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

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

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

the class ComplexExpressionExtractor method handleNode.

private void handleNode(Expression node, Collection<Expression> children) {
    if (node.getParent() instanceof Statement) {
        return;
    }
    int depth = 0;
    for (Expression child : children) {
        Integer childDepth = depths.get(child);
        depth = Math.max(depth, childDepth != null ? childDepth : 1);
    }
    if (depth >= maxDepth) {
        VariableElement newVar = GeneratedVariableElement.newLocalVar("complex$" + count++, node.getTypeMirror(), currentMethod);
        Statement newStmt = new VariableDeclarationStatement(newVar, node.copy());
        assert currentStatement != null;
        TreeUtil.insertBefore(currentStatement, newStmt);
        node.replaceWith(new SimpleName(newVar));
    } else {
        depths.put(node, depth + 1);
    }
}
Also used : 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) DoStatement(com.google.devtools.j2objc.ast.DoStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Aggregations

SimpleName (com.google.devtools.j2objc.ast.SimpleName)48 VariableElement (javax.lang.model.element.VariableElement)26 Expression (com.google.devtools.j2objc.ast.Expression)25 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)16 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)16 TypeMirror (javax.lang.model.type.TypeMirror)15 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)14 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)14 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)13 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)13 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)12 Block (com.google.devtools.j2objc.ast.Block)11 Statement (com.google.devtools.j2objc.ast.Statement)11 ExecutableElement (javax.lang.model.element.ExecutableElement)11 TypeElement (javax.lang.model.element.TypeElement)10 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)9 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)9 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)9 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)8 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)8