Java Reflection and the pain in Refactoring
java, language-agnostic, refactoring, reflection
Solution
Java reflection causes many of the same problems you get with dynamically typed languages such as Python and Ruby. In fact, one way to think about dynamically typed languages is that everything is called using reflection, and the languages just provide a nice, clean syntax for reflection.
And yes, with dynamically typed languages (or heavy uses of reflection), refactoring is hard. You don't get the nice Eclipse refactoring capabilities. Instead, grep becomes your friend.
From my experience, the best thing you can do is build yourself a good safety net of unit tests. That way, if you break some dynamic code during refactoring, at least you'll catch it quickly when you run the tests.
If you're doing lots of statically typed code, you're in big trouble if you don't have a good base of unit tests. If you're doing lots of dynamically typed code (including code with lots of reflection), you don't have any hope of being successful without a good base of unit tests.
Problem
Java Reflection provides a mechanism to introspect an Object at runtime. No second thoughts, this is a great feature, but it breaks all the Refactoring conventions! There is no easy way (other than `File Search`) even in modern IDE's to know which attribute is referenced and where. This makes Refactorings much more complex (tiresome!) and error prone. To be frank, it's not just the `Reflection API`; `Hibernate mapping files (hbm.xml)` and `JSP files` both refer to the attributes as String and when you refactor your attribute name, then you have to manually change in all these places. Worse, the changes in Hibernate mapping files or JSP files result in runtime errors. I am interested in knowing how other programmers handle this in Java. Are there some tools? I use Eclipse/IBM RAD as main development platform. Normally we use a `constant` to define the attribute and use it whenever possible but its not always possible. I would also be interested how other languages handle this!