wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How to retrieve data from Annotation type? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to retrieve data from Annotation type?" Watch "How to retrieve data from Annotation type?" New topic
Author

How to retrieve data from Annotation type?

tom chansky
Greenhorn

Joined: Nov 01, 2004
Posts: 28
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

Joined: Aug 31, 2004
Posts: 11343

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().


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to retrieve data from Annotation type?
 
Similar Threads
@Documented annotation
how to insert a current date into the databse colunm using hybernate ?
Timestamping in Hibernate
Why doesn't this code work in Netbeans 7.0?
Column 'NEXT_HI' is either not in any table in the FROM list or appears