Skip to content

Commit 9497815

Browse files
tapankavasthiTapan Avasthi
andauthored
KTLN-645: Nested Declarations in Annotations in Kotlin (#594)
Co-authored-by: Tapan Avasthi <[email protected]>
1 parent a75e5d1 commit 9497815

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.annotations
2+
3+
annotation class Parent(val type: Type) {
4+
annotation class Child1(val prop1: String)
5+
annotation class Child2(val prop2: Int)
6+
enum class Type { TYPE1, TYPE2 }
7+
}
8+
9+
@Parent(Parent.Type.TYPE1)
10+
@Parent.Child1("sample prop")
11+
@Parent.Child2(prop2 = 1)
12+
class ClassUsingNestedAnnotation {
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.annotations
2+
3+
import org.junit.jupiter.api.Test
4+
import kotlin.reflect.full.findAnnotation
5+
import kotlin.test.assertEquals
6+
7+
class NestedDeclarationUnitTest {
8+
9+
10+
@Test
11+
fun `given nested declarations in annotation, when check for parent-level member, should get expected result`() {
12+
val parentAnnotation = ClassUsingNestedAnnotation::class.findAnnotation<Parent>()
13+
assertEquals(Parent.Type.TYPE1, parentAnnotation?.type)
14+
}
15+
16+
@Test
17+
fun `given nested declarations in annotation, when check for child1 annotation, should get expected result`() {
18+
val child1Annotation = ClassUsingNestedAnnotation::class.findAnnotation<Parent.Child1>()
19+
assertEquals("sample prop", child1Annotation?.prop1)
20+
}
21+
22+
@Test
23+
fun `given nested declarations in annotation, when check for child2 annotation, should get expected result`() {
24+
val child2Annotation = ClassUsingNestedAnnotation::class.findAnnotation<Parent.Child2>()
25+
assertEquals(1, child2Annotation?.prop2)
26+
}
27+
}

0 commit comments

Comments
 (0)