Note that there are some explanatory texts on larger screens.

plurals
  1. POJava 1.5.0.12 and custom Annotations at Runtime
    primarykey
    data
    text
    <p>I'm in a project where I need to use the above specific JAVA Version. And I wan't to use a custom annotation and query it's presence during RUNTIME using reflection. So I wrote an annotation, a class to annotate and a test class. The problem ist that, the annotation is not there. When I use one of the built in Annotations, everything is fine, the annotation is there. When I try my code under JAVA 1.6 everything is fine...</p> <p>Is there a known bug in this java version or do I need to add something more?</p> <p>BR Markus</p> <p>The code:</p> <pre><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&lt;HelloWorld&gt; clazz = HelloWorld.class; System.out.println( clazz.getAnnotation( GreetsTheWorld.class ) ); //access the constructor annotation Constructor&lt;HelloWorld&gt; 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)); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload