• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java annotation @Retention(value=RUNTIME) confusion  RSS feed

 
shashank dwivedi
Ranch Hand
Posts: 65
AngularJS Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does it mean by @Retention(value=RUNTIME) in annotations?
Does it mean this annotation can be ignored at compile time even if the annotation class doesn't exist?
What if at runtime JVM fails to inject annotation using reflection (considering annotation doesn't exist)?

Found this link:
http://stackoverflow.com/questions/3567413/why-doesnt-a-missing-annotation-cause-a-classnotfoundexception-at-runtime

Please help me understand this concept.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Retention policies specify until when an annotation should be retained.

SOURCE means that the annotation is thrown away after the compiler has compiled it to byte-code. That means that the .class knows nothing about the annotation.
COMPILE means that the annotation is included in the byte-code. You can inspect classes and find the annotation. The annotation is thrown away when you run the JVM.
RUNTIME means that the annotation is available inside the JVM. You can use reflection to find it.

If an annotation doesn't exist, you will never be able to compile the code. This has nothing to do with retention policies.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!