I would like to use annotations in my application. For this reason I create "hello world" for annotations:
follows example:
public class HelloAnnotation
{
@Foo(bar = "Hello World !")
public String str;
public static void main(final String[] args) throws Exception
{
System.out.println(HelloAnnotation.class.getField("str").getAnnotations().length);
}
}
And this is the Annotation:
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
public @interface Foo
{
public String doTestTarget();
}
My problem is now that getAnnotations() in main is empty. What is wrong with my code?
Add the following to your annotation:
@Retention(RetentionPolicy.RUNTIME)
From the javadoc for @Retention:
the retention policy defaults to
RetentionPolicy.CLASS
From the javadoc for RetentionPolicy:
CLASS
RUNTIME
SOURCE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With