What is reflection, and why is it useful?
I\'m particularly interested in Java, but I assume the principles are the same in any language.
I want to answer this question by example. First of all Hibernate
project uses Reflection API
to generate CRUD
statements to bridge the chasm between the running application and the persistence store. When things change in the domain, the Hibernate
has to know about them to persist them to the data store and vice versa.
Alternatively works Lombok Project
. It just injects code at compile time, result in code being inserted into your domain classes. (I think it is OK for getters and setters)
Hibernate
chose reflection
because it has minimal impact on the build process for an application.
And from Java 7 we have MethodHandles
, which works as Reflection API
. In projects, to work with loggers we just copy-paste the next code:
Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
Because it is hard to make typo-error in this case.