Home > Java > javaTutorial > Using nested annotations for key-value pairs in a custom annotation

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

Susan Sarandon
Release: 2025-01-20 04:04:10
Original
355 people have browsed it

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

Introduction

In my previous article "Using HashMap in Custom Annotations", I explained how to use HashMap in annotations using enumeration constants.

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

List of types supported in annotations

Annotations

Requires two custom annotations. The first annotation (such as MapItem) contains a key-value pair, and the second annotation (such as MapItems) contains a list of MapItem annotations.

Custom annotation @MapItem

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

<code class="language-java">@Target(ElementType.FIELD)
public @interface MapItem {

    String key();
    String value();
}</code>
Copy after login

Custom annotation @MapItems

The annotation @MapItems defines a MapItem list.

<code class="language-java">@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MapItems {

    MapItem[] items();
}</code>
Copy after login

Functional testing

The @MapItem annotation list is set in the @MapItems annotation.

<code class="language-java">class ExampleDto {

    @MapItems(items = {
        @MapItem(key = "1", value = "MALE"),
        @MapItem(key = "2", value = "FEMALE"),
        @MapItem(key = "6", value = "DIVERS")
    })
    public String salutation;
}</code>
Copy after login

MapItemsTest tests MapItems annotations. The test is performed on the salutation field.

To demonstrate how to use a @MapItem list, I create a HashMap from the @MapItem and compare it to the expected HashMap.

<code class="language-java">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
        );
    }
}</code>
Copy after login

Conclusion

Advantages

This is a neat solution and easy to implement.

Disadvantages

For example, if key-value pairs are to be used in a validator, they must be obtained indirectly.

Full example

https://www.php.cn/link/164710e8521a5b39302f816392f05bc2

Related articles

  • Use HashMap in custom annotations
  • Create custom Jackson JsonSerializer and JsonDeserializer for mapped values

The above is the detailed content of Using nested annotations for key-value pairs in a custom annotation. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template