Using nested annotations for key-value pairs in a custom annotation

Introduction

In my article “Using a hashmap in a custom annotation” I explained how to use a HashMap in an annotation using an Enum Constant.

Nested annotations can also be used to map key-value pairs.

List of supported Types in Annotatio…


This content originally appeared on DEV Community and was authored by André Laugks

Introduction

In my article "Using a hashmap in a custom annotation" I explained how to use a HashMap in an annotation using an Enum Constant.

Nested annotations can also be used to map key-value pairs.

List of supported Types in Annotations

Annotations

Two customs annotations are required. The first annotation (e.g. MapItem) containing a key-value pair and the second annotation (e.g. MapItems) containing a list of MapItem annotations.

Custom Annotation @MapItem

The annotation @MapItem represents a single key-value pair.

@Target(ElementType.FIELD)
public @interface MapItem {

    String key();
    String value();
}

Custom Annotation @MapItems

The annotation @MapItems defines a list of MapItem.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MapItems {

    MapItem[] items();
}

Functional Test

The list of @MapItem annotations are set in the @MapItems annotation.

class ExampleDto {

    @MapItems(items = {
        @MapItem(key = "1", value = "MALE"),
        @MapItem(key = "2", value = "FEMALE"),
        @MapItem(key = "6", value = "DIVERS")
    })
    public String salutation;
}

The MapItemsTest tests the MapItems annotation. The test is done on the salutation field.

To demonstrate how the list of @MapItem can be used, I create a HashMap from @MapItem and compare it to an expected HashMap.

class MapItemsTest {

    @Test
    void testMapItems() throws NoSuchFieldException {

        Field field = ExampleDto.class.getDeclaredField("salutation");
        field.setAccessible(true);

        MapItems annotation = field.getAnnotation(MapItems.class);

        Map<String, String> mappingItems = Arrays
                .stream(annotation.items())
                .collect(
                    Collectors.toMap(
                        MapItem::key,
                        MapItem::value
                    )
        );

        assertEquals(
            new HashMap<>() {{
                put("1", "MALE");
                put("2", "FEMALE");
                put("6", "DIVERS");
            }},
            mappingItems
        );
    }
}

Conclusion

Pro

It is a smart solution and easy to implement.

Contra

If the key-value pairs are to be used in a validator, for example, they must be fetched indirectly.

Full example

https://github.com/alaugks/article-annotation-key-value-pairs

Related article


This content originally appeared on DEV Community and was authored by André Laugks


Print Share Comment Cite Upload Translate Updates
APA

André Laugks | Sciencx (2025-01-19T18:44:22+00:00) Using nested annotations for key-value pairs in a custom annotation. Retrieved from https://www.scien.cx/2025/01/19/using-nested-annotations-for-key-value-pairs-in-a-custom-annotation/

MLA
" » Using nested annotations for key-value pairs in a custom annotation." André Laugks | Sciencx - Sunday January 19, 2025, https://www.scien.cx/2025/01/19/using-nested-annotations-for-key-value-pairs-in-a-custom-annotation/
HARVARD
André Laugks | Sciencx Sunday January 19, 2025 » Using nested annotations for key-value pairs in a custom annotation., viewed ,<https://www.scien.cx/2025/01/19/using-nested-annotations-for-key-value-pairs-in-a-custom-annotation/>
VANCOUVER
André Laugks | Sciencx - » Using nested annotations for key-value pairs in a custom annotation. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/19/using-nested-annotations-for-key-value-pairs-in-a-custom-annotation/
CHICAGO
" » Using nested annotations for key-value pairs in a custom annotation." André Laugks | Sciencx - Accessed . https://www.scien.cx/2025/01/19/using-nested-annotations-for-key-value-pairs-in-a-custom-annotation/
IEEE
" » Using nested annotations for key-value pairs in a custom annotation." André Laugks | Sciencx [Online]. Available: https://www.scien.cx/2025/01/19/using-nested-annotations-for-key-value-pairs-in-a-custom-annotation/. [Accessed: ]
rf:citation
» Using nested annotations for key-value pairs in a custom annotation | André Laugks | Sciencx | https://www.scien.cx/2025/01/19/using-nested-annotations-for-key-value-pairs-in-a-custom-annotation/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.