Alex Rivera | Logout

Java Reflection and the pain in Refactoring

Asked 2009-06-22T15:41:06.140
10

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!

Edit
Report

1 Answer

0

You can avoid writing reflection API with dp4j.com when you know at compile time what you are looking for.

About xml mappings, they are a pain, I also experienced with NetBeans Platform and less so with JPA2. The good news is that annotations are taking over, and this offers again compile-time checking. I'm not sure about Hibernate, but both JPA and NetBeans (more as of 7) offer annotation equivalents of xml mappings.

I had also developed SqlWrapper to get away from using strings with JDBC. More sophisticated (and complicated) is JPA2's criteria API.

answered 2011-03-30T20:05:38.730

Your Answer