diff options
author | Marcin Aman <marcin.aman@gmail.com> | 2021-01-20 10:41:48 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-20 10:41:48 +0100 |
commit | 1f592a7ec2786e0a0b77d224d1414ef3042caae4 (patch) | |
tree | 71d2ec07325d505d2c1b78a71720ddc825b2b87c /plugins/base/src | |
parent | 93520158b4be59f98015d07e8fecaa7e5a49fd99 (diff) | |
download | dokka-1f592a7ec2786e0a0b77d224d1414ef3042caae4.tar.gz dokka-1f592a7ec2786e0a0b77d224d1414ef3042caae4.tar.bz2 dokka-1f592a7ec2786e0a0b77d224d1414ef3042caae4.zip |
Annotations for parameters (#1704)
Diffstat (limited to 'plugins/base/src')
6 files changed, 136 insertions, 9 deletions
diff --git a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt index 1ac519a0..18231601 100644 --- a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt @@ -777,7 +777,10 @@ private class DokkaDescriptorVisitor( null, upperBounds.map { it.toBound() }, setOf(sourceSet), - extra = PropertyContainer.withAll(additionalExtras().toSourceSetDependent().toAdditionalModifiers()) + extra = PropertyContainer.withAll( + additionalExtras().toSourceSetDependent().toAdditionalModifiers(), + getAnnotations().toSourceSetDependent().toAnnotations() + ) ) private suspend fun org.jetbrains.kotlin.descriptors.annotations.Annotations.getPresentableName(): String? = diff --git a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt index cd51e9dd..5b913dcb 100644 --- a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt @@ -360,7 +360,11 @@ class DefaultPsiToDocumentableTranslator( ).toSourceSetDependent(), null, getBound(psiParameter.type), - setOf(sourceSetData) + setOf(sourceSetData), + PropertyContainer.withAll( + psiParameter.annotations.toList().toListOfAnnotations().toSourceSetDependent() + .toAnnotations() + ) ) }, docs.toSourceSetDependent(), @@ -468,7 +472,11 @@ class DefaultPsiToDocumentableTranslator( javadocParser.parseDocumentation(type).toSourceSetDependent(), null, mapBounds(type.bounds), - setOf(sourceSetData) + setOf(sourceSetData), + PropertyContainer.withAll( + type.annotations.toList().toListOfAnnotations().toSourceSetDependent() + .toAnnotations() + ) ) } } diff --git a/plugins/base/src/test/kotlin/model/annotations/JavaAnnotationsForParametersTest.kt b/plugins/base/src/test/kotlin/model/annotations/JavaAnnotationsForParametersTest.kt new file mode 100644 index 00000000..5679010d --- /dev/null +++ b/plugins/base/src/test/kotlin/model/annotations/JavaAnnotationsForParametersTest.kt @@ -0,0 +1,88 @@ +package model.annotations + +import org.jetbrains.dokka.links.DRI +import org.jetbrains.dokka.model.Annotations +import org.jetbrains.dokka.model.DClass +import org.jetbrains.dokka.model.DFunction +import org.junit.jupiter.api.Test +import utils.AbstractModelTest +import kotlin.test.assertEquals + +class JavaAnnotationsForParametersTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") { + + @Test + fun `function with deprecated parameter`() { + inlineModelTest( + """ + |class Test { + | public void fn(@Deprecated String name) {} + |} + """.trimIndent() + ) { + with((this / "java" / "Test").cast<DClass>()) { + with((this / "fn").cast<DFunction>()) { + val dri = + parameters.first().extra[Annotations]?.directAnnotations?.flatMap { it.value }?.map { it.dri } + assertEquals(listOf(DRI("java.lang", "Deprecated")), dri) + } + } + } + } + + @Test + fun `function with parameter that has custom annotation`() { + inlineModelTest( + """ + |@Retention(RetentionPolicy.RUNTIME) + |@Target(ElementType.PARAMETER) + |public @interface Hello { + | public String bar() default ""; + |} + |class Test { + | public void foo(@Hello(bar = "baz") String arg){ } + |} + """.trimIndent() + ) { + with((this / "java" / "Test").cast<DClass>()) { + with((this / "foo").cast<DFunction>()) { + val annotations = + parameters.first().extra[Annotations]?.directAnnotations?.flatMap { it.value } + val driOfHello = DRI("java", "Hello") + val annotationsValues = annotations?.flatMap { it.params.values }?.map { it.toString() }?.toList() + + assertEquals(listOf(driOfHello), annotations?.map { it.dri }) + assertEquals(listOf("baz"), annotationsValues) + } + } + } + } + + @Test + fun `function with annotated generic parameter`() { + inlineModelTest( + """ + |@Retention(RetentionPolicy.RUNTIME) + |@Target(ElementType.TYPE_PARAMETER) + |@interface Hello { + | public String bar() default ""; + |} + |class Test { + | public <@Hello(bar = "baz") T> List<T> foo() { + | return null; + | } + |} + """.trimIndent() + ) { + with((this / "java" / "Test").cast<DClass>()) { + with((this / "foo").cast<DFunction>()) { + val annotations = generics.first().extra[Annotations]?.directAnnotations?.flatMap { it.value } + val driOfHello = DRI("java", "Hello") + val annotationsValues = annotations?.flatMap { it.params.values }?.map { it.toString() }?.toList() + + assertEquals(listOf(driOfHello), annotations?.map { it.dri }) + assertEquals(listOf("baz"), annotationsValues) + } + } + } + } +}
\ No newline at end of file diff --git a/plugins/base/src/test/kotlin/model/annotations/KotlinAnnotationsForParametersTest.kt b/plugins/base/src/test/kotlin/model/annotations/KotlinAnnotationsForParametersTest.kt new file mode 100644 index 00000000..4cc34c09 --- /dev/null +++ b/plugins/base/src/test/kotlin/model/annotations/KotlinAnnotationsForParametersTest.kt @@ -0,0 +1,30 @@ +package model.annotations + +import org.jetbrains.dokka.links.DRI +import org.jetbrains.dokka.model.Annotations +import org.jetbrains.dokka.model.DFunction +import org.junit.jupiter.api.Test +import utils.AbstractModelTest +import kotlin.test.assertEquals + +class KotlinAnnotationsForParametersTest : AbstractModelTest("/src/main/kotlin/annotations/Test.kt", "annotations") { + @Test + fun `generic receiver with annotations`() { + inlineModelTest( + """ + |@Target(AnnotationTarget.TYPE_PARAMETER) + |annotation class Hello(val bar: String) + |fun <@Hello("abc") T> foo(arg: String): List<T> = TODO() + """.trimIndent() + ) { + with((this / "annotations" / "foo").cast<DFunction>()) { + val annotations = generics.first().extra[Annotations]?.directAnnotations?.flatMap { it.value } + val driOfHello = DRI("annotations", "Hello") + val annotationsValues = annotations?.flatMap { it.params.values }?.map { it.toString() }?.toList() + + assertEquals(listOf(driOfHello), annotations?.map { it.dri }) + assertEquals(listOf("abc"), annotationsValues) + } + } + } +}
\ No newline at end of file diff --git a/plugins/base/src/test/kotlin/utils/ModelUtils.kt b/plugins/base/src/test/kotlin/utils/ModelUtils.kt index 87a9c802..24bf87c0 100644 --- a/plugins/base/src/test/kotlin/utils/ModelUtils.kt +++ b/plugins/base/src/test/kotlin/utils/ModelUtils.kt @@ -20,6 +20,7 @@ abstract class AbstractModelTest(val path: String? = null, val pkg: String) : Mo sourceSet { sourceRoots = listOf("src/") analysisPlatform = platform + classpath += jvmStdlibPath!! } } } diff --git a/plugins/base/src/test/kotlin/utils/TestUtils.kt b/plugins/base/src/test/kotlin/utils/TestUtils.kt index 036599c9..a0528acd 100644 --- a/plugins/base/src/test/kotlin/utils/TestUtils.kt +++ b/plugins/base/src/test/kotlin/utils/TestUtils.kt @@ -1,9 +1,10 @@ package utils +import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest import org.jetbrains.dokka.model.* import org.jetbrains.dokka.model.doc.* import org.jetbrains.dokka.model.doc.P -import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertTrue import kotlin.collections.orEmpty @@ -21,7 +22,7 @@ abstract class ModelDSL : BaseAbstractTest() { @TestDSL interface AssertDSL { - infix fun Any?.equals(other: Any?) = this.assertEqual(other) + infix fun Any?.equals(other: Any?) = assertEquals(other, this) infix fun Collection<Any>?.allEquals(other: Any?) = this?.also { c -> c.forEach { it equals other } } ?: run { assert(false) { "Collection is empty" } } infix fun <T> Collection<T>?.exists(e: T) { @@ -35,10 +36,6 @@ interface AssertDSL { fun <T> Collection<T>.assertCount(n: Int, prefix: String = "") = assert(count() == n) { "${prefix}Expected $n, got ${count()}" } - - fun <T> T?.assertEqual(expected: T, prefix: String = "") = assert(this == expected) { - "${prefix}Expected $expected, got $this" - } } inline fun <reified T : Any> Any?.assertIsInstance(name: String): T = |