aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/test/kotlin/model/annotations/JavaAnnotationsForParametersTest.kt
blob: d6564343ded7e61da68df85b91cda9f5442a6850 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package model.annotations

import org.jetbrains.dokka.base.signatures.KotlinSignatureUtils.annotations
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.model.*
import org.jetbrains.dokka.utilities.cast
import org.junit.jupiter.api.Test
import utils.AbstractModelTest
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class JavaAnnotationsForParametersTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") {

    @Test
    fun `function with deprecated parameter`() {
        inlineModelTest(
            """
            |public 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 "";
            |}
            |public 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 "";
            |}
            |public 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)
                }
            }
        }
    }

    @Test
    fun `function with generic parameter that has annotated bounds`() {
        inlineModelTest(
            """
            |@Retention(RetentionPolicy.RUNTIME)
            |@Target({ElementType.TYPE_USE})
            |@interface Hello {
            |    public String bar() default "";
            |}
            |public class Test {
            |   public <T extends @Hello(bar = "baz") String> List<T> foo() {
            |        return null;
            |    }
            |}
        """.trimIndent()
        ) {
            with((this / "java" / "Test").cast<DClass>()) {
                with((this / "foo").cast<DFunction>()) {
                    val annotations = ((generics.first().bounds.first() as Nullable).inner as GenericTypeConstructor)
                        .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 `type parameter annotations should be visible even if type declaration has none`() {
        inlineModelTest(
            """
            |@Retention(RetentionPolicy.RUNTIME)
            |@Target(ElementType.PARAMETER)
            |public @interface Hello {
            |    public String bar() default "";
            |}
            |public class Test {
            |    public <T> void foo(java.util.List<@Hello T> param) {}
            |}
        """.trimIndent()
        ) {
            with((this / "java" / "Test").cast<DClass>()) {
                with((this / "foo").cast<DFunction>()) {
                    val paramAnnotations = parameters.first()
                        .type.cast<GenericTypeConstructor>()
                        .projections.first().cast<TypeParameter>()
                        .annotations()
                        .values
                        .flatten()

                    assertEquals(1, paramAnnotations.size)
                    assertEquals(DRI("java", "Hello"), paramAnnotations[0].dri)
                }
            }
        }
    }

    @Test
    fun `type parameter annotations should not be propagated from resolved type`() {
        inlineModelTest(
            """
            |@Retention(RetentionPolicy.RUNTIME)
            |@Target(ElementType.PARAMETER)
            |public @interface Hello {
            |    public String bar() default "";
            |}
            |public class Test {
            |    public <@Hello T> void foo(java.util.List<T> param) {}
            |}
        """.trimIndent()
        ) {
            with((this / "java" / "Test").cast<DClass>()) {
                with((this / "foo").cast<DFunction>()) {
                    val paramAnnotations = parameters.first()
                        .type.cast<GenericTypeConstructor>()
                        .projections.first().cast<TypeParameter>()
                        .annotations()

                    assertTrue(paramAnnotations.isEmpty())
                }
            }
        }
    }
}