Extending checkstyle 4.4
Arnaud Roques <[email protected]> Mon, 29 Sep 2008 18:00:16 +0200 (CEST)
| Newsgroups | gmane.comp.java.audit.checkstyle.user |
|---|---|
| Message-ID | <[email protected]> |
v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} Normal 0 21 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Tableau Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman";} Hello,
Checkstyle is really a great tool to improve code quality, and I've developped some checkers that other people may find usefull. (They have been used with JDK1.4).
If you have some idea to add in this list, please tell me.
I also apologize for the long post, but is there a better place to share checkers?
---------------------------------
AvoidNegativeLogic
This checker avoid the usage of inverted logic. It looks for code like
if (a != null) {
System.out.println("not null");
} else {
System.out.println("null");
}
and promotes the usage of
if (a == null) {
System.out.println("null");
} else {
System.out.println("not null");
}
---------------------------------
CollapseIfStatement
This checker looks for if statement with can be simplified.
For example:
if (a != null) {
if (b == null) {
System.err.println();
}
}
Can be simplified in:
if (a != null && b == null) {
System.err.println();
}
---------------------------------
DeclareVariableInItsBloc
Variable should be declared in the bloc that uses it.
For example:
Object dummy;
for (Iterator it = list.iterator(); it.hasNext();) {
dummy = it.next();
...
}
(dummy not used anymore)
Can be changed in:
for (Iterator it = list.iterator(); it.hasNext();) {
Object dummy = it.next();
...
}
---------------------------------
HiddenCatchBloc
Very often, people catch exception and do not use the exception itself.
Example:
try {
...
} catch (IOException e) {
return null;
}
May be we should at least log the exception:
try {
...
} catch (IOException e) {
log.error("IOError ", e);
return null;
}
---------------------------------
ImmutableField
This checker detects private fields that are unused in a class and promotes the usage of final for fields that are never changed.
---------------------------------
ImportControl
This check control that import are valid into a java file, depending on its package.
Example, to control that classes into com.foo2 (and subpackages) does not import com.foo1:
<module name="cs.ImportControl">
<property name="importThis" value="com\.foo1" />
<property name="notByPackages" value="com\.foo2" />
</module>
Another example, to control that only classes into com.dummy2 (and subpackages) does import com.dummy1:
<module name="cs.ImportControl">
<property name="importThis" value="com\.dummy1" />
<property name="onlyByPackages" value="com\.dummy2" />
</module>
---------------------------------
InstanceofIsNeverNull
When an object is an instanceof something, it cannot be null.
So the following code :
if (obj != null && obj instanceof List) {...}
should be changed into:
if (obj instanceof List) {...}
---------------------------------
NewIsNeverNull
When an object is created with new, it is never null.
So the following code :
MyClass obj = new MyClass();
if (obj != null) {
// Some code
}
should be changed into:
MyClass obj = new MyClass();
// Some code
---------------------------------
PatternFilter
This filter looks like SuppressionFilter, but the configuration is done in the filter itself, and not into another XML file.
<module name="Checker">
<module name="cs.PatternFilter">
<property name="ignoreFiles"
value="(.*(<=Managed)(<=View)Bean\.java)|(.*Home\.java)" />
<property name="modules" value="TabCharacter|WhitespaceAround" />
</module>
<module name="TreeWalker">
...
</module>
</module>
---------------------------------
TooEarlyVariableDeclaration
This checker try to detect too early variable declaration:
int i = 0, j = 4; // Error: Declare variables on separate lines
int i;
i = 4; // those two lines can be merged into "int i = 4"
int i = 0; // this affectation is useless
i = 4;
int i; // Declare variable as close as possible to where they are used
[...A lot of code that does NOT use i...]
i = 6;
---------------------------------
UnitTestMethods
This checker is really great for Test Driven Developpement. It checks that every non private method of a class is JUnit tested.
Example of module configuration:
<module name="cs.UnitTestMethods">
<property name="severity" value="warning" />
<property name="filesToCheck"
value="^(.*[/\\])src([/\\]dummy[/\\]\w+)\.java$" />
<property name="testCases"
value="$1test$2Test.java" />
<property name="minSemicolon"
value="2" />
</module>
This example looks for every Java file in src/dummy or src\dummy. Non-private methods that have more than 2 semicolons must have a test method. The TestCase must be in test/dummy directory.
---------------------------------
UnnecessarilyElse
This checker detects unnecessarily else statement. Example:
if (a==null) {
// some code
return null;
} else {
// other things
}
which can be changed into:
if (a==null) {
// some code
return null;
}
// other things
---------------------------------
UnnecessaryParentheses
This checker detects some unnecessary parentheses which are not detected by the standard check of CheckStyle. Up to now, it only deals with ==,!=,&& and ||
It detects the following code:
((a==null) && (b!=null))
which is better read this way (except for people coming from LISP :-)
(a==null && b!=null)
---------------------------------
UnusedLocalVariable
This checker detects unused local variable. The detection is not 100% accurate.
If the checker declares a variable as unused, it means that the variable is really unused.
But in some (rare) cases, an unused variable can be undetected by the checker. For example, if you call the variable foo and that you also have some method called foo() used.
---------------------------------
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Checkstyle-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user
cs.jar
(application/java-archive, 56.4 KB) - not displayed