• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

annotations not returned by getAnnotations()

 
Ranch Hand
Posts: 218
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm reading Jeanne & Scott's complete OCP 11 study guide, chapter 13 about Annotations.

Following the syntax they give, I define an Annotation:

Exercise.java


I then create a class, with that Exercise annotation applied, than can print out it's own annotations:

PrintMyAnnotations.java


but nothing gets printed


If, instead of @Exercise, I use a @Deprecated annotation


I get, as expected, an annotation reported:


Could anyone tell me why, in the case of @Exercise, I'm not getting any annotation reported?

 
Marshal
Posts: 79971
396
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at the differences between your annotation and @Deprecated. Deprecated has both targets and retention (=RUNTIME).  If you look in the JLS (=Java® Language Specification), you will find that annotations are not always present. I suggest you add a retention to your annotation and see what happens. Not certain, but I think the absence of a target isn't a problem at the moment.

Addition: getAnnotations() only returns annotations that are present.
 
Richard Hayward
Ranch Hand
Posts: 218
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I suggest you add a retention to your annotation and see what happens.



Changing the annotation so as to include that:

MyExercise.java


PrintMyAnnotations.java


It works!



Seems that without RetentionPolicy.RUNTIME, the annotation doesn't exist at runtime.

Thanks Campbell
 
Campbell Ritchie
Marshal
Posts: 79971
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Hayward wrote:. . . Seems that without RetentionPolicy.RUNTIME, the annotation doesn't exist at runtime.

Yes, that seems to be what the JLS link I showed you says.

Thanks Campbell

That's a pleasure
 
Campbell Ritchie
Marshal
Posts: 79971
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way: Two (very) minor points: Class is a parametrised type, so it might be preferable to declare it as such. It won't make any difference in sich a simple use case. Many people use clazz as the identifier; you can't use class as an identifier because it is a keyword.
 
It's a tiny ad only because the water is so cold.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic