Skip to content

Commit 2e66b13

Browse files
kstangertomball
authored andcommitted
Fixes translation when an interface is used as a type literal value in an annotation.
Change on 2014/10/23 by kstanger <[email protected]> ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=78355295
1 parent a2a2c24 commit 2e66b13

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

translator/src/main/java/com/google/devtools/j2objc/gen/ObjectiveCImplementationGenerator.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ private void printAnnotationProperties(List<AnnotationTypeMemberDeclaration> mem
669669
private void printTypeAnnotationsMethod(AbstractTypeDeclaration decl) {
670670
List<Annotation> runtimeAnnotations = TreeUtil.getRuntimeAnnotationsList(decl.getAnnotations());
671671
if (runtimeAnnotations.size() > 0) {
672-
println("+ (IOSObjectArray *)__annotations {");
672+
println("\n+ (IOSObjectArray *)__annotations {");
673673
printAnnotationCreate(runtimeAnnotations);
674674
}
675675
}
@@ -679,7 +679,7 @@ private void printMethodAnnotationMethods(Iterable<MethodDeclaration> methods) {
679679
List<Annotation> runtimeAnnotations =
680680
TreeUtil.getRuntimeAnnotationsList(method.getAnnotations());
681681
if (runtimeAnnotations.size() > 0) {
682-
printf("+ (IOSObjectArray *)__annotations_%s {\n", methodKey(method.getMethodBinding()));
682+
printf("\n+ (IOSObjectArray *)__annotations_%s {\n", methodKey(method.getMethodBinding()));
683683
printAnnotationCreate(runtimeAnnotations);
684684
}
685685
printParameterAnnotationMethods(method);
@@ -731,7 +731,7 @@ private void printFieldAnnotationMethods(AbstractTypeDeclaration node) {
731731
TreeUtil.getRuntimeAnnotationsList(field.getAnnotations());
732732
if (!runtimeAnnotations.isEmpty()) {
733733
for (VariableDeclarationFragment var : field.getFragments()) {
734-
printf("+ (IOSObjectArray *)__annotations_%s_ {\n", var.getName().getIdentifier());
734+
printf("\n+ (IOSObjectArray *)__annotations_%s_ {\n", var.getName().getIdentifier());
735735
printAnnotationCreate(runtimeAnnotations);
736736
}
737737
}
@@ -742,7 +742,7 @@ private void printAnnotationCreate(List<Annotation> runtimeAnnotations) {
742742
print(" return [IOSObjectArray arrayWithObjects:(id[]) { ");
743743
printAnnotations(runtimeAnnotations);
744744
printf(" } count:%d type:[IOSClass "
745-
+ "classWithProtocol:@protocol(JavaLangAnnotationAnnotation)]];\n}\n\n",
745+
+ "classWithProtocol:@protocol(JavaLangAnnotationAnnotation)]];\n}\n",
746746
runtimeAnnotations.size());
747747
}
748748

@@ -794,7 +794,11 @@ private void printAnnotationValue(Object value) {
794794
printf("%s_get_%s()", NameTable.getFullName(declaringClass), var.getName());
795795
} else if (value instanceof ITypeBinding) {
796796
ITypeBinding type = (ITypeBinding) value;
797-
printf("[[%s class] getClass]", NameTable.getFullName(type));
797+
if (type.isInterface()) {
798+
printf("[IOSClass classWithProtocol:@protocol(%s)]", NameTable.getFullName(type));
799+
} else {
800+
printf("[[%s class] getClass]", NameTable.getFullName(type));
801+
}
798802
} else if (value instanceof String) {
799803
StringLiteral node = new StringLiteral((String) value);
800804
print(StatementGenerator.generateStringLiteral(node));

translator/src/test/java/com/google/devtools/j2objc/gen/ObjectiveCImplementationGeneratorTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,4 +778,13 @@ public void testInitializeNotInClassExtension() throws IOException {
778778
assertNotInTranslation(translation, "+ (void)initialize OBJC_METHOD_FAMILY_NONE;");
779779
assertOccurrences(translation, "+ (void)initialize", 1);
780780
}
781+
782+
public void testInterfaceTypeLiteralAsAnnotationValue() throws IOException {
783+
addSourceFile(
784+
"import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME)"
785+
+ " @interface Foo { Class<?> value(); }", "Foo.java");
786+
String translation = translateSourceFile(
787+
"@Foo(CharSequence.class) class Test {}", "Test", "Test.m");
788+
assertTranslation(translation, "[IOSClass classWithProtocol:@protocol(JavaLangCharSequence)]");
789+
}
781790
}

0 commit comments

Comments
 (0)