Reflection: change order of arguments of inner generic type

As in KClassifier.createType and everywhere in the compiler, specify arguments
for the innermost type first. This is more convenient to use because generally
the construction/introspection of such type starts from the innermost class
anyway (i.e. something like generateSequence can be used, without the need to
call .reverse() in the end)
This commit is contained in:
Alexander Udalov
2016-08-02 13:28:49 -07:00
parent 89d69bc7eb
commit a7f4037206
7 changed files with 95 additions and 10 deletions
@@ -47,9 +47,7 @@ class ReflectJavaClassifierType(public override val reflectType: Type) : Reflect
get() = with(reflectType) { this is Class<*> && getTypeParameters().isNotEmpty() }
override val typeArguments: List<JavaType>
get() = generateSequence({ reflectType as? ParameterizedType }, { it.ownerType as? ParameterizedType }).flatMap {
it.actualTypeArguments.asSequence().map { ReflectJavaType.create(it) }
}.toList()
get() = reflectType.parameterizedTypeArguments.map(ReflectJavaType.Factory::create)
override val annotations: Collection<JavaAnnotation>
get() {
@@ -20,6 +20,8 @@ import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import java.lang.reflect.Array
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
val Class<*>.safeClassLoader: ClassLoader
get() = classLoader ?: ClassLoader.getSystemClassLoader()
@@ -74,3 +76,16 @@ val Class<*>.desc: String
fun Class<*>.createArrayType(): Class<*> =
Array.newInstance(this, 0).javaClass
/**
* @return all arguments of a parameterized type, including those of outer classes in case this type represents an inner generic.
* The returned list starts with the arguments to the innermost class, then continues with those of its outer class, and so on.
* For example, for the type `Outer<A, B>.Inner<C, D>` the result would be `[C, D, A, B]`.
*/
val Type.parameterizedTypeArguments: List<Type>
get() {
if (this !is ParameterizedType) return emptyList()
if (ownerType == null) return actualTypeArguments.toList()
return generateSequence(this) { it.ownerType as? ParameterizedType }.flatMap { it.actualTypeArguments.asSequence() }.toList()
}