Search in sources :

Example 1 with ExecutablePair

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

the class EnhancedForRewriter method convertToJavaIteratorLoop.

private void convertToJavaIteratorLoop(EnhancedForStatement node) {
    Expression expression = node.getExpression();
    TypeMirror expressionType = expression.getTypeMirror();
    VariableElement loopVariable = node.getParameter().getVariableElement();
    DeclaredType iterableType = typeUtil.findSupertype(expressionType, "java.lang.Iterable");
    ExecutablePair iteratorMethod = typeUtil.findMethod(iterableType, "iterator");
    DeclaredType iteratorType = (DeclaredType) iteratorMethod.type().getReturnType();
    ExecutablePair hasNextMethod = typeUtil.findMethod(iteratorType, "hasNext");
    ExecutablePair nextMethod = typeUtil.findMethod(iteratorType, "next");
    assert hasNextMethod != null && nextMethod != null;
    VariableElement iteratorVariable = GeneratedVariableElement.newLocalVar("iter__", iteratorType, null);
    MethodInvocation iteratorInvocation = new MethodInvocation(iteratorMethod, TreeUtil.remove(expression));
    VariableDeclarationStatement iteratorDecl = new VariableDeclarationStatement(iteratorVariable, iteratorInvocation);
    MethodInvocation hasNextInvocation = new MethodInvocation(hasNextMethod, new SimpleName(iteratorVariable));
    MethodInvocation nextInvocation = new MethodInvocation(nextMethod, new SimpleName(iteratorVariable));
    Block newLoopBody = makeBlock(TreeUtil.remove(node.getBody()));
    newLoopBody.addStatement(0, new VariableDeclarationStatement(loopVariable, nextInvocation));
    WhileStatement whileLoop = new WhileStatement();
    whileLoop.setExpression(hasNextInvocation);
    whileLoop.setBody(newLoopBody);
    Block block = new Block();
    List<Statement> stmts = block.getStatements();
    stmts.add(iteratorDecl);
    stmts.add(whileLoop);
    replaceLoop(node, block, whileLoop);
}
Also used : PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) Statement(com.google.devtools.j2objc.ast.Statement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Block(com.google.devtools.j2objc.ast.Block) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 2 with ExecutablePair

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

the class DestructorGenerator method addDeallocMethod.

private void addDeallocMethod(AbstractTypeDeclaration node) {
    TypeElement type = node.getTypeElement();
    boolean hasFinalize = hasFinalizeMethod(type);
    List<Statement> releaseStatements = createReleaseStatements(node);
    if (releaseStatements.isEmpty() && !hasFinalize) {
        return;
    }
    ExecutableElement deallocElement = GeneratedExecutableElement.newMethodWithSelector(NameTable.DEALLOC_METHOD, typeUtil.getVoid(), type).addModifiers(Modifier.PUBLIC);
    MethodDeclaration deallocDecl = new MethodDeclaration(deallocElement);
    deallocDecl.setHasDeclaration(false);
    Block block = new Block();
    deallocDecl.setBody(block);
    List<Statement> stmts = block.getStatements();
    if (hasFinalize) {
        String clsName = nameTable.getFullName(type);
        stmts.add(new NativeStatement("JreCheckFinalize(self, [" + clsName + " class]);"));
    }
    stmts.addAll(releaseStatements);
    if (options.useReferenceCounting()) {
        stmts.add(new ExpressionStatement(new SuperMethodInvocation(new ExecutablePair(superDeallocElement))));
    }
    node.addBodyDeclaration(deallocDecl);
}
Also used : NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) TypeElement(javax.lang.model.element.TypeElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Statement(com.google.devtools.j2objc.ast.Statement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Block(com.google.devtools.j2objc.ast.Block) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation)

Example 3 with ExecutablePair

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

the class JavaCloneWriter method createReleaseStatement.

private Statement createReleaseStatement(VariableElement var) {
    if (options.useARC()) {
        TypeMirror voidType = typeUtil.getVoid();
        FunctionElement element = new FunctionElement("JreRelease", voidType, null).addParameters(TypeUtil.ID_TYPE);
        FunctionInvocation invocation = new FunctionInvocation(element, voidType);
        invocation.addArgument(new SimpleName(var));
        return new ExpressionStatement(invocation);
    } else {
        return new ExpressionStatement(new MethodInvocation(new ExecutablePair(releaseMethod), new SimpleName(var)));
    }
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Example 4 with ExecutablePair

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

the class JavaCloneWriter method endVisit.

@Override
public void endVisit(TypeDeclaration node) {
    TypeElement type = node.getTypeElement();
    VariableElement originalVar = GeneratedVariableElement.newParameter("original", type.asType(), null);
    List<Statement> adjustments = getFieldAdjustments(node, originalVar);
    if (adjustments.isEmpty()) {
        return;
    }
    TypeMirror voidType = typeUtil.getVoid();
    ExecutableElement javaCloneElement = GeneratedExecutableElement.newMethodWithSelector(JAVA_CLONE_METHOD, voidType, type).addParameter(originalVar);
    MethodDeclaration declaration = new MethodDeclaration(javaCloneElement);
    declaration.setHasDeclaration(false);
    node.addBodyDeclaration(declaration);
    declaration.addParameter(new SingleVariableDeclaration(originalVar));
    Block body = new Block();
    declaration.setBody(body);
    List<Statement> statements = body.getStatements();
    ExecutableElement javaCloneSuperElement = GeneratedExecutableElement.newMethodWithSelector(JAVA_CLONE_METHOD, voidType, typeUtil.getJavaObject());
    SuperMethodInvocation superCall = new SuperMethodInvocation(new ExecutablePair(javaCloneSuperElement));
    superCall.addArgument(new SimpleName(originalVar));
    statements.add(new ExpressionStatement(superCall));
    statements.addAll(adjustments);
}
Also used : ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) TypeElement(javax.lang.model.element.TypeElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Statement(com.google.devtools.j2objc.ast.Statement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) TypeMirror(javax.lang.model.type.TypeMirror) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 5 with ExecutablePair

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

the class JavaToIOSMethodTranslator method addCopyWithZoneMethod.

private void addCopyWithZoneMethod(TypeDeclaration node) {
    // Create copyWithZone: method.
    GeneratedExecutableElement copyElement = GeneratedExecutableElement.newMethodWithSelector("copyWithZone:", TypeUtil.ID_TYPE, node.getTypeElement());
    MethodDeclaration copyDecl = new MethodDeclaration(copyElement);
    copyDecl.setHasDeclaration(false);
    // Add NSZone *zone parameter.
    VariableElement zoneParam = GeneratedVariableElement.newParameter("zone", NSZONE_TYPE, copyElement);
    copyElement.addParameter(zoneParam);
    copyDecl.addParameter(new SingleVariableDeclaration(zoneParam));
    Block block = new Block();
    copyDecl.setBody(block);
    ExecutableElement cloneElement = ElementUtil.findMethod(typeUtil.getJavaObject(), "clone");
    MethodInvocation invocation = new MethodInvocation(new ExecutablePair(cloneElement), null);
    if (options.useReferenceCounting()) {
        invocation = new MethodInvocation(new ExecutablePair(RETAIN_METHOD), invocation);
    }
    block.addStatement(new ReturnStatement(invocation));
    node.addBodyDeclaration(copyDecl);
}
Also used : GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) Block(com.google.devtools.j2objc.ast.Block) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

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