our J开发者_如何学运维ava application has this problem with shadowed fields (a subclass declare the same fields' names as it superclass) in many classes. The application won't work correctly with our new enhancement if any class has shadowed fields.
Is there any tool to quickly scan for those?
Thanks
I looked through PMD and options under eclipse and they don't really help though. I don't think PMD has the capability to traverse across classes? Or am I wrong?
You can write a custom rule for pmd. I don't think any of the default rules cover what you want to do.
Eclipse has an option that's normally off to check for this problem. Go to Window > Preferences, navigate to Java > Compiler > Errors/Warnings, then expand "Name shadowing and conflicts". There are several options there that will help.
I'm not sure if Netbeans has a similar setting, but I imagine it does.
Well you could simply write a small app that reads all the java classes and searches for inheritance trees and then sees if there are sub classes that have these shadow fields.
You could even have the app rename the shadowed fields if you wanted to get a little more fancy (not much more fancy). If you opted to attempt renaming them programmatically then you will have to find all references in the application and update them as well.
I do not know of any tools that do this off the top of my head but a good Google search could probably do that for you =P
Also check your IDE to see if it has the capability you are after.
精彩评论