Got empty result from method.getDeclaredAnnotations

上一篇 / 下一篇  2010-07-20 08:05:09 / 个人分类:java

Was very confusing:
@MyAnnotation
myMethod(){
...
}

Then write code like:

...// Get the method
assert(method.getDeclaredAnnotations.length==1);

The test failed. After hours research, it turned out that the reason is the annotation is not declared as RUNTIME. Correct it:

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

By the way getDeclaredAnnotations method doesn't return annotations inherited from parent class.This is how it distinguish from getAnnotations method.

TAG: annotation java method getAnnotations

 

评分:0

我来说两句

Open Toolbar