use of com.google.devtools.j2objc.ast.Block in project j2objc by google.
the class EnumRewriter method addValueOfMethod.
private void addValueOfMethod(EnumDeclaration node) {
TypeElement type = node.getTypeElement();
ExecutableElement method = ElementUtil.findMethod(type, "valueOf", "java.lang.String");
assert method != null : "Can't find valueOf method on enum type.";
String typeName = nameTable.getFullName(type);
int numConstants = node.getEnumConstants().size();
VariableElement nameParam = GeneratedVariableElement.newParameter("name", method.getParameters().get(0).asType(), method);
MethodDeclaration methodDecl = new MethodDeclaration(method);
methodDecl.addParameter(new SingleVariableDeclaration(nameParam));
Block body = new Block();
methodDecl.setBody(body);
StringBuilder impl = new StringBuilder();
if (numConstants > 0) {
impl.append(UnicodeUtils.format(" for (int i = 0; i < %s; i++) {\n" + " %s *e = %s_values_[i];\n" + " if ([name isEqual:[e name]]) {\n" + " return e;\n" + " }\n" + " }\n", numConstants, typeName, typeName));
}
impl.append(" @throw create_JavaLangIllegalArgumentException_initWithNSString_(name);\n" + " return nil;");
body.addStatement(new NativeStatement(impl.toString()));
node.addBodyDeclaration(methodDecl);
}
use of com.google.devtools.j2objc.ast.Block in project j2objc by google.
the class EnumRewriter method addValuesMethod.
private void addValuesMethod(EnumDeclaration node) {
TypeElement type = node.getTypeElement();
ExecutableElement method = ElementUtil.findMethod(type, "values");
assert method != null : "Can't find values method on enum type.";
String typeName = nameTable.getFullName(type);
MethodDeclaration methodDecl = new MethodDeclaration(method);
Block body = new Block();
methodDecl.setBody(body);
body.addStatement(new NativeStatement(UnicodeUtils.format(" return [IOSObjectArray arrayWithObjects:%s_values_ count:%s type:%s_class_()];", typeName, node.getEnumConstants().size(), typeName)));
node.addBodyDeclaration(methodDecl);
}
use of com.google.devtools.j2objc.ast.Block in project j2objc by google.
the class Functionizer method makeAllocatingConstructor.
/**
* Create a wrapper for a constructor that does the object allocation.
*/
private FunctionDeclaration makeAllocatingConstructor(MethodDeclaration method, boolean releasing) {
assert method.isConstructor();
ExecutableElement element = method.getExecutableElement();
TypeElement declaringClass = ElementUtil.getDeclaringClass(element);
String name = releasing ? nameTable.getReleasingConstructorName(element) : nameTable.getAllocatingConstructorName(element);
FunctionDeclaration function = new FunctionDeclaration(name, declaringClass.asType());
function.setLineNumber(method.getLineNumber());
function.setModifiers(ElementUtil.isPrivate(element) ? Modifier.PRIVATE : Modifier.PUBLIC);
function.setReturnsRetained(!releasing);
TreeUtil.copyList(method.getParameters(), function.getParameters());
Block body = new Block();
function.setBody(body);
StringBuilder sb = new StringBuilder(releasing ? "J2OBJC_CREATE_IMPL(" : "J2OBJC_NEW_IMPL(");
sb.append(nameTable.getFullName(declaringClass));
sb.append(", ").append(nameTable.getFunctionName(element));
for (SingleVariableDeclaration param : function.getParameters()) {
sb.append(", ").append(nameTable.getVariableQualifiedName(param.getVariableElement()));
}
sb.append(")");
body.addStatement(new NativeStatement(sb.toString()));
return function;
}
use of com.google.devtools.j2objc.ast.Block 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.ast.Block in project j2objc by google.
the class SwitchRewriter method fixVariableDeclarations.
/**
* Moves all variable declarations above the first case statement.
*/
private void fixVariableDeclarations(SwitchStatement node) {
List<Statement> statements = node.getStatements();
Block block = new Block();
List<Statement> blockStmts = block.getStatements();
for (int i = 0; i < statements.size(); i++) {
Statement stmt = statements.get(i);
if (stmt instanceof VariableDeclarationStatement) {
VariableDeclarationStatement declStmt = (VariableDeclarationStatement) stmt;
statements.remove(i--);
List<VariableDeclarationFragment> fragments = declStmt.getFragments();
for (VariableDeclarationFragment decl : fragments) {
Expression initializer = decl.getInitializer();
if (initializer != null) {
Assignment assignment = new Assignment(new SimpleName(decl.getVariableElement()), TreeUtil.remove(initializer));
statements.add(++i, new ExpressionStatement(assignment));
}
}
blockStmts.add(declStmt);
}
}
if (blockStmts.size() > 0) {
// There is at least one variable declaration, so copy this switch
// statement into the new block and replace it in the parent list.
node.replaceWith(block);
blockStmts.add(node);
}
}
Aggregations