0

I can not get annotations of beans, i'm working with spring framework:

Runnable test class:

public class Main {

    public static void main(String[] args) {
        PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(Test.class,"foo");
        Method m=pd.getReadMethod();
        System.out.println(m.isAnnotationPresent(Annot.class));
    }

}

Bean class;

public class Test {

    private String foo;

        @Annot
        public String getFoo() {
            return foo;
        }

        public void setFoo(String foo) {
            this.foo = foo;
        }

    }

Annotation class:

public @interface Annot {

}

The main class get "false" as output... why?

Tobia
  • 9,165
  • 28
  • 114
  • 219
  • 1
    possible duplicate of [Java : accessing annotations through reflection](http://stackoverflow.com/questions/2286998/java-accessing-annotations-through-reflection) – wastl May 27 '14 at 09:12

1 Answers1

3

Your annotation is missing a runtime retention policy.

Do the following:

@Retention(RetentionPolicy.RUNTIME)
public @interface Annot {

}

Check out this SO question which explains the default policy and what it does. To sum the information in that answer, the default retention policy is CLASS, which means that the annotation is in the bytecode, but does not have to be retained when the class is loaded

Community
  • 1
  • 1
geoand
  • 60,071
  • 24
  • 172
  • 190