Skip to main content

Posts

Showing posts with the label Java Reflection

Java : Get the Values In An Annotation Using Reflection

Recently I was researching on implementing lucene search (using Hibernate Search). Although I found success with it, I wanted to automate the process of passing the fields that are indexed and their respective index names. Hibernate Search uses a bunch of annotations in the getter to specify their index name and filters used. My Requirement I want to go through all the 'Field' annotation present in every method defined inside my class and get the value of the 'name' field from it. Sometimes 'Field' is present inside as a nested annotation inside 'Fields'. Here the tricky part is where you've to set YourAnnotation ann =  method.getAnnotation(YourAnnotation.class) and get an the annotation object from it. Here is an example problem: Here is the solution: And here is my output...