From c9125d3f596b26615bddd7fc73227d1422ab73bf Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Sat, 9 Jun 2012 14:48:04 +0200 Subject: [PATCH] correctly call properties on annotation classes #KT-1932 Fixed --- .../jetbrains/jet/codegen/CodegenUtil.java | 6 ++++- .../jetbrains/jet/codegen/JetTypeMapper.java | 9 ++++--- .../testData/codegen/regressions/kt1932.kt | 24 +++++++++++++++++++ .../org/jetbrains/jet/codegen/StdlibTest.java | 4 ++++ 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/codegen/regressions/kt1932.kt diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/CodegenUtil.java b/compiler/backend/src/org/jetbrains/jet/codegen/CodegenUtil.java index 02b2e6f19cf..8f8a89364e5 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/CodegenUtil.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/CodegenUtil.java @@ -41,7 +41,11 @@ public class CodegenUtil { private static final Random RANDOM = new Random(55L); public static boolean isInterface(DeclarationDescriptor descriptor) { - return descriptor instanceof ClassDescriptor && ((ClassDescriptor)descriptor).getKind() == ClassKind.TRAIT; + if (descriptor instanceof ClassDescriptor) { + final ClassKind kind = ((ClassDescriptor) descriptor).getKind(); + return kind == ClassKind.TRAIT || kind == ClassKind.ANNOTATION_CLASS; + } + return false; } public static boolean isInterface(JetType type) { diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/JetTypeMapper.java b/compiler/backend/src/org/jetbrains/jet/codegen/JetTypeMapper.java index 44a6150147e..add8b892eac 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/JetTypeMapper.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/JetTypeMapper.java @@ -792,7 +792,10 @@ public class JetTypeMapper { public JvmPropertyAccessorSignature mapGetterSignature(PropertyDescriptor descriptor, OwnerKind kind) { - String name = PropertyCodegen.getterName(descriptor.getName()); + final DeclarationDescriptor parentDescriptor = descriptor.getContainingDeclaration(); + boolean isAnnotation = parentDescriptor instanceof ClassDescriptor && + ((ClassDescriptor) parentDescriptor).getKind() == ClassKind.ANNOTATION_CLASS; + String name = isAnnotation ? descriptor.getName().getName() : PropertyCodegen.getterName(descriptor.getName()); // TODO: do not generate generics if not needed BothSignatureWriter signatureWriter = new BothSignatureWriter(BothSignatureWriter.Mode.METHOD, true); @@ -801,8 +804,8 @@ public class JetTypeMapper { signatureWriter.writeParametersStart(); - if (kind == OwnerKind.TRAIT_IMPL) { - ClassDescriptor containingDeclaration = (ClassDescriptor) descriptor.getContainingDeclaration(); + if(kind == OwnerKind.TRAIT_IMPL) { + ClassDescriptor containingDeclaration = (ClassDescriptor) parentDescriptor; assert containingDeclaration != null; signatureWriter.writeParameterType(JvmMethodParameterKind.THIS); mapType(containingDeclaration.getDefaultType(), signatureWriter, MapTypeMode.IMPL); diff --git a/compiler/testData/codegen/regressions/kt1932.kt b/compiler/testData/codegen/regressions/kt1932.kt new file mode 100644 index 00000000000..5a83165a102 --- /dev/null +++ b/compiler/testData/codegen/regressions/kt1932.kt @@ -0,0 +1,24 @@ +import java.lang.annotation.Retention +import java.lang.annotation.RetentionPolicy +import java.lang.annotation.Annotation + +Retention(RetentionPolicy.RUNTIME) annotation class foo(val name : String) + +class Test() { + foo("OK") fun hello(input : String) { + } +} + +fun box(): String { + val test = Test() + for (method in javaClass().getMethods()) { + val anns = method?.getAnnotations() as Array + if (!anns.isEmpty()) { + for (ann in anns) { + val fooAnn = ann as foo + return fooAnn.name + } + } + } + return "fail" +} diff --git a/compiler/tests/org/jetbrains/jet/codegen/StdlibTest.java b/compiler/tests/org/jetbrains/jet/codegen/StdlibTest.java index b46008785f9..c40273aa212 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/StdlibTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/StdlibTest.java @@ -320,4 +320,8 @@ public class StdlibTest extends CodegenTestCase { assertEquals(invoke1[0].value(), RetentionPolicy.RUNTIME); assertEquals(invoke1[1].value(), RetentionPolicy.SOURCE); } + + public void testInvokeAnnotationMethod() { + blackBoxFile("regressions/kt1932.kt"); + } }