Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of Java contravariance #3092

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions plugins/base/src/test/kotlin/model/JavaTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.jetbrains.dokka.model.doc.Text
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import utils.AbstractModelTest
import utils.assertContains
import utils.assertNotNull
import utils.name
import kotlin.test.assertEquals
Expand Down Expand Up @@ -439,4 +440,48 @@ class JavaTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") {
}
}

@Test
fun variances() {
inlineModelTest(
"""
|public class Foo {
| public void superBound(java.util.List<? super String> param) {}
| public void extendsBound(java.util.List<? extends String> param) {}
| public void unbounded(java.util.List<?> param) {}
|}
""", configuration = configuration
) {
with((this / "java" / "Foo").cast<DClass>()) {
val functionNames = functions.map { it.name }
assertContains(functionNames, "superBound")
assertContains(functionNames, "extendsBound")
assertContains(functionNames, "unbounded")

for (function in functions) {
val param = function.parameters.single()
val type = param.type as GenericTypeConstructor
val variance = type.projections.single()

when (function.name) {
"superBound" -> {
assertTrue(variance is Contravariance<*>)
val bound = (variance as Contravariance<*>).inner
assertEquals((bound as GenericTypeConstructor).dri.classNames, "String")
}
"extendsBound" -> {
assertTrue(variance is Covariance<*>)
val bound = (variance as Covariance<*>).inner
assertEquals((bound as GenericTypeConstructor).dri.classNames, "String")
}
"unbounded" -> {
assertTrue(variance is Covariance<*>)
val bound = (variance as Covariance<*>).inner
assertTrue(bound is JavaObject)
}
}
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,10 @@ internal class DokkaPsiParser(


private fun getVariance(type: PsiWildcardType): Projection = when {
type.isExtends -> Covariance(getBound(type.extendsBound))
type.isSuper -> Contravariance(getBound(type.superBound))
// If the type isn't explicitly bounded, it still has an implicit `extends Object` bound
type.extendsBound != PsiType.NULL -> Covariance(getBound(type.extendsBound))
type.superBound != PsiType.NULL -> Contravariance(getBound(type.superBound))
else -> throw IllegalStateException("${type.presentableText} has incorrect bounds")
}

Expand Down