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);
}
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);
}
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)));
}
}
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);
}
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);
}
Aggregations