Support loading Java annotations with TYPE_PARAMETER target

This commit is contained in:
Denis Zharkov
2016-03-16 15:56:25 +03:00
parent 04eb5ff4f7
commit 26081bf817
17 changed files with 125 additions and 24 deletions
@@ -21,19 +21,19 @@ import org.jetbrains.kotlin.name.FqName
import java.lang.reflect.AnnotatedElement
interface ReflectJavaAnnotationOwner : JavaAnnotationOwner {
val element: AnnotatedElement
val element: AnnotatedElement?
override fun getAnnotations() = getAnnotations(element.declaredAnnotations)
override fun getAnnotations() = element?.declaredAnnotations?.getAnnotations() ?: emptyList()
override fun findAnnotation(fqName: FqName) = findAnnotation(element.declaredAnnotations, fqName)
override fun findAnnotation(fqName: FqName) = element?.declaredAnnotations?.findAnnotation(fqName)
override fun isDeprecatedInJavaDoc() = false
}
fun getAnnotations(annotations: Array<Annotation>): List<ReflectJavaAnnotation> {
return annotations.map { ReflectJavaAnnotation(it) }
fun Array<Annotation>.getAnnotations(): List<ReflectJavaAnnotation> {
return map { ReflectJavaAnnotation(it) }
}
fun findAnnotation(annotations: Array<Annotation>, fqName: FqName): ReflectJavaAnnotation? {
return annotations.firstOrNull { it.annotationClass.java.classId.asSingleFqName() == fqName }?.let { ReflectJavaAnnotation(it) }
fun Array<Annotation>.findAnnotation(fqName: FqName): ReflectJavaAnnotation? {
return firstOrNull { it.annotationClass.java.classId.asSingleFqName() == fqName }?.let { ReflectJavaAnnotation(it) }
}
@@ -16,24 +16,26 @@
package org.jetbrains.kotlin.load.java.structure.reflect
import org.jetbrains.kotlin.load.java.structure.JavaType
import org.jetbrains.kotlin.load.java.structure.JavaTypeParameter
import org.jetbrains.kotlin.load.java.structure.JavaTypeParameterListOwner
import org.jetbrains.kotlin.load.java.structure.JavaTypeProvider
import org.jetbrains.kotlin.load.java.structure.*
import org.jetbrains.kotlin.name.Name
import java.lang.reflect.AnnotatedElement
import java.lang.reflect.Constructor
import java.lang.reflect.Method
import java.lang.reflect.TypeVariable
class ReflectJavaTypeParameter(
val typeVariable: TypeVariable<*>
) : ReflectJavaElement(), JavaTypeParameter {
) : ReflectJavaElement(), JavaTypeParameter, ReflectJavaAnnotationOwner {
override fun getUpperBounds(): List<ReflectJavaClassifierType> {
val bounds = typeVariable.bounds.map { bound -> ReflectJavaClassifierType(bound) }
if (bounds.singleOrNull()?.type == Any::class.java) return emptyList()
return bounds
}
override val element: AnnotatedElement?
// TypeVariable is AnnotatedElement only in JDK8
get() = typeVariable as? AnnotatedElement
override fun getOwner(): JavaTypeParameterListOwner? {
val owner = typeVariable.genericDeclaration
return when (owner) {
@@ -26,9 +26,9 @@ class ReflectJavaValueParameter(
private val name: String?,
private val isVararg: Boolean
) : ReflectJavaElement(), JavaValueParameter {
override fun getAnnotations() = getAnnotations(annotations)
override fun getAnnotations() = annotations.getAnnotations()
override fun findAnnotation(fqName: FqName) = findAnnotation(annotations, fqName)
override fun findAnnotation(fqName: FqName) = annotations.findAnnotation(fqName)
override fun isDeprecatedInJavaDoc() = false