@Override protected void printStaticConstructorDeclaration(MethodDeclaration m) { String className = NameTable.getFullName(Types.getMethodBinding(m).getDeclaringClass()); StringBuffer sb = new StringBuffer(); sb.append("{\nif (self == [" + className + " class]) {\n"); for (Statement statement : ASTUtil.getStatements(m.getBody())) { sb.append(generateStatement(statement, false)); } sb.append("}\n}"); print("+ (void)initialize " + reindent(sb.toString()) + "\n\n"); }
private void printMethodAnnotationMethods(List<MethodDeclaration> methods) { for (MethodDeclaration method : methods) { List<Annotation> runtimeAnnotations = ASTUtil.getRuntimeAnnotations(ASTUtil.getModifiers(method)); if (runtimeAnnotations.size() > 0) { printf( "+ (IOSObjectArray *)__annotations_%s {\n", methodKey(Types.getMethodBinding(method))); printAnnotationCreate(runtimeAnnotations); } printParameterAnnotationMethods(method); } }
@Override protected String constructorDeclaration(MethodDeclaration m) { String methodBody; IMethodBinding binding = Types.getMethodBinding(m); boolean memDebug = Options.memoryDebug(); List<Statement> statements = ASTUtil.getStatements(m.getBody()); if (binding.getDeclaringClass().isEnum()) { return enumConstructorDeclaration(m, statements, binding); } StringBuffer sb = new StringBuffer("{\n"); int constructorIdx = findConstructorInvocation(statements); int idx = 0; while (idx < constructorIdx) { sb.append(generateStatement(statements.get(idx++), false)); } String superCall = idx == constructorIdx ? generateStatement(statements.get(idx++), false) : "[super init]"; if (idx >= statements.size()) { sb.append("return "); if (memDebug) { sb.append("JreMemDebugAdd("); } sb.append(superCall).append(memDebug ? ");\n}" : ";\n}"); } else { sb.append("if (self = ").append(superCall).append(") {\n"); while (idx < statements.size()) { sb.append(generateStatement(statements.get(idx++), false)); } if (memDebug) { sb.append("JreMemDebugAdd(self);\n"); } sb.append("}\nreturn self;\n}"); } methodBody = sb.toString(); if (invokedConstructors.contains(methodKey(binding))) { return super.constructorDeclaration(m, true) + " " + reindent(methodBody) + "\n\n" + super.constructorDeclaration(m, false) + " {\n return " + generateStatement(createInnerConstructorInvocation(m), false) + ";\n}\n\n"; } else { return super.constructorDeclaration(m, false) + " " + reindent(methodBody) + "\n\n"; } }
private void printParameterAnnotationMethods(MethodDeclaration method) { List<SingleVariableDeclaration> params = ASTUtil.getParameters(method); // Quick test to see if there are any parameter annotations. boolean hasAnnotations = false; for (SingleVariableDeclaration param : params) { List<Annotation> runtimeAnnotations = ASTUtil.getRuntimeAnnotations(ASTUtil.getModifiers(param)); if (runtimeAnnotations.size() > 0) { hasAnnotations = true; break; } } if (hasAnnotations) { // Print array of arrays, with an element in the outer array for each parameter. printf( "+ (IOSObjectArray *)__annotations_%s_params {\n", methodKey(Types.getMethodBinding(method))); print(" return [IOSObjectArray arrayWithObjects:(id[]) { "); for (int i = 0; i < params.size(); i++) { if (i > 0) { print(", "); } SingleVariableDeclaration param = params.get(i); List<Annotation> runtimeAnnotations = ASTUtil.getRuntimeAnnotations(ASTUtil.getModifiers(param)); if (runtimeAnnotations.size() > 0) { print("[IOSObjectArray arrayWithObjects:(id[]) { "); printAnnotations(runtimeAnnotations); printf( " } count:%d type:[IOSClass classWithProtocol:" + "@protocol(JavaLangAnnotationAnnotation)]]", runtimeAnnotations.size()); } else { print( "[IOSObjectArray arrayWithLength:0 " + "type:[IOSClass classWithProtocol:@protocol(JavaLangAnnotationAnnotation)]]"); } } printf( " } count:%d type:[IOSClass classWithProtocol:" + "@protocol(JavaLangAnnotationAnnotation)]];\n}\n", params.size()); } }
private String generateNativeStub(MethodDeclaration m) { IMethodBinding binding = Types.getMethodBinding(m); String methodName = NameTable.getName(binding); return String.format("{\n @throw \"%s method not implemented\";\n}", methodName); }
@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"); }