Java 1.5.0.12 and custom runtime annotations

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:

// The annotation
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Retention(RUNTIME) 
public @interface GreetsTheWorld {
  public String value();
}

// The Annotated Class
@GreetsTheWorld("Hello, class!") 
public class HelloWorld {

  @GreetsTheWorld("Hello, field!") 
  public String greetingState;

  @GreetsTheWorld("Hello, constructor!") 
  public HelloWorld() {
  }

  @GreetsTheWorld("Hello, method!") 
  public void sayHi() {
  }
}

// The test
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 {
    //access the class annotation
    Class<HelloWorld> clazz = HelloWorld.class;
    System.out.println( clazz.getAnnotation( GreetsTheWorld.class ) );

    //access the constructor annotation
    Constructor<HelloWorld> constructor = clazz.getConstructor((Class[]) null);
    System.out.println(constructor.getAnnotation(GreetsTheWorld.class));

    //access the method annotation
    Method method = clazz.getMethod( "sayHi" );
    System.out.println(method.getAnnotation(GreetsTheWorld.class));

    //access the field annotation
    Field field = clazz.getField("greetingState");
    System.out.println(field.getAnnotation(GreetsTheWorld.class));
  }
}
+3
source share
1 answer

, : . , , , java , 1.5. ​​ 1.2, . 1.5 .

+1

Source: https://habr.com/ru/post/1785924/


All Articles