A Sample Configuration
We have covered some of the vast range of checks that Checkstyle can do for us.
Here is now a sample configuration which we will use to check a Java source
code.
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.1//EN"
"http://www.puppycrawl.com/dtds/configuration_1_1.dtd">
<module name="Checker">
<module name="TreeWalker">
<!-- check for constnat naming starts with c-->
<module name="ConstantName">
<property name="format" value="^c[A-Z][a-zA-Z0-9]*$" />
</module>
<!--chech for imports-->
<module name="AvoidStarImport"/>
<module name="IllegalImport"/>
<module name="RedundantImport"/>
<module name="UnusedImports"/>
<!--check for tabs-->
<module name="TabCharacter"/>
<!--check for curly braces-->
<module name="LeftCurly">
<property name="option" value="nl"/>
<property name="tokens" value="LITERAL_IF, LITERAL_ELSE"/>
</module>
<module name="RightCurly">
<property name="option" value="alone"/>
<property name="tokens" value="LITERAL_IF, LITERAL_ELSE"/>
</module>
<!--check for equals and hashcode-->
<module name="EqualsHashCode"/>
<!--chech for final class-->
<module name="FinalClass"/>
<!--check for protected members not allowed-->
<module name="VisibilityModifier">
<property name="protectedAllowed" value="false"/>
</module>
</module>
</module>
Listing 1: mycheck.xml, the checkstyle config file
Now let us create a sample Java class to pass through the style check.
/*
* GUIManager.java
*
* Created on 27 July 2002, 21:06
*/
import java.util.*;
/**
*
* @author Administrator
* @version
*/
public class GUIManager extends Object {
protected static final int PERSON = 1;
private GUIManager(){ }
public void load(String file)
{
if(file !=null) {
System.out.println("loading file..");
}else{
System.out.println("file is null");
}
}
public boolean equals(GUIManager manager)
{
return true;
}
}
Listing 2: A sample java file
Running Checkstyle
In order to run the checkstyle, you can do the following:
java -jar checkstyle-all-3.1.jar -c mychecks.xml -f plain -o guirep
ort.txt GUIManager.java
Look at the flags in this:
- -c specifies the config file to use.
- -f defines the file format (plain/xml).
- -o defines the output file.
In this case, we are running the checkstyle with the –jar option
directly from the .jar file. But you can also define the main
checkstyle class to invoke.
java com.puppycrawl.tools.checkstyle.Main -c mychecks.xml -f plain -o guirep
ort.txt GUIManager.java
If you now open the output file, you will see the following error reports.
Starting audit...
GUIManager.java:8: Using the '.*' form of import should be avoided -
java.util.*.
GUIManager.java:14: Class GUIManager should be declared as final.
GUIManager.java:16:32: Name 'PERSON' must match pattern '^c[A-Z][a-zA-Z0-9]*$'.
GUIManager.java:22:23: '{' should be on a new line.
GUIManager.java:24:7: '}' should be alone on a line.
GUIManager.java:24:12: '{' should be on a new line.
GUIManager.java:29:5: Definition of 'equals()' without corresponding definition
of 'hashCode()'.
Audit done.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|