This discussion is archived
2 Replies Latest reply: Feb 2, 2011 7:23 AM by 802316 RSS

Recovering Annotations From a Class

836145 Newbie
Currently Being Moderated
Hello,

I have to find out which annotations where used inside a class. The code I am using is a small twik version of the java annotation tutorial [http://download.oracle.com/javase/tutorial/java/javaOO/annotations.html] .

My Test file is:
package annotationlogger.example;

@ClassPreamble (
   author = "Henry",
   date = "02/02/2011",
   currentRevision = 1,
   lastModified = "02/02/2011",
   lastModifiedBy = "Henry",
   reviewers = {"Alice", "Bob", "Cindy"} // Note array notation
)
class MyFoo {

    @MyTest protected int MyField;

    @Test public static void m1(@MyTest int I) { }

    @SuppressWarnings("unchecked")
    public static void m2() { }
    @Test public static void m3() {
        throw new RuntimeException("Boom");
    }
    public static void m4() { }
    @Test public static void m5() { }
    public static void m6() { }
    @Test public static void m7() {
        throw new RuntimeException("Crash");
    }
    public static void m8() { }
}
My main code to acquire the annotation would be:
package annotationlogger.example;

import java.lang.annotation.*;

public class RunMyTests {
   public static void main(String[] args) throws Exception {
      Class TestClass = Class.forName("annotationlogger.example.MyFoo");
      Annotation[] A = TestClass.getAnnotations();
      System.out.println(A.length);
      for ( int i = 0; i<A.length; i++ ) {
          System.out.println(A);
}
}
}
I am using reflection on the Class.forName, because I want to be able to test any class without chaging the source code later on.

The problem is that the getAnnotations() method is returning no annotations. I stfw and the forums for a similar problem, but I couldnt find anything to help me.

Please if someone could give me some insight It would help me a lot.

P.S.: Sorry my bad english, it is not my first language.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
  • 1. Re: Retrieving Annotations From a Class
    tschodt Pro
    Currently Being Moderated
    833142 wrote:
    I have to find out which annotations where used inside a class. The code I am using is a small tweak version of the [url http://download.oracle.com/javase/tutorial/java/javaOO/annotations.html]java annotation tutorial.
    package annotationlogger.example;
    
    @ClassPreamble (
    author = "Henry",
    )
    class MyFoo {
    }
    I am using reflection on the Class.forName, because I want to be able to test any class without chaging the source code later on.

    The problem is that the getAnnotations() method is returning no annotations. I stfw and the forums for a similar problem, but I couldnt find anything to help me.

    Please if someone could give me some insight It would help me a lot. [url http://download.oracle.com/javase/tutorial/java/javaOO/annotations.html]java annotation tutorial at the bottom of the page
    package annotationlogger.example;
    
    @Retention(RetentionPolicy.RUNTIME) // To make annotation information available at runtime
    @ClassPreamble (
       author = "Henry",
    )
    class MyFoo {
    }
  • 2. Re: Recovering Annotations From a Class
    802316 Pro
    Currently Being Moderated
    833142 wrote:
    The problem is that the getAnnotations() method is returning no annotations. I stfw and the forums for a similar problem, but I couldnt find anything to help me.
    You don't have any RUNTIME annotations on the class itself.
    There is no function to give you the annotations you have used anywhere. Reflections will only give you annotations which have been marked with RUNTIME retention.

    Another way to get this information is to use a byte code reader like the ASM library.
    This will give you any annotation (with CLASS or RUNTIME retention policy) anywhere in the class.
    To get annotations with source retention, you need to read the source.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points