About Rodrigo Sasaki
If you have ever asked yourself questions like these:
– “How do I invoke a method, having only it’s name in a String?”
- “How do I list all the attributes in a class dynamically?”
- “How do I write a method that resets the state of any given object to default values?”
Then you have probably already heared of Java’s Reflection API, and in case you haven’t, this is a good opportunity to see what it’s all about, and what it can be used for. This feature is really powerful, but as always, has to be used with some good judgement.
The advantage it brings to the table is the ability to analyse information regarding a class, as it’s attributes, methods, annotations, or even the state of an instance, all of that in runtime. The dynamism obtained by this sounds really useful, doesn’t it?
In this tutorial I intent to show some basic usage of the Reflection API. What can be done, what shouldn’t be done, advantages and drawbacks.
Source : http://www.javacodegeeks.com/2013/07/javas-reflection-api.html