Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get annotation in object java

I'm working in one logic, but i dont know if it is possible to do it, i want to use annotation for this, so this is my code

public class Hola {
    public JSONConverter() {
        String message=  getClass().getAnnotation(HolaAn.class).getMessage();

    }
}


@Target({ElementType.FIELD})
public @interface HolaAn{

    String getMessage();
}




public class MessageTest{

@HolaAn(getMessage= "MUNDO")
private Hola hola;

    @Test
    public void testMessage(){
        hola= new Hola();

    }


}

But i have nullPointerException, i dont know very well how to work with my own annotation, any one can said me if this is possible and how to do it?

like image 709
nicearma Avatar asked Feb 17 '15 13:02

nicearma


People also ask

What is @field annotation in Java?

Use @Field to define a structured data type's field name for an object mapped to NoSql data. Annotation Elements. Table 2-24 describes this annotation's elements.

What is @interface annotation in Java?

Annotation is defined like a ordinary Java interface, but with an '@' preceding the interface keyword (i.e., @interface ). You can declare methods inside an annotation definition (just like declaring abstract method inside an interface). These methods are called elements instead.

How do you inherit annotations?

Annotations, just like methods or fields, can be inherited between class hierarchies. If an annotation declaration is marked with @Inherited , then a class that extends another class with this annotation can inherit it.


2 Answers

First of all, you need to change annotation retention to RUNTIME (default is CLASS), so they may be read reflectively. Change to like this:

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

    String message();

}

You are trying to get annotation from the class, but your annotation are on a field, the only element target. In this example, you are able to get the annotation in this way:

@HolaAn(message = "MUNDO")
private Hola hola;

@Test
public void testMessageOnField() {
    final Field[] fields = HolaTest.class.getDeclaredFields();
    for (final Field field : fields) {
        if (field.isAnnotationPresent(HolaAn.class)) {
            final HolaAn annotation = field.getAnnotation(HolaAn.class);
            Assert.assertTrue(annotation.message().equals("MUNDO"));
        }
    }
}

If you need to get the annotation from the class, change it to something like this:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
public @interface HolaAn {

    String message();

}

Then, you are able to get annotation message like this:

@HolaAn(message = "CLASS")
public class Hola {

    public Hola() {
        final String message = this.getClass().getAnnotation(HolaAn.class).message();
        System.out.println(message);
    }

}

@Test
public void testMessage() {
    hola = new Hola();
}

Or:

@Test
public void testMessageSecondWay() {
    hola = new Hola();
    final Class<?> theClass = hola.getClass();
    if (theClass.isAnnotationPresent(HolaAn.class)) {
        final HolaAn annotation = theClass.getAnnotation(HolaAn.class);
        Assert.assertTrue(annotation.message().equals("CLASS"));
    }
}
like image 141
Bruno Ribeiro Avatar answered Oct 17 '22 05:10

Bruno Ribeiro


You should change your annotation to

@Target({ ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@interface HolaAn {
    String getMessage();
}

This is necessary to compile the annotation to the classfile.

Now you can access your message via:

Field declaredField = new MessageTest().getClass().getDeclaredField(
        "hola");
System.out.println((declaredField.getDeclaredAnnotation(HolaAn.class)
        .getMessage()));
like image 38
Jan Avatar answered Oct 17 '22 04:10

Jan