private Statement createInnerConstructorInvocation(MethodDeclaration m) { ConstructorInvocation invocation = m.getAST().newConstructorInvocation(); Types.addBinding(invocation, Types.getBinding(m)); for (SingleVariableDeclaration param : ASTUtil.getParameters(m)) { SimpleName paramName = param.getName(); IVariableBinding paramBinding = Types.getVariableBinding(paramName); SimpleName argName = m.getAST().newSimpleName(paramName.getIdentifier()); Types.addBinding(argName, paramBinding); ASTUtil.getArguments(invocation).add(argName); } return invocation; }
@Override protected void generate(EnumDeclaration node) { List<EnumConstantDeclaration> constants = ASTUtil.getEnumConstants(node); List<MethodDeclaration> methods = Lists.newArrayList(); List<FieldDeclaration> fields = Lists.newArrayList(); MethodDeclaration initializeMethod = null; for (BodyDeclaration decl : ASTUtil.getBodyDeclarations(node)) { if (decl instanceof FieldDeclaration) { fields.add((FieldDeclaration) decl); } else if (decl instanceof MethodDeclaration) { MethodDeclaration md = (MethodDeclaration) decl; if (md.getName().getIdentifier().equals("initialize")) { initializeMethod = md; } else { methods.add(md); } } } syncLineNumbers(node.getName()); // avoid doc-comment String typeName = NameTable.getFullName(node); newline(); for (EnumConstantDeclaration constant : constants) { IVariableBinding var = Types.getVariableBinding(constant.getName()); printf("static %s *%s;\n", typeName, NameTable.getStaticVarQualifiedName(var)); } printf("IOSObjectArray *%s_values;\n", typeName); newline(); printf("@implementation %s\n\n", typeName); printStaticVars(fields, /* isInterface */ false); printStaticReferencesMethod(fields, typeName + "_values"); for (EnumConstantDeclaration constant : constants) { String name = NameTable.getName(constant.getName()); printf("+ (%s *)%s {\n", typeName, name); printf(" return %s_%s;\n", typeName, name); println("}"); } newline(); // Enum constants needs to implement NSCopying. Being singletons, they // can just return self, as long the retain count is incremented. String selfString = Options.useReferenceCounting() ? "[self retain]" : "self"; printf("- (id)copyWithZone:(NSZone *)zone {\n return %s;\n}\n\n", selfString); printStaticFieldAccessors(fields, methods, /* isInterface */ false); printMethodsAndOcni(node, methods, blockComments.get(node)); printf("+ (void)initialize {\n if (self == [%s class]) {\n", typeName); for (int i = 0; i < constants.size(); i++) { EnumConstantDeclaration constant = constants.get(i); List<Expression> args = ASTUtil.getArguments(constant); String name = NameTable.getName(constant.getName()); String constantTypeName = NameTable.getFullName(Types.getMethodBinding(constant).getDeclaringClass()); printf(" %s_%s = [[%s alloc] init", typeName, name, constantTypeName); if (args.isEmpty()) { print("With"); } else { print( StatementGenerator.generateArguments( Types.getMethodBinding(constant), args, fieldHiders, getBuilder().getSourcePosition())); print(" with"); } printf("NSString:@\"%s\" withInt:%d];\n", name, i); } printf(" %s_values = [[IOSObjectArray alloc] initWithObjects:(id[]){ ", typeName); for (EnumConstantDeclaration constant : constants) { printf("%s_%s, ", typeName, NameTable.getName(constant.getName())); } printf( "nil } count:%d type:[IOSClass classWithClass:[%s class]]];\n", constants.size(), typeName); if (initializeMethod != null) { for (Statement s : ASTUtil.getStatements(initializeMethod.getBody())) { printf( " %s", StatementGenerator.generate(s, fieldHiders, false, getBuilder().getSourcePosition())); } } println(" }\n}\n"); // Print generated values and valueOf methods. println("+ (IOSObjectArray *)values {"); printf(" return [IOSObjectArray arrayWithArray:%s_values];\n", typeName); println("}\n"); printf("+ (%s *)valueOfWithNSString:(NSString *)name {\n", typeName); printf(" for (int i = 0; i < [%s_values count]; i++) {\n", typeName); printf(" %s *e = %s_values->buffer_[i];\n", typeName, typeName); printf(" if ([name isEqual:[e name]]) {\n"); printf(" return e;\n"); printf(" }\n"); printf(" }\n"); if (Options.useReferenceCounting()) { printf( " @throw [[[JavaLangIllegalArgumentException alloc] initWithNSString:name]" + " autorelease];\n"); } else { printf(" @throw [[JavaLangIllegalArgumentException alloc] initWithNSString:name];\n"); } printf(" return nil;\n"); println("}\n"); if (!Options.stripReflection()) { printTypeAnnotationsMethod(node); printMetadata(node); } println("@end"); }