I am in a project where I need to use the above JAVA version. And I don't want to use custom annotation and request its presence during RUNTIME using reflection. So I wrote an annotation, an annotation class, and a test class. The problem is that the annotations are not there. When I use one of the built-in annotations, everything is in order, the annotation is there. When I try to execute my code under JAVA 1.6, everything is fine ...
Is there a known bug in this version of java or do I need to add something else?
BR Marcus
The code:
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Retention(RUNTIME)
public @interface GreetsTheWorld {
public String value();
}
@GreetsTheWorld("Hello, class!")
public class HelloWorld {
@GreetsTheWorld("Hello, field!")
public String greetingState;
@GreetsTheWorld("Hello, constructor!")
public HelloWorld() {
}
@GreetsTheWorld("Hello, method!")
public void sayHi() {
}
}
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class HelloWorldAnnotationTest {
public static void main( String[] args ) throws Exception {
Class<HelloWorld> clazz = HelloWorld.class;
System.out.println( clazz.getAnnotation( GreetsTheWorld.class ) );
Constructor<HelloWorld> constructor = clazz.getConstructor((Class[]) null);
System.out.println(constructor.getAnnotation(GreetsTheWorld.class));
Method method = clazz.getMethod( "sayHi" );
System.out.println(method.getAnnotation(GreetsTheWorld.class));
Field field = clazz.getField("greetingState");
System.out.println(field.getAnnotation(GreetsTheWorld.class));
}
}
source
share