Join the DZone community and get the full member experience.Join For Free
The Java Reflection API is a very nice feature of the Java language. With the API you can do the magic with Java classes, create an object or call a method on the fly based only on a name. The real Magic!
So then, we all should use it all the time, to write a better code, yes? NO!
Avoid using it, try to write a code without the Reflection. The worst you can do is if you introduce some generic mechanism with it - a custom validation framework or a flow engine where class and method names are stored as a String (in a text file or a XML file).
You can ask: why?
The answer is simple: your IDE doesn't support it! When you refactor your class or method, eg. just rename it, IDE will find all the references and change them appropriately for you. But IDE won't know about your custom solution, is just a text, nothing more!
When you are using the Reflection, your IDE is blind!
Right now I'm working on our custom validation framework, to move definitions of validation from a XML file directly to a Java code. The case is, to not to change the validators, just used them as is. And not to use the Reflection as well ;-)
I've done some draft implementation base on anonymous classes and the word "final" (thanks Rafal for the tip!). And introduce the ValidationAware interface to be able at the same time use the old way and my new solution! Next week on Monday I will ask other team mates for opinion!
Published at DZone with permission of Lukasz Lenart . See the original article here.
Opinions expressed by DZone contributors are their own.