527 lines
18 KiB
HTML
527 lines
18 KiB
HTML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE html
|
|
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
|
"DTD/xhtml1-strict.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
<head>
|
|
<title>Checks for Coding problems</title>
|
|
<link rel="stylesheet" type="text/css" href="mystyle.css"/>
|
|
</head>
|
|
|
|
<body>
|
|
<!-- The header -->
|
|
<table border="0" width="100%" summary="header layout">
|
|
<tr>
|
|
<td><h1>Checks for Coding problems</h1></td>
|
|
<td align="right"><img src="logo.png" alt="Checkstyle Logo"/></td>
|
|
</tr>
|
|
</table>
|
|
<!-- content -->
|
|
<table border="0" width="100%" cellpadding="5" summary="body layout">
|
|
<tr>
|
|
<!--Left menu-->
|
|
<td class="menu" valign="top">
|
|
<ul>
|
|
<li>
|
|
<a href="#AvoidInlineConditionals">AvoidInlineConditionals</a>
|
|
</li>
|
|
<li>
|
|
<a href="#DoubleCheckedLocking">DoubleCheckedLocking</a>
|
|
</li>
|
|
<li>
|
|
<a href="#EmptyStatement">EmptyStatement</a>
|
|
</li>
|
|
<li>
|
|
<a href="#EqualsHashCode">EqualsHashCode</a>
|
|
</li>
|
|
<li>
|
|
<a href="#HiddenField">HiddenField</a>
|
|
</li>
|
|
<li>
|
|
<a href="#IllegalInstantiation">IllegalInstantiation</a>
|
|
</li>
|
|
<li>
|
|
<a href="#InnerAssignment">InnerAssignment</a>
|
|
</li>
|
|
<li>
|
|
<a href="#MagicNumber">MagicNumber</a>
|
|
</li>
|
|
<li>
|
|
<a href="#MissingSwitchDefault">MissingSwitchDefault</a>
|
|
</li>
|
|
<li>
|
|
<a href="#SimplifyBooleanExpression">SimplifyBooleanExpression</a>
|
|
</li>
|
|
<li>
|
|
<a href="#SimplifyBooleanReturn">SimplifyBooleanReturn</a>
|
|
</li>
|
|
</ul>
|
|
</td>
|
|
<!--Content-->
|
|
<td class="content" valign="top" align="left">
|
|
|
|
<!-- --> <a name="AvoidInlineConditionals"></a> <h2>AvoidInlineConditionals</h2> <h4>Description</h4>
|
|
<p class="body">
|
|
Detects inline conditionals.
|
|
|
|
An example inline conditional is this:
|
|
</p>
|
|
<pre class="body" >
|
|
String a = getParameter("a");
|
|
String b = (a==null || a.length<1) ? null : a.substring(1);
|
|
</pre>
|
|
<p class="body">
|
|
Rationale: Some developers find inline conditionals hard to read,
|
|
so their company's coding standards forbids them.
|
|
</p>
|
|
<h4>Examples</h4>
|
|
<p class="body">
|
|
To configure the check:
|
|
</p>
|
|
<pre class="body">
|
|
<module name="AvoidInlineConditionals"/>
|
|
</pre>
|
|
<h4>Package</h4>
|
|
<p class="body">
|
|
com.puppycrawl.tools.checkstyle.checks
|
|
</p>
|
|
<h4>Parent Module</h4>
|
|
<p class="body">
|
|
<a href="config.html#treewalker">TreeWalker</a>
|
|
</p>
|
|
|
|
<!-- --> <a name="DoubleCheckedLocking"></a> <h2>DoubleCheckedLocking</h2> <h4>Description</h4>
|
|
<p class="body">
|
|
The "double-checked locking" idiom (DCL) tries to avoid the runtime cost
|
|
of synchronization. An example that uses the DCL idiom is this:
|
|
</p>
|
|
<pre class="body">
|
|
public class MySingleton
|
|
{
|
|
private static theInstance = null;
|
|
|
|
private MySingleton() {}
|
|
|
|
public MySingleton getInstance() {
|
|
if ( theInstance == null ) { // synchronize only if necessary
|
|
synchronized( MySingleton.class ) {
|
|
if ( theInstance == null ) {
|
|
theInstance = new MySingleton();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
</pre>
|
|
<p class="body">
|
|
The problem with the DCL idiom in Java is that it just does not work correctly.
|
|
Using it introduces bugs that are extremely hard to track down and reproduce.
|
|
The <a href="http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html">
|
|
"Double-Checked Locking is Broken" Declaration</a> has an in depth explanation
|
|
of the exact problem which has to do with the semantics of the Java memory model.
|
|
</p>
|
|
<p class="body">
|
|
The DoubleCheckedLocking check will find source code where a test is wrapped in a
|
|
synchronized block that is wrapped in the same test, like in the example above.
|
|
</p>
|
|
<h4>Examples</h4>
|
|
<p class="body">
|
|
To configure the check:
|
|
</p>
|
|
<pre class="body">
|
|
<module name="DoubleCheckedLocking"/>
|
|
</pre>
|
|
<h4>Package</h4>
|
|
<p class="body">
|
|
com.puppycrawl.tools.checkstyle.checks
|
|
</p>
|
|
<h4>Parent Module</h4>
|
|
<p class="body">
|
|
<a href="config.html#treewalker">TreeWalker</a>
|
|
</p>
|
|
|
|
<!-- --> <a name="EmptyStatement"></a> <h2>EmptyStatement</h2> <h4>Description</h4>
|
|
<p class="body">
|
|
Detects empty statements (standalone ;).
|
|
</p>
|
|
<h4>Examples</h4>
|
|
<p class="body">
|
|
To configure the check:
|
|
</p>
|
|
<pre class="body">
|
|
<module name="EmptyStatement"/>
|
|
</pre>
|
|
<h4>Package</h4>
|
|
<p class="body">
|
|
com.puppycrawl.tools.checkstyle.checks
|
|
</p>
|
|
<h4>Parent Module</h4>
|
|
<p class="body">
|
|
<a href="config.html#treewalker">TreeWalker</a>
|
|
</p>
|
|
|
|
<!-- --> <a name="EqualsHashCode"></a> <h2>EqualsHashCode</h2>
|
|
<h4>Description</h4>
|
|
<p class="body">
|
|
Checks that classes that override <span class="code">equals()</span> also
|
|
override <span class="code">hashCode()</span>.
|
|
</p>
|
|
<p class="body">
|
|
Rationale: The contract of <span class="code">equals()</span> and <span class="code">
|
|
hashCode()</span> requires that equal objects have the same hashCode. Hence,
|
|
whenever you override <span class="code">equals()</span> you must override <span class="code">
|
|
hashCode()</span> to ensure that your class can be used in collections that are
|
|
hash based.
|
|
</p>
|
|
|
|
<h4>Example</h4>
|
|
<p class="body">
|
|
To configure the check:
|
|
</p>
|
|
<pre class="body">
|
|
<module name="EqualsHashCode"/>
|
|
</pre>
|
|
<h4>Package</h4>
|
|
<p class="body">
|
|
com.puppycrawl.tools.checkstyle.checks
|
|
</p>
|
|
<h4>Parent Module</h4>
|
|
<p class="body">
|
|
<a href="config.html#treewalker">TreeWalker</a>
|
|
</p>
|
|
|
|
<!-- --> <a name="HiddenField"></a> <h2>HiddenField</h2> <h4>Description</h4>
|
|
<p class="body">
|
|
Checks that a local variable or a parameter does not shadow a field that is
|
|
defined in the same class.
|
|
</p>
|
|
<h4>Properties</h4>
|
|
<table width="100%" border="1" cellpadding="5" class="body">
|
|
<tr class="header">
|
|
<th>name</th>
|
|
<th>description</th>
|
|
<th>type</th>
|
|
<th>default value</th>
|
|
</tr>
|
|
<tr>
|
|
<td>tokens</td>
|
|
<td>tokens to check</td>
|
|
<td>subset of tokens <a
|
|
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PARAMETER_DEF">PARAMETER_DEF</a>,
|
|
<a
|
|
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">VARIABLE_DEF</a></td>
|
|
|
|
<td><a
|
|
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PARAMETER_DEF">PARAMETER_DEF</a>,
|
|
<a
|
|
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">VARIABLE_DEF</a></td>
|
|
</tr>
|
|
</table>
|
|
<h4>Examples</h4>
|
|
<p class="body">
|
|
To configure the check:
|
|
</p>
|
|
<pre class="body">
|
|
<module name="HiddenField"/>
|
|
</pre>
|
|
<p class="body">
|
|
To configure the check so that it checks local variables but not parameters:
|
|
</p>
|
|
<pre class="body">
|
|
<module name="HiddenField">
|
|
<property name="tokens" value="VARIABLE_DEF"/>
|
|
</module>
|
|
</pre>
|
|
<h4>Package</h4>
|
|
<p class="body">
|
|
com.puppycrawl.tools.checkstyle.checks
|
|
</p>
|
|
<h4>Parent Module</h4>
|
|
<p class="body">
|
|
<a href="config.html#treewalker">TreeWalker</a>
|
|
</p>
|
|
|
|
<!-- --> <a name="IllegalInstantiation"></a> <h2>IllegalInstantiation</h2> <h4>Description</h4>
|
|
<p class="body">
|
|
Checks for illegal instantiations where a factory method is preferred.
|
|
</p>
|
|
<p class="body">
|
|
Rationale: Depending on the project, for some classes it might be preferable to
|
|
create instances through factory methods rather than calling the constructor.
|
|
</p>
|
|
<p class="body">
|
|
A simple example is the <span class="code">java.lang.Boolean</span> class. In
|
|
order to save memory and CPU cycles, it is preferable to use the predefined
|
|
constants <span class="code">
|
|
TRUE</span> and <span class="code">FALSE</span>. Constructor invocations should
|
|
be replaced by calls to <span class="code">Boolean.valueOf()</span>.
|
|
</p>
|
|
<p class="body">
|
|
Some extremely performance sensitive projects may require the use of factory
|
|
methods for other classes as well, to enforce the usage of number caches or
|
|
object pools.
|
|
</p>
|
|
<h4>Notes</h4>
|
|
<p class="body">
|
|
There is a limitation that it is currently not possible to specify array classes.
|
|
</p>
|
|
<table width="100%" border="1" cellpadding="5" class="body">
|
|
<tr class="header">
|
|
<th>name</th>
|
|
<th>description</th>
|
|
<th>type</th>
|
|
<th>default value</th>
|
|
</tr>
|
|
<tr>
|
|
<td>classes</td>
|
|
<td>classes that should not be instantiated</td>
|
|
<td><a href="property_types.html#stringSet">String Set</a></td>
|
|
<td>{}</td>
|
|
</tr>
|
|
</table>
|
|
<h4>Example</h4>
|
|
<p class="body">
|
|
To configure the check to find instantiations of java.lang.Boolean:
|
|
</p>
|
|
<pre class="body">
|
|
<module name="IllegalInstantiation">
|
|
<property name="classes" value="java.lang.Boolean"/>
|
|
</module>
|
|
</pre>
|
|
|
|
<h4>Package</h4>
|
|
<p class="body">
|
|
com.puppycrawl.tools.checkstyle.checks
|
|
</p>
|
|
<h4>Parent Module</h4>
|
|
<p class="body">
|
|
<a href="config.html#treewalker">TreeWalker</a>
|
|
</p>
|
|
<!-- --> <a name="InnerAssignment"></a> <h2>InnerAssignment</h2> <h4>Description</h4>
|
|
<p class="body">
|
|
Checks for assignments in subexpressions, such as in <span class="code">String s
|
|
= Integer.toString(i = 2);</span>.
|
|
</p>
|
|
<p class="body">
|
|
Rationale: With the exception of <span class="code">for</span> iterators, all
|
|
assignments should occur in their own toplevel statement to increase readability.
|
|
With inner assignments like the above it is difficult to see all places where a
|
|
variable is set.
|
|
</p>
|
|
<h4>Properties</h4>
|
|
<table width="100%" border="1" cellpadding="5" class="body">
|
|
<tr class="header">
|
|
<th>name</th>
|
|
<th>description</th>
|
|
<th>type</th>
|
|
<th>default value</th>
|
|
</tr>
|
|
<tr>
|
|
<td>tokens</td>
|
|
<td>assignments to check</td>
|
|
<td>subset of tokens <a
|
|
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ASSIGN">ASSIGN</a>,
|
|
<a
|
|
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#BAND_ASSIGN">BAND_ASSIGN</a>,
|
|
<a
|
|
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#BOR_ASSIGN">BOR_ASSIGN</a>,
|
|
<a
|
|
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#BSR_ASSIGN">BSR_ASSIGN</a>,
|
|
<a
|
|
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#BXOR_ASSIGN">BXOR_ASSIGN</a>,
|
|
<a
|
|
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#DIV_ASSIGN">DIV_ASSIGN</a>,
|
|
<a
|
|
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#MINUS_ASSIGN">MINUS_ASSIGN</a>,
|
|
<a
|
|
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#MOD_ASSIGN">MOD_ASSIGN</a>,
|
|
<a
|
|
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PLUS_ASSIGN">PLUS_ASSIGN</a>,
|
|
<a
|
|
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#SL_ASSIGN">SL_ASSIGN</a>,
|
|
<a
|
|
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#SR_ASSIGN">SR_ASSIGN</a>,
|
|
<a
|
|
href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#STAR_ASSIGN">STAR_ASSIGN</a></td>
|
|
<td>all tokens</td>
|
|
</tr>
|
|
</table>
|
|
<h4>Examples</h4>
|
|
<p class="body">
|
|
To configure the check:
|
|
</p>
|
|
<pre class="body">
|
|
<module name="InnerAssignment"/>
|
|
</pre>
|
|
<p class="body">
|
|
To configure the check for only <span class="code">=</span>, <span class="code">
|
|
+=</span>, and <span class="code">-=</span> operators:
|
|
</p>
|
|
<pre class="body">
|
|
<module name="InnerAssignment">
|
|
<property name="tokens" value="ASSIGN,PLUS_ASSIGN,MINUS_ASSIGN"/>
|
|
</module>
|
|
</pre>
|
|
<h4>Package</h4>
|
|
<p class="body">
|
|
com.puppycrawl.tools.checkstyle.checks
|
|
</p>
|
|
<h4>Parent Module</h4>
|
|
<p class="body">
|
|
<a href="config.html#treewalker">TreeWalker</a>
|
|
</p>
|
|
|
|
<!-- --> <a name="MagicNumber"></a> <h2>MagicNumber</h2> <h4>Description</h4>
|
|
<p class="body">
|
|
Checks that there are no "magic numbers", where a
|
|
magic number is a numeric literal that is not defined as a
|
|
constant. By default, -1, 0, 1, and 2 are not considered to
|
|
be magic numbers.
|
|
</p>
|
|
<h4>Properties</h4>
|
|
<table width="100%" border="1" cellpadding="5" class="body">
|
|
<tr class="header">
|
|
<th>name</th>
|
|
<th>description</th>
|
|
<th>type</th>
|
|
<th>default value</th>
|
|
</tr>
|
|
<tr>
|
|
<td>tokens</td>
|
|
<td>tokens to check</td>
|
|
<td>subset of tokens NUM_DOUBLE, NUM_FLOAT, NUM_INT, NUM_LONG</td>
|
|
<td>all tokens</td>
|
|
</tr>
|
|
<tr>
|
|
<td>ignoreNumbers</td>
|
|
<td>non-magic numbers</td>
|
|
<td>list of numbers</td>
|
|
<td>-1, 0, 1, 2</td>
|
|
</tr>
|
|
</table>
|
|
<h4>Examples</h4>
|
|
<p class="body">
|
|
To configure the check:
|
|
</p>
|
|
<pre class="body">
|
|
<module name="MagicNumber"/>
|
|
</pre>
|
|
<p class="body">
|
|
To configure the check so that it checks floating-point
|
|
numbers that are neither 0, 0.5, nor 1:
|
|
</p>
|
|
<pre class="body">
|
|
<module name="MagicNumber">
|
|
<property name="tokens" value="NUM_DOUBLE, NUM_FLOAT"/>
|
|
<property name="ignoreNumbers" value="0, 0.5, 1"/>
|
|
</module>
|
|
</pre>
|
|
<h4>Package</h4>
|
|
<p class="body">
|
|
com.puppycrawl.tools.checkstyle.checks
|
|
</p>
|
|
<h4>Parent Module</h4>
|
|
<p class="body">
|
|
<a href="config.html#treewalker">TreeWalker</a>
|
|
</p>
|
|
|
|
<!-- --> <a name="MissingSwitchDefault"></a> <h2>MissingSwitchDefault</h2> <h4>Description</h4>
|
|
<p class="body">
|
|
Checks that switch statement has "default" clause.
|
|
</p>
|
|
<p class="body">
|
|
Rationale: It's usually a good idea to introduce a default case
|
|
in every switch statement. Even if the developer is sure that
|
|
all currently possible cases are covered, this should be
|
|
expressed in the default branch, e.g. by using an
|
|
assertion. This way the code is protected aginst later changes,
|
|
e.g. introduction of new types in an enumeration type.
|
|
</p>
|
|
<h4>Examples</h4>
|
|
<p class="body">
|
|
To configure the check:
|
|
</p>
|
|
<pre class="body">
|
|
<module name="MissingSwitchDefault"/>
|
|
</pre>
|
|
<h4>Package</h4>
|
|
<p class="body">
|
|
com.puppycrawl.tools.checkstyle.checks
|
|
</p>
|
|
<h4>Parent Module</h4>
|
|
<p class="body">
|
|
<a href="config.html#treewalker">TreeWalker</a>
|
|
</p>
|
|
|
|
<!-- --> <a name="SimplifyBooleanExpression"></a> <h2>SimplifyBooleanExpression</h2>
|
|
<h4>Description</h4>
|
|
<p class="body">
|
|
Checks for overly complicated boolean expressions. Currently finds code like <span class="code">
|
|
if (b == true)</span>, <span class="code">b || true</span>, <span class="code">!false</span>, etc.
|
|
</p>
|
|
<p class="body">
|
|
Rationale: Complex boolean logic makes code hard to understand and maintain.
|
|
</p>
|
|
<h4>Example</h4>
|
|
<p class="body">
|
|
To configure the check:
|
|
</p>
|
|
<pre class="body">
|
|
<module name="SimplifyBooleanExpression"/>
|
|
</pre>
|
|
<h4>Package</h4>
|
|
<p class="body">
|
|
com.puppycrawl.tools.checkstyle.checks
|
|
</p>
|
|
<h4>Parent Module</h4>
|
|
<p class="body">
|
|
<a href="config.html#treewalker">TreeWalker</a>
|
|
</p>
|
|
|
|
<!-- --> <a name="SimplifyBooleanReturn"></a> <h2>SimplifyBooleanReturn</h2> <h4>Description</h4>
|
|
<p class="body">
|
|
Checks for overly complicated boolean return statements. For example the following code
|
|
</p>
|
|
<pre class="body">
|
|
if (valid())
|
|
return false;
|
|
else
|
|
return true;
|
|
</pre>
|
|
<p class="body">
|
|
could be written as
|
|
</p>
|
|
<pre class="body">
|
|
return !valid();
|
|
</pre>
|
|
<p class="body">
|
|
The Idea for this Check has been shamelessly stolen
|
|
from the equivalent <a href="http://pmd.sourceforge.net">PMD</a> rule.
|
|
</p>
|
|
<h4>Example</h4>
|
|
<p class="body">
|
|
To configure the check:
|
|
</p>
|
|
<pre class="body">
|
|
<module name="SimplifyBooleanReturn"/>
|
|
</pre>
|
|
<h4>Package</h4>
|
|
<p class="body">
|
|
com.puppycrawl.tools.checkstyle.checks
|
|
</p>
|
|
<h4>Parent Module</h4>
|
|
<p class="body">
|
|
<a href="config.html#treewalker">TreeWalker</a>
|
|
</p>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
<hr />
|
|
<div><a href="index.html">Back to the Checkstyle Home Page</a></div>
|
|
<p class="copyright">
|
|
Copyright © 2002-2003 Oliver Burn. All rights Reserved.
|
|
</p>
|
|
|
|
</body>
|
|
</html>
|