File tree Expand file tree Collapse file tree
main/kotlin/com/baeldung/annotations
test/kotlin/com/baeldung/annotations Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com.baeldung.annotations
2+
3+ @Target(AnnotationTarget .FIELD )
4+ annotation class Positive
5+
6+ @Target(AnnotationTarget .FIELD )
7+ annotation class AllowedNames (val names : Array <String >)
Original file line number Diff line number Diff line change 1+ package com.baeldung.annotations
2+
3+ class Item (@Positive val amount : Float , @AllowedNames([" Alice" , " Bob" ]) val name : String )
Original file line number Diff line number Diff line change 1+ package com.baeldung.annotations
2+
3+ fun main (args : Array <String >) {
4+ val item = Item (amount = 1.0f , name = " Bob" )
5+ val validator = Validator ()
6+ println (" Is instance valid? ${validator.isValid(item)} " )
7+ }
Original file line number Diff line number Diff line change 1+ package com.baeldung.annotations
2+
3+ /* *
4+ * Naive annotation-based validator.
5+ * @author A.Shcherbakov
6+ */
7+ class Validator () {
8+
9+ /* *
10+ * Return true if every item's property annotated with @Positive is positive and if
11+ * every item's property annotated with @AllowedNames has a value specified in that annotation.
12+ */
13+ fun isValid (item : Item ): Boolean {
14+ val fields = item::class .java.declaredFields
15+ for (field in fields) {
16+ field.isAccessible = true
17+ for (annotation in field.annotations) {
18+ val value = field.get(item)
19+ if (field.isAnnotationPresent(Positive ::class .java)) {
20+ val amount = value as Float
21+ if (amount < 0 ) {
22+ return false
23+ }
24+ }
25+ if (field.isAnnotationPresent(AllowedNames ::class .java)) {
26+ val allowedNames = field.getAnnotation(AllowedNames ::class .java)?.names
27+ val name = value as String
28+ allowedNames?.let {
29+ if (! it.contains(name)) {
30+ return false
31+ }
32+ }
33+ }
34+ }
35+ }
36+ return true
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ package com.baeldung.annotations
2+
3+ import org.junit.jupiter.api.Assertions.assertTrue
4+ import org.junit.jupiter.api.Assertions.assertFalse
5+ import org.junit.jupiter.api.Test
6+
7+
8+ class ValidationUnitTest {
9+
10+ @Test
11+ fun whenAmountIsOneAndNameIsAlice_thenTrue () {
12+ assertTrue(Validator ().isValid(Item (1f , " Alice" )))
13+ }
14+
15+ @Test
16+ fun whenAmountIsOneAndNameIsBob_thenTrue () {
17+ assertTrue(Validator ().isValid(Item (1f , " Bob" )))
18+ }
19+
20+
21+ @Test
22+ fun whenAmountIsMinusOneAndNameIsAlice_thenFalse () {
23+ assertFalse(Validator ().isValid(Item (- 1f , " Alice" )))
24+ }
25+
26+ @Test
27+ fun whenAmountIsMinusOneAndNameIsBob_thenFalse () {
28+ assertFalse(Validator ().isValid(Item (- 1f , " Bob" )))
29+ }
30+
31+ @Test
32+ fun whenAmountIsOneAndNameIsTom_thenFalse () {
33+ assertFalse(Validator ().isValid(Item (1f , " Tom" )))
34+ }
35+
36+ @Test
37+ fun whenAmountIsMinusOneAndNameIsTom_thenFalse () {
38+ assertFalse(Validator ().isValid(Item (- 1f , " Tom" )))
39+ }
40+
41+
42+ }
You can’t perform that action at this time.
0 commit comments