maverickxx
asked on
Java Code Style
Background:
We have a legacy Java code( Code written over 10 years ago). Legacy Code followed distinct coding styles. Example of one being that Instance variables were declared at the bottom of the class.
A new team took over the legacy code and started changes( adding spring framework and other changes). After few months, we realized that team was declaring instance variables at the top of the class. Decision was made to keep the instance variables at the bottom(old style).
My question :
1. Is it possible using some tool/plug or some code formatting file in to find out number of classes affected by the change in coding style?
2. If #1 is possible, and I manage to identify the classes, what would be the easiest way to change the declaration of all instance variables to be at the bottom of the class rather than at the top.
Can I configure a code formatting file or checkstyle or PMD or some other tool to achieve above objectives?
We have a legacy Java code( Code written over 10 years ago). Legacy Code followed distinct coding styles. Example of one being that Instance variables were declared at the bottom of the class.
A new team took over the legacy code and started changes( adding spring framework and other changes). After few months, we realized that team was declaring instance variables at the top of the class. Decision was made to keep the instance variables at the bottom(old style).
My question :
1. Is it possible using some tool/plug or some code formatting file in to find out number of classes affected by the change in coding style?
2. If #1 is possible, and I manage to identify the classes, what would be the easiest way to change the declaration of all instance variables to be at the bottom of the class rather than at the top.
Can I configure a code formatting file or checkstyle or PMD or some other tool to achieve above objectives?
Why not write a Java programme to do it?
ASKER
Krakatoa,
I was looking for a quick and easy way for identifying number of changes. Also I am not sure of there are any more formatting changes.
I want to leverage any formatting libraries or plug ins if any
I was looking for a quick and easy way for identifying number of changes. Also I am not sure of there are any more formatting changes.
I want to leverage any formatting libraries or plug ins if any
CheckStyle is the only example I have used so my answer is based on that. It has a "com.puppycrawl.tools.chec kstyle.che cks.coding .Declarati onOrderChe ck" that implements the checking of the order of declarations in a class, however, it is non-configurable in the order that it expects to see things, and the order that it is checking for is for instance variables to be at the top.
You could take the source code for "DeclarationOrderCheck" and you should just be able to change the numbers that are assigned to the STATE_* constants at the top, in order for it to check for a different order (also, you will have to change the initial state assignment to "mScopeState" to be what ever the first one should be). This page, http://checkstyle.sourceforge.net/writingchecks.html, should help you in how to setup your checkstyle configuration to use a custom written check.
Then, once you identify classes that don't conform to your standards, if you use Eclipse as your IDE, you can select from the menu, "Source" -> "Sort Members...", and then click the link that appears in the window to configure the "Members Sort Order" in the Preferences and then apply that sorting to your Java files. Eclipse is the only IDE that I use, so I can't speak for others if you use something else.
You could take the source code for "DeclarationOrderCheck" and you should just be able to change the numbers that are assigned to the STATE_* constants at the top, in order for it to check for a different order (also, you will have to change the initial state assignment to "mScopeState" to be what ever the first one should be). This page, http://checkstyle.sourceforge.net/writingchecks.html, should help you in how to setup your checkstyle configuration to use a custom written check.
Then, once you identify classes that don't conform to your standards, if you use Eclipse as your IDE, you can select from the menu, "Source" -> "Sort Members...", and then click the link that appears in the window to configure the "Members Sort Order" in the Preferences and then apply that sorting to your Java files. Eclipse is the only IDE that I use, so I can't speak for others if you use something else.
ASKER
Thank you mccarl. your link was very useful.
link you provided will work if I want to change the order of declarations. But I want to move the the variables to bottom of the class before closing braces.
link you provided will work if I want to change the order of declarations. But I want to move the the variables to bottom of the class before closing braces.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks mccarl
Not a problem, glad to help!