• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to retrieve data from Annotation type?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
reply
    Bookmark Topic Watch Topic
  • New Topic