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

Annotations on java parameters do not appear in Documentable #1694

Closed
owengray-google opened this issue Jan 13, 2021 · 0 comments · Fixed by #1710
Closed

Annotations on java parameters do not appear in Documentable #1694

owengray-google opened this issue Jan 13, 2021 · 0 comments · Fixed by #1710
Labels
bug feedback: Google An issue/PR submitted by colleagues at Google, most likely related to the Android API reference docs

Comments

@owengray-google
Copy link
Contributor

Describe the bug

Tl;dr: public void foo(@nullable Integer arg1, @nonnull Integer arg2) cannot be handled in dokka: annotations on parameters don't work in java. Nullability is fairly important, and parameters seem to be the most common use case.

In our repo, we have the following two tests, which pass with the NOTE (which may be a bug in dokka as well):

    @Test
    fun `Method component has annotation and value in 4x Kotlin and Java`() {
        val annotationsK = """
            |annotation class Hello(val bar: String)
            |@Hello("abc")
            |@Hello(bar = "baz")
            |fun foo() = Unit
        """.render().functionAnnotations()
        val annotationsJ = """
            |@Retention(RetentionPolicy.RUNTIME)
            |@Target(ElementType.METHOD)
            |public @interface Hello {
            |    public String bar() default "";
            |}
            |@Hello("abc")
            |@Hello(bar = "baz")
            |public void foo() {}
        """.render(java = true).functionAnnotations()

        for (annotations in listOf(annotationsK, annotationsJ)) {
            val annotationOne = annotations.components().first()
            val parameterOne = annotationOne.data.parameters.item()
            val annotationTwo = annotations.components().last()
            val parameterTwo = annotationTwo.data.parameters.item()

            // NOTE: "value" in java does not match "bar" in kotlin
            if (annotations == annotationsK) assertThat(parameterOne.name).isEqualTo("bar")
            else assertThat(parameterOne.name).isEqualTo("value")
            assertThat(parameterOne.value).isEqualTo("\"abc\"")
            assertThat(parameterTwo.name).isEqualTo("bar")
            assertThat(parameterTwo.value).isEqualTo("\"baz\"")
        }
    }
    @Test
    fun `Property component has annotation and value in 4x Kotlin and Java`() {
        val annotationsK = """
            |annotation class Hello(val bar: String)
            |@Hello("abc")
            |@Hello(bar = "baz")
            |val foo: String = "foofoo"
        """.render().property()!!.annotations()
        val annotationsJ = """
            |@Retention(RetentionPolicy.RUNTIME)
            |@Target(ElementType.FIELD)
            |public @interface Hello {
            |    public String bar() default "";
            |}
            |@Hello("abc")
            |@Hello(bar = "baz")
            |public String foo = "foofoo"
        """.render(java = true).property()!!.annotations()

        for (annotations in listOf(annotationsK, annotationsJ)) {
            val annotationOne = annotations.components().first()
            val parameterOne = annotationOne.data.parameters.item()
            val annotationTwo = annotations.components().last()
            val parameterTwo = annotationTwo.data.parameters.item()

            // NOTE: "value" in java does not match "bar" in kotlin
            if (annotations == annotationsK) assertThat(parameterOne.name).isEqualTo("bar")
            else assertThat(parameterOne.name).isEqualTo("value")
            assertThat(parameterOne.value).isEqualTo("\"abc\"")
            assertThat(parameterTwo.name).isEqualTo("bar")
            assertThat(parameterTwo.value).isEqualTo("\"baz\"")
        }
    }

And we have the following two tests, which fail in the java version, because the DParameter has no Annotations when coming from Java.

    @Test
    fun `Parameter component has annotation and value in 4x Kotlin and Java`() {
        val annotationsK = """
            |annotation class Hello(val bar: String)
            |fun foo(@Hello("abc") @Hello(bar = "baz") arg: String) = Unit
        """.render().function()!!.parameters.single().annotations()
        val annotationsJ = """
            |@Retention(RetentionPolicy.RUNTIME)
            |@Target(ElementType.PARAMETER)
            |public @interface Hello {
            |    public String bar() default "";
            |}
            |public void foo(@Hello("abc") @Hello(bar = "baz") String arg)
        """.render(java = true).function()!!.parameters.single().annotations()

        for (annotations in listOf(annotationsK/*, annotationsJ*/)) {
            val annotationOne = annotations.components().first()
            val parameterOne = annotationOne.data.parameters.item()
            val annotationTwo = annotations.components().last()
            val parameterTwo = annotationTwo.data.parameters.item()

            if (annotations == annotationsK) assertThat(parameterOne.name).isEqualTo("bar")
            else assertThat(parameterOne.name).isEqualTo("value")
            assertThat(parameterOne.value).isEqualTo("\"abc\"")
            assertThat(parameterTwo.name).isEqualTo("bar")
            assertThat(parameterTwo.value).isEqualTo("\"baz\"")
        }
    }
    @Test
    fun `Type parameter component has annotation and value in 4x Kotlin and Java`() {
        val annotationsK = """
            |annotation class Hello(val bar: String)
            |fun <@Hello("abc") @Hello(bar = "baz") T> foo(arg: String): List<T>
        """.render().function()!!.generics.single().annotations()
        val annotationsJ = """
            |@Retention(RetentionPolicy.RUNTIME)
            |@Target(ElementType.TYPE_PARAMETER)
            |public @interface Hello {
            |    public String bar() default "";
            |}
            |public <@Hello("abc") @Hello(bar = "baz") T> List<T> foo()
        """.render(java = true).function()!!.generics.single().annotations()

        for (annotations in listOf(annotationsK/*, annotationsJ*/)) {
            val annotationOne = annotations.components().first()
            val parameterOne = annotationOne.data.parameters.item()
            val annotationTwo = annotations.components().last()
            val parameterTwo = annotationTwo.data.parameters.item()

            if (annotations == annotationsK) assertThat(parameterOne.name).isEqualTo("bar")
            else assertThat(parameterOne.name).isEqualTo("value")
            assertThat(parameterOne.value).isEqualTo("\"abc\"")
            assertThat(parameterTwo.name).isEqualTo("bar")
            assertThat(parameterTwo.value).isEqualTo("\"baz\"")
        }
    }

I believe this bug may also occur with the following input, but I don't have a test for it:

        val annotationsK = """
            |annotation class Hello(val bar: String)
            |fun <T : @Hello("abc") @Hello(bar = "baz") String> foo(arg: String): List<T>
        """.render()
        val annotationsJ = """
            |@Retention(RetentionPolicy.RUNTIME)
            |@Target(ElementType.TYPE_PARAMETER)
            |public @interface Hello {
            |    public String bar() default "";
            |}
            |public <T extends @Hello("abc") @Hello(bar = "baz") String> List<T> foo()
        """.render(java = true)

Screenshots
Screen Shot 2021-01-13 at 3 32 24 PM

Dokka Configuration
Operating system: macOS/Windows/Linux
Build tool: Gradle v6.5.1
dokkaVersion = "1.4.20-dev-36"
Google's dokka-devsite-plugin project

@MarcinAman MarcinAman added the feedback: Google An issue/PR submitted by colleagues at Google, most likely related to the Android API reference docs label Jan 14, 2021
@MarcinAman MarcinAman linked a pull request Jan 19, 2021 that will close this issue
@MarcinAman MarcinAman removed a link to a pull request Jan 20, 2021
@MarcinAman MarcinAman linked a pull request Feb 1, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug feedback: Google An issue/PR submitted by colleagues at Google, most likely related to the Android API reference docs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants