This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Reading Annotations without using instanceof  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a small question, is there some way that once I've gotten all the annotations on a given class or method that I can read it's values without first checking if the annotation is "instanceof" some annotation type and then casting it?

The following snippet of code is how I've determined how to read the annotation using "instanceof" but I'm curious to know if there's another way of doing it in the event the annotation is of a type you might not be expecting.


 
Patrick Grimard
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out.


 
Marshal
Posts: 60168
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use == or != for reference types to check whether they are identical. Use the equals() method.
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hava a look at the API... You can pass in what Annotation you are looking for and get back that annotation or null.

Sample:
MyAnnotation a = obj.getClass().getAnnotation(MyAnnotation.class);
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!