/java/

Java Custom Annotations Example

2014-10-01 20:37:47

Example 1

BtbwText.java

package pl.btbw.core.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

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

    String someText() default "Szalek";

}

Post.java

package persistent;

import pl.btbw.core.annotation.BtbwText;

public class Post {

    @BtbwText(someText = "Lorem ipsum dolor sit amet.")
    private String name;

    public Post() {
    }

    public String getName() {
        return name;
    }

}

PostTest.java

package persistent;

import org.junit.Test;
import pl.btbw.core.annotation.BtbwText;

import java.lang.reflect.Field;

import static org.fest.assertions.Assertions.assertThat;

public class PostTest {

    @Test
    public void testBtbwTextAnnotation() throws IllegalAccessException {

        Post obj = new Post();

        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(BtbwText.class)) {
                field.setAccessible(true);
                BtbwText annotation = field.getAnnotation(BtbwText.class);
                field.set(obj, annotation.someText());
                field.setAccessible(false);
            }
        }
        assertThat(obj.getName()).isEqualTo("Lorem ipsum dolor sit amet.");
    }

}

Example 2

ValueOverride.java

package pl.btbw.core;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

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

    String key() default "";

}

Article.java

package persistent;

import pl.btbw.core.ValueOverride;

public class Article {

    @ValueOverride(key = "NAME")
    private String name;

    private String description;

    public Article() {
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }
}

ArticleTest.java

package persistent;

import org.junit.Test;
import pl.btbw.core.ValueOverride;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import static org.fest.assertions.Assertions.assertThat;

public class ArticleTest {

    @Test
    public void testName() throws Exception {

        Article obj = new Article();

        Map

<string, string=""> dataFromRest = new HashMap&lt;&gt;();
        dataFromRest.put("NAME", "article no 1");
        dataFromRest.put("DESCRIPTION", "article long description");

        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(ValueOverride.class)) {
                ValueOverride annotation = field.getAnnotation(ValueOverride.class);
                String value = dataFromRest.get(annotation.key());
                field.setAccessible(true);
                field.set(obj, value);
                field.setAccessible(false);
            }
        }

        assertThat(obj.getName()).isEqualTo("article no 1");
        assertThat(obj.getDescription()).isNull();
    }
}