Fix annotations on Java elements in reflection

#KT-10840 Fixed
This commit is contained in:
Alexander Udalov
2016-01-28 16:38:51 +03:00
parent b946d725d7
commit 2a5b4d2c83
6 changed files with 45 additions and 5 deletions
@@ -0,0 +1,11 @@
@Anno("J")
public class J {
@Anno("foo")
public static int foo = 42;
@Anno("bar")
public static void bar() {}
@Anno("constructor")
public J() {}
}
@@ -0,0 +1,12 @@
import kotlin.test.assertEquals
annotation class Anno(val value: String)
fun box(): String {
assertEquals("[@Anno(value=J)]", J::class.annotations.toString())
assertEquals("[@Anno(value=foo)]", J::foo.annotations.toString())
assertEquals("[@Anno(value=bar)]", J::bar.annotations.toString())
assertEquals("[@Anno(value=constructor)]", ::J.annotations.toString())
return "OK"
}
@@ -581,6 +581,12 @@ public class BlackBoxWithJavaCodegenTestGenerated extends AbstractBlackBoxCodege
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxWithJava/reflection"), Pattern.compile("^([^\\.]+)$"), true);
}
@TestMetadata("annotationsOnJavaMembers")
public void testAnnotationsOnJavaMembers() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxWithJava/reflection/annotationsOnJavaMembers/");
doTestWithJava(fileName);
}
@TestMetadata("callInstanceJavaMethod")
public void testCallInstanceJavaMethod() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxWithJava/reflection/callInstanceJavaMethod/");
@@ -23,7 +23,7 @@ import org.jetbrains.kotlin.load.java.structure.JavaElement
import org.jetbrains.kotlin.load.java.structure.reflect.ReflectJavaElement
object RuntimeSourceElementFactory : JavaSourceElementFactory {
private class RuntimeSourceElement(override val javaElement: ReflectJavaElement) : JavaSourceElement {
class RuntimeSourceElement(override val javaElement: ReflectJavaElement) : JavaSourceElement {
override fun toString() = javaClass.name + ": " + javaElement.toString()
override fun getContainingFile(): SourceFile = SourceFile.NO_SOURCE_FILE
}
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.load.java.structure.JavaAnnotationArgument
import org.jetbrains.kotlin.name.Name
import java.lang.reflect.Method
class ReflectJavaAnnotation(private val annotation: Annotation) : ReflectJavaElement(), JavaAnnotation {
class ReflectJavaAnnotation(val annotation: Annotation) : ReflectJavaElement(), JavaAnnotation {
override fun findArgument(name: Name): JavaAnnotationArgument? {
return getArgumentValue(annotation.annotationClass.java.getDeclaredMethod(name.asString()))
}
@@ -17,6 +17,8 @@
package kotlin.reflect.jvm.internal
import org.jetbrains.kotlin.descriptors.annotations.Annotated
import org.jetbrains.kotlin.load.java.components.RuntimeSourceElementFactory
import org.jetbrains.kotlin.load.java.structure.reflect.ReflectJavaAnnotation
import org.jetbrains.kotlin.load.kotlin.reflect.ReflectAnnotationSource
import kotlin.reflect.KAnnotatedElement
@@ -24,7 +26,16 @@ internal interface KAnnotatedElementImpl : KAnnotatedElement {
val annotated: Annotated
override val annotations: List<Annotation>
get() = annotated.annotations.map {
(it.source as? ReflectAnnotationSource)?.annotation
}.filterNotNull()
get() = annotated.annotations.mapNotNull {
val source = it.source
when (source) {
is ReflectAnnotationSource -> {
source.annotation
}
is RuntimeSourceElementFactory.RuntimeSourceElement -> {
(source.javaElement as? ReflectJavaAnnotation)?.annotation
}
else -> null
}
}
}