In my last post about common Java violations, I listed a set of mistakes that java developers tend to make. While refactoring a Java project with the objective to resolve those violations, I used the refactoring features of Eclipse extensively to quickly change the code. Below is the compilation of such refactoring techniques.

1. Adding curly braces around block level statements

It is often a good practice to wrap the block level statements with {curly braces}. But still, if there is only one statement in the block, then some developers prefer not to wrap them with {}. But Checkstyle will complain if you do so.

If you want to change this,


to this:


Eclipse's source cleanupÂis there for your help.

  1.  In Project Explorer,Âright click on the source folder and select Source -> Clean up'¦using source clean up to add curly braces around block statements
  2. Choose Use custom profileÂand then click configureÂnext to the custom profile section.
  3. By default, clean up action is configured to do multiple cleanup task. Since we are focused only on adding curly braces, we will disable all other cleanup tasks. To do this, navigate to all the tabs in the Custom Clean upsÂwindow and deselect all the cleanups.
  4. Then in the Code StyleÂtab, select the Use blocks in if/while/for/do statementsÂoption and click OK.
  5. Then in Clean UpÂdialog, click NextÂand the refactoring will occur. You'll be presented with a review page with the changes made.

2. Joining to if statements into one

Lets say, you have a code like this:


It is safe to combine the two if statements into one, unless you have some other code in between the two ifÂstatements. Of course, you can manually edit the code to remove the second if and move the condition to up. But wait, when Eclipse can do this for us, why should we do it ourselves?Join inner if statement with outer if statements

  1. Place your cursor on the ifÂkeyword of the inner ifÂstatement.
  2. Press Ctrl 1Âwhich will open a context menu.
  3. Select the option Join 'if' statement with outer 'if' statement.
  4. Voila! the two if statements are now combined into one.

You'll get:

if(isLoggedIn && isAdmin){

3. Renaming a filed and its getter / setter methods

According to a this, renaming an element is the mostly used refactoring in Eclipse. So, when you rename a field which has setter/getter methods, you'd manually rename those method names. But Eclipse can help here to simplify this.[![][6]][6]

  1. Place your cursor on the field name that you want to rename.
  2. Press Ctrl ShiftÂand then press R keyÂtwo times continuously which will open the 'Rename Field'Âdialog box.
  3. Check the options 'Rename getter' and 'Rename setter' while providing a new name to your field.
  4. On clicking OK, this will rename the field as well as its getter/setter methods.

4. Inverting ifÂstatement

Suppose you have a code like this:

  // ask to login
  // allow access

Above code is 100% valid. But code quality tools such as Checkstyle might complain, because we are using a negativity check in the first condition (i.e. !isLoggedIn). When you have only one case (just the if block), then we can't do much about it. But when you have both ifÂand else, then you can just invert the conditions to avoid this scenario.

  1. Place your cursor on the first ifÂkeyword.
  2. Press Ctrl 1Âand then select Invert 'if' statement.
  3. Eclipse will invert the conditions and the corresponding blocks.
  4. Finally you'll get: if(isLoggedIn){ // allow access }else{ // ask to login }

Helps to improve the readability of the code.


Of course, the above are just the tip of the iceberg. Eclipse is capable of doing much-more advanced refactoring. So, what are your secret refactoring techniques?