Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to retrieve data from Annotation type?

 
tom chansky
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created an Annotation type:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ClassInfo {

public String created();
public String createdBy();
public String lastModified();
public String lastModifiedBy();
public int revision();

}

And then, I created a class that uses that Annotation:

@ClassInfo(
created = "Jan 31 2005",
createdBy = "Me",
lastModified = "Feb 9 2005",
lastModifiedBy = "You",
revision = 3
)
public class Foo {
...
}

Then, I can test that Annotation using the following test class:

public Class test {
public static void main(String[] args) {
System.out.println(Foo.class.isAnnotationPresent(ClassInfo.class));
}
}

However, how do I retrieve the above specific info (i.e. "Jan 31 2005") from that ClassInfo implemented within Foo?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by tom chansky:
...how do I retrieve the above specific info (i.e. "Jan 31 2005") from that ClassInfo implemented within Foo?


Note that the getAnnotation method returns a reference to an Annotation. You need to convert this interface type (Annotation) to your own interface type (ClassInfo) in order to call the methods you've declared, like created().
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic