15207

Access annotation at runtime

Question:

How can I access in main whether check in the Sample class is true or false?<br /> What should I write in Main class?

package annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface annotation { public String name() default "Jimmy"; public boolean check() default false; } <hr /> package annotation; @annotation(name = "Jack", check = false) public class Sample { public String str = "Hi"; public void printHi(String str) { System.out.println(str); } } <hr /> package annotation; public class Main { public static void main(String[] args) { } }

Answer1:

Use <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getAnnotation%28java.lang.Class%29" rel="nofollow">Sample.class.getAnnotation(annotation.class)</a> to get your annotation instance, and call check() to get the check value:

System.out.println(Sample.class.getAnnotation(annotation.class).check());

Note that classes should start with an upper-case letter, and that naming an annotation "annotation" is quite confusing.

Recommend

  • Dynamic variables with given type
  • Deserialize to String or Object using Jackson
  • Jersey - Register ExceptionMapper for Resource Methods
  • How to get Eclipse to show Javadoc for javax annotations
  • Hibernate annotations. @Where vs @WhereJoinTable
  • How to create a constraint validator for multiple fields through annotation in spring 4.*
  • Question about pointer alignment
  • WCF service runs in Debug mode but not in Release
  • What is the Linux Equivalent of Kernel32.dll?
  • CS1703: In Xamarin.Droid, should I use the .Net Standard windowsruntime.dll located in Mono.Framewor
  • Comparing user's facebook/twitter friends to site's users in Python/Django
  • How do I add a File Type Association in a Windows Phone 8.1 app manifest?
  • runtime error when linking ffmpeg libraries in qt creator
  • Why can't UI components be accessed from a backgroundworker?
  • Ruby on Rails App deployed to heroku showing “We're sorry, but something went wrong”
  • Detecting null parameter in preprocessor macro
  • Azure webjobs output logs indexing taking very long
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Firefox Extension - Monitor refresh and change of tab
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • Display issues when we change from one jquery mobile page to another in firefox
  • C# - Serializing and deserializing static member
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Cross-Platform Protobuf Serialization
  • Modifying destination and filename of gulp-svg-sprite
  • Why winpcap requires both .lib and .dll to run?
  • Python - Map / Reduce - How do I read JSON specific field in using DISCO count words example
  • Return words with double consecutive letters
  • GridView Sorting works once only
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • AT Commands to Send SMS not working in Windows 8.1
  • How to format a variable of double type
  • Python: how to group similar lists together in a list of lists?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • coudnt use logback because of log4j
  • apache spark aggregate function using min value
  • JaxB to read class hierarchy
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++