Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java Code Style

Posted on 2013-06-12
7
Medium Priority
?
331 Views
Last Modified: 2013-06-18
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?
0
Comment
Question by:maverickxx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 39241418
Why not write a Java programme to do it?
0
 

Author Comment

by:maverickxx
ID: 39242862
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
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39243281
CheckStyle is the only example I have used so my answer is based on that. It has a "com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheck" 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.
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

Author Comment

by:maverickxx
ID: 39243422
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.
0
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 39243437
If you have a look at the source code for the existing "DeclarationOrderCheck" (I'm looking at the online version, HERE) you will see near the top the constants that define the expected order of declarations...
    private static final int STATE_STATIC_VARIABLE_DEF = 1;
    private static final int STATE_INSTANCE_VARIABLE_DEF = 2;
    private static final int STATE_CTOR_DEF = 3;
    private static final int STATE_METHOD_DEF = 4;

Open in new window

You would make a new class that is a copy of this one but changing those constants, probably something like the following is what you are after...
    private static final int STATE_STATIC_VARIABLE_DEF = 1;
    private static final int STATE_CTOR_DEF = 2;
    private static final int STATE_METHOD_DEF = 3;
    private static final int STATE_INSTANCE_VARIABLE_DEF = 4;

Open in new window

This will make checkstyle report on any classes where the instance variable declarations and NOT at the bottom of the class.

As I said previously though, this will only identify cases that don't comply. You will need something like Eclipse's "Sort Members" action to actually move those variable declarations to where you want them.
0
 

Author Closing Comment

by:maverickxx
ID: 39257207
Thanks mccarl
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39257909
Not a problem, glad to help!
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question