Skip to content

Commit

Permalink
Don't map Java types in annotation parameters
Browse files Browse the repository at this point in the history
Users may specify Java types explicitly by instances of `Class<T>`.
The situation is similar to `getClassDeclarationByName` where we have
decided to keep those Java types not mapped.

It would be troublesome if users try to use reflection on types that
were mapped to Kotlin builtins, becuase some of those builtins don't
even exist in classpath.

(cherry picked from commit 39cc187)
  • Loading branch information
ting-yuan authored and KSP Auto Pick committed Oct 28, 2021
1 parent 9af75b8 commit a48e25e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,37 @@ class KSAnnotationJavaImpl private constructor(val psi: PsiAnnotation) : KSAnnot
presentValueArguments.plus(argumentsFromDefault)
}

/*
* Don't map Java types in annotation parameters
*
* Users may specify Java types explicitly by instances of `Class<T>`.
* The situation is similar to `getClassDeclarationByName` where we have
* decided to keep those Java types not mapped.
*
* It would be troublesome if users try to use reflection on types that
* were mapped to Kotlin builtins, becuase some of those builtins don't
* even exist in classpath.
*
* Therefore, ResolverImpl.resolveJavaType cannot be used.
*/
private fun resolveJavaTypeSimple(psiType: PsiType): KSType {
return when (psiType) {
is PsiPrimitiveType -> {
ResolverImpl.instance.getClassDeclarationByName(psiType.boxedTypeName!!)!!.asStarProjectedType()
}
is PsiArrayType -> {
val componentType = resolveJavaTypeSimple(psiType.componentType)
val componentTypeRef = ResolverImpl.instance.createKSTypeReferenceFromKSType(componentType)
val typeArgs = listOf(ResolverImpl.instance.getTypeArgument(componentTypeRef, Variance.INVARIANT))
ResolverImpl.instance.builtIns.arrayType.replace(typeArgs)
}
else -> {
ResolverImpl.instance.getClassDeclarationByName(psiType.canonicalText)?.asStarProjectedType()
?: KSErrorType
}
}
}

private fun calcValue(value: PsiAnnotationMemberValue?): Any? {
if (value is PsiAnnotation) {
return getCached(value)
Expand All @@ -118,18 +149,8 @@ class KSAnnotationJavaImpl private constructor(val psi: PsiAnnotation) : KSAnnot
}
}
return when (result) {
is PsiPrimitiveType -> {
// map Java primitives like int and short to Kotlin counterparts like kotlin.Int, kotlin.Short.
KSTypeImpl.getCached(ResolverImpl.instance.resolveJavaType(result))
}
is PsiArrayType -> {
// map Java Array to Kotlin Array.
// String[] -> Array<String> with platform nullability.
KSTypeImpl.getCached(ResolverImpl.instance.resolveJavaType(result))
}
is PsiType -> {
ResolverImpl.instance.getClassDeclarationByName(result.canonicalText)?.asStarProjectedType()
?: KSErrorType
resolveJavaTypeSimple(result)
}
is PsiLiteralValue -> {
result.value
Expand Down
6 changes: 3 additions & 3 deletions compiler-plugin/testData/api/annotationWithJavaTypeValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
// EXPECTED:
// JavaAnnotated
// JavaAnnotation ->
// primitives = [Char, Boolean, Byte, Short, Int, Long, Float, Double]
// primitives = [Character, Boolean, Byte, Short, Integer, Long, Float, Double]
// objects = [Character, Boolean, Byte, Short, Integer, Long, Float, Double]
// primitiveArrays = [(CharArray..CharArray?), (BooleanArray..BooleanArray?), (ByteArray..ByteArray?), (ShortArray..ShortArray?), (IntArray..IntArray?), (LongArray..LongArray?), (FloatArray..FloatArray?), (DoubleArray..DoubleArray?)]
// objectArrays = [(Array<(Char..Char?)>..Array<out (Char..Char?)>?), (Array<(Boolean..Boolean?)>..Array<out (Boolean..Boolean?)>?), (Array<(Byte..Byte?)>..Array<out (Byte..Byte?)>?), (Array<(Short..Short?)>..Array<out (Short..Short?)>?), (Array<(Int..Int?)>..Array<out (Int..Int?)>?), (Array<(Long..Long?)>..Array<out (Long..Long?)>?), (Array<(Float..Float?)>..Array<out (Float..Float?)>?), (Array<(Double..Double?)>..Array<out (Double..Double?)>?), (Array<(String..String?)>..Array<out (String..String?)>?), (Array<(Any..Any?)>..Array<out (Any..Any?)>?)]
// primitiveArrays = [Array<Character>, Array<Boolean>, Array<Byte>, Array<Short>, Array<Integer>, Array<Long>, Array<Float>, Array<Double>]
// objectArrays = [Array<Character>, Array<Boolean>, Array<Byte>, Array<Short>, Array<Integer>, Array<Long>, Array<Float>, Array<Double>, Array<String>, Array<Object>]
// END
// FILE: a.kt

Expand Down

0 comments on commit a48e25e

Please sign in to comment.