Skip to content

Commit

Permalink
fix variance and classifier reference for Java unbounded type argumen…
Browse files Browse the repository at this point in the history
…t <?>
  • Loading branch information
neetopia committed Oct 9, 2020
1 parent c34bf72 commit 728505e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class ResolveJavaTypeProcessor : AbstractTestProcessor() {
}

override fun visitFunctionDeclaration(function: KSFunctionDeclaration, data: Unit) {
if (function.simpleName.asString() == "wildcardParam") {
function.parameters[0].type!!.accept(this, Unit)
}
function.returnType?.accept(this, Unit)
}

Expand All @@ -56,9 +59,9 @@ class ResolveJavaTypeProcessor : AbstractTestProcessor() {
}

fun KSTypeReference.render(): String {
val sb = StringBuilder(this.resolve()?.declaration?.qualifiedName?.asString() ?: "<ERROR>")
if (this.resolve()?.arguments?.isNotEmpty() == true) {
sb.append("<${this.resolve()!!.arguments.map {
val sb = StringBuilder(this.resolve().declaration.qualifiedName?.asString() ?: "<ERROR>")
if (this.resolve().arguments.isNotEmpty()) {
sb.append("<${this.resolve().arguments.map {
when (it.variance) {
Variance.STAR -> "*"
Variance.INVARIANT -> ""
Expand All @@ -67,7 +70,7 @@ class ResolveJavaTypeProcessor : AbstractTestProcessor() {
} + it.type?.render()
}.joinToString(", ")}>")
}
if (this.resolve()?.nullability != Nullability.NOT_NULL) {
if (this.resolve().nullability != Nullability.NOT_NULL) {
sb.append("?")
}
return sb.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class KSTypeArgumentJavaImpl private constructor(val psi: PsiType) : KSTypeArgum
when {
psi.isExtends -> Variance.COVARIANT
psi.isSuper -> Variance.CONTRAVARIANT
psi.bound == null -> Variance.COVARIANT
else -> Variance.INVARIANT
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ import com.google.devtools.ksp.symbol.impl.KSObjectCache
import com.google.devtools.ksp.symbol.impl.binary.KSClassDeclarationDescriptorImpl
import com.google.devtools.ksp.symbol.impl.binary.KSClassifierReferenceDescriptorImpl
import com.google.devtools.ksp.symbol.impl.kotlin.KSErrorType
import com.google.devtools.ksp.symbol.impl.kotlin.KSTypeImpl
import com.google.devtools.ksp.symbol.impl.toLocation
import org.jetbrains.kotlin.descriptors.NotFoundClasses
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.typeUtil.makeNullable

class KSTypeReferenceJavaImpl private constructor(val psi: PsiType) : KSTypeReference {
companion object : KSObjectCache<PsiType, KSTypeReferenceJavaImpl>() {
Expand Down Expand Up @@ -85,6 +87,7 @@ class KSTypeReferenceJavaImpl private constructor(val psi: PsiType) : KSTypeRefe
)
}
}
null -> KSClassifierReferenceDescriptorImpl.getCached((ResolverImpl.instance.builtIns.anyType as KSTypeImpl).kotlinType.makeNullable())
else -> throw IllegalStateException()
}
}
Expand Down
4 changes: 4 additions & 0 deletions compiler-plugin/testData/api/resolveJavaType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
// EXPECTED:
// kotlin.Int
// kotlin.String?
// kotlin.collections.MutableSet<out kotlin.Any?>?
// kotlin.Unit
// kotlin.IntArray?
// C.T?
// C.PFun.P?
Expand All @@ -45,6 +47,8 @@ public class C<T> {

public String strFun() {}

public void wildcardParam(Set<?> param1) {}

public int[] intArrayFun() {}

public T TFoo() {}
Expand Down

0 comments on commit 728505e

Please sign in to comment.