Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to retrieve data from Annotation type?

 
tom chansky
Greenhorn
Posts: 28
  • 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
  • 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().
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic