Friday, March 18, 2016

Quick fix for common checkstyle issues

If you are running checkstyles maven plugin against your code, you will most probably be prompted with these two common issues. It can be really frustrating having to go through each file and fix some issues such as 1.

1.  error: File contains tab characters
2.  error: Wrong order for 'java.util.ArrayList' (or any other import that you have used) import.

Quick fix for first issue: In intellij idea plugin, select the respective code and go to edit -> convert indents -> to spaces.


As for the second issue there is no exact quick fix that I could find. Here basically you need to arrange your imports in lexicographical order. For example imports with 'java' needs to be listed prior to 'javax'.  You need to arrange imports of similar group together, and then sort imports within each group as well. For example if you take the below set of imports, first you need to group them into a single group, and then arrange them in alphabetical order from the set in red to the set in blue.


import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.zip.ZipInputStream;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipInputStream; 


Hope this saved your time!