Kapt: Fix type arguments in JeDeclaredType. In case of type variable, JeTypeVariableType should be returned
(cherry picked from commit ed34ec0)
This commit is contained in:
committed by
Yan Zhulanow
parent
ebcc762ae9
commit
7810678389
@@ -0,0 +1,8 @@
|
||||
interface I<T>
|
||||
|
||||
abstract class A<T> : I<T>
|
||||
|
||||
@Deprecated("")
|
||||
class B : A<String>()
|
||||
|
||||
class C<T : CharSequence> : A<T>()
|
||||
+15
-3
@@ -49,10 +49,22 @@ class JeDeclaredType(
|
||||
get() = psiClass.manager
|
||||
|
||||
override fun getTypeArguments(): List<TypeMirror> {
|
||||
if (psiType.isRaw) return emptyList()
|
||||
|
||||
return when (psiType) {
|
||||
is PsiClassReferenceType -> psiType.parameters.map { it.toJeType(psiManager) }
|
||||
is PsiClassType -> {
|
||||
val substitutor = psiType.resolveGenerics().substitutor
|
||||
val psiClass = psiType.resolve() ?: return psiType.parameters.map { it.toJeType(psiManager) }
|
||||
|
||||
val args = mutableListOf<TypeMirror>()
|
||||
for (typeParameter in psiClass.typeParameters) {
|
||||
val substitutedParameter = substitutor.substitute(typeParameter)
|
||||
if (substitutedParameter != null)
|
||||
args += substitutedParameter.toJeType(psiManager)
|
||||
else
|
||||
args += JeTypeVariableType(PsiTypesUtil.getClassType(typeParameter), typeParameter)
|
||||
}
|
||||
|
||||
args
|
||||
}
|
||||
else -> emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
+25
@@ -20,6 +20,9 @@ import org.jetbrains.kotlin.java.model.elements.*
|
||||
import org.jetbrains.kotlin.java.model.types.JeDeclaredType
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
|
||||
import javax.lang.model.element.AnnotationMirror
|
||||
import javax.lang.model.type.DeclaredType
|
||||
import javax.lang.model.type.TypeMirror
|
||||
import javax.lang.model.type.TypeVariable
|
||||
|
||||
class ProcessorTests : AbstractProcessorTest() {
|
||||
override val testDataDir = "plugins/annotation-processing/testData/processors"
|
||||
@@ -128,4 +131,26 @@ class ProcessorTests : AbstractProcessorTest() {
|
||||
assertTrue(superB.typeArguments.size == 1)
|
||||
assertTrue(interfaceC.typeArguments.size == 1)
|
||||
}
|
||||
|
||||
fun testTypeArguments2() = test("TypeArguments2", "*") { set, roundEnv, env ->
|
||||
val b = env.findClass("B")
|
||||
val bSuperTypes = env.typeUtils.directSupertypes(b.asType())
|
||||
assertEquals(1, bSuperTypes.size)
|
||||
val bASuperTypes = env.typeUtils.directSupertypes(bSuperTypes.first())
|
||||
assertEquals(2, bASuperTypes.size) // Object and I
|
||||
|
||||
fun List<TypeMirror>.iInterface() = first { it.toString().matches("I(<.*>)?".toRegex()) } as DeclaredType
|
||||
|
||||
val bai = bASuperTypes.iInterface()
|
||||
assertEquals(1, bai.typeArguments.size)
|
||||
assertEquals("java.lang.String", bai.typeArguments.first().toString())
|
||||
|
||||
val c = env.findClass("C")
|
||||
val cSuperTypes = env.typeUtils.directSupertypes(c.asType())
|
||||
assertEquals(1, cSuperTypes.size)
|
||||
val cai = env.typeUtils.directSupertypes(cSuperTypes.first()).iInterface()
|
||||
assertEquals(1, cai.typeArguments.size)
|
||||
val typeArg = cai.typeArguments.first()
|
||||
assertTrue(typeArg is TypeVariable)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user