MagicNumberCheck: javadoc and xdoc was updated to describe all options, examples were added. Issue #148
This commit is contained in:
parent
7a1b0b12a6
commit
d58f720c81
|
|
@ -27,18 +27,67 @@ import java.util.Arrays;
|
|||
|
||||
/**
|
||||
* <p>
|
||||
* Checks for magic numbers.
|
||||
* Checks that there are no <a href="http://en.wikipedia.org/wiki/Magic_number_%28programming%29">
|
||||
* "magic numbers"</a> 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>
|
||||
* <p>
|
||||
* An example of how to configure the check to ignore
|
||||
* numbers 0, 1, 1.5, 2:
|
||||
* Check have following options:
|
||||
* ignoreHashCodeMethod - ignore magic numbers in hashCode methods;
|
||||
* ignoreAnnotation - ignore magic numbers in annotation declarations;
|
||||
* ignoreFieldDeclaration - ignore magic numbers in field declarations.
|
||||
* <p>
|
||||
* To configure the check with default configuration:
|
||||
* </p>
|
||||
* <pre>
|
||||
* <module name="MagicNumber">
|
||||
* <property name="ignoreNumbers" value="0, 1, 1.5, 2"/>
|
||||
* <property name="ignoreHashCodeMethod" value="true"/>
|
||||
* </module>
|
||||
* <module name="MagicNumber"/>
|
||||
* </pre>
|
||||
* <p>
|
||||
* results is following violations:
|
||||
* </p>
|
||||
* <pre>
|
||||
* <code>
|
||||
* {@literal @}MyAnnotation(6) // violation
|
||||
* class MyClass {
|
||||
* private field = 7; // violation
|
||||
*
|
||||
* void foo() {
|
||||
* int i = i + 1; // no violation
|
||||
* int j = j + 8; // violation
|
||||
* }
|
||||
* }
|
||||
* </code>
|
||||
* </pre>
|
||||
* <p>
|
||||
* To configure the check so that it checks floating-point numbers
|
||||
* that are not 0, 0.5, or 1:
|
||||
* </p>
|
||||
* <pre>
|
||||
* <module name="MagicNumber">
|
||||
* <property name="tokens" value="NUM_DOUBLE, NUM_FLOAT"/>
|
||||
* <property name="ignoreNumbers" value="0, 0.5, 1"/>
|
||||
* <property name="ignoreFieldDeclaration" value="true"/>
|
||||
* <property name="ignoreAnnotation" value="true"/>
|
||||
* </module>
|
||||
* </pre>
|
||||
* <p>
|
||||
* results is following violations:
|
||||
* </p>
|
||||
* <pre>
|
||||
* <code>
|
||||
* {@literal @}MyAnnotation(6) // no violation
|
||||
* class MyClass {
|
||||
* private field = 7; // no violation
|
||||
*
|
||||
* void foo() {
|
||||
* int i = i + 1; // no violation
|
||||
* int j = j + 8; // violation
|
||||
* }
|
||||
* }
|
||||
* </code>
|
||||
* </pre>
|
||||
*
|
||||
* @author Rick Giles
|
||||
* @author Lars Kühne
|
||||
* @author Daniel Solano Gómez
|
||||
|
|
@ -75,10 +124,13 @@ public class MagicNumberCheck extends Check
|
|||
|
||||
/** the numbers to ignore in the check, sorted */
|
||||
private double[] ignoreNumbers = {-1, 0, 1, 2};
|
||||
|
||||
/** Whether to ignore magic numbers in a hash code method. */
|
||||
private boolean ignoreHashCodeMethod;
|
||||
|
||||
/** Whether to ignore magic numbers in annotation. */
|
||||
private boolean ignoreAnnotation;
|
||||
|
||||
/** Whether to ignore magic numbers in field declaration. */
|
||||
private boolean ignoreFieldDeclaration;
|
||||
|
||||
|
|
|
|||
|
|
@ -925,9 +925,10 @@ class SomeClass
|
|||
<section name="MagicNumber">
|
||||
<subsection name="Description">
|
||||
<p>
|
||||
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.
|
||||
Checks that there are no <a href="http://en.wikipedia.org/wiki/Magic_number_%28programming%29">
|
||||
"magic numbers"</a> 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>
|
||||
</subsection>
|
||||
|
||||
|
|
@ -963,22 +964,36 @@ class SomeClass
|
|||
<td><a href="property_types.html#boolean">boolean</a></td>
|
||||
<td><span class="default">false</span></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>ignoreFieldDeclaration</td>
|
||||
<td>ignore magic numbers in field declarations.</td>
|
||||
<td><a href="property_types.html#boolean">boolean</a></td>
|
||||
<td><span class="default">false</span></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>ignoreFieldDeclaration</td>
|
||||
<td>ignore magic numbers in field declarations.</td>
|
||||
<td><a href="property_types.html#boolean">boolean</a></td>
|
||||
<td><span class="default">false</span></td>
|
||||
</tr>
|
||||
</table>
|
||||
</subsection>
|
||||
|
||||
<subsection name="Examples">
|
||||
<p>
|
||||
To configure the check:
|
||||
To configure the check with default configuration:
|
||||
</p>
|
||||
<source>
|
||||
<module name="MagicNumber"/>
|
||||
</source>
|
||||
<p>
|
||||
results is following violations:
|
||||
</p>
|
||||
<source>
|
||||
@MyAnnotation(6) // violation
|
||||
class MyClass {
|
||||
private field = 7; // violation
|
||||
|
||||
void foo() {
|
||||
int i = i + 1; // no violation
|
||||
int j = j + 8; // violation
|
||||
}
|
||||
}
|
||||
</source>
|
||||
|
||||
<p>
|
||||
To configure the check so that it checks floating-point numbers
|
||||
|
|
@ -989,8 +1004,24 @@ class SomeClass
|
|||
<property name="tokens" value="NUM_DOUBLE, NUM_FLOAT"/>
|
||||
<property name="ignoreNumbers" value="0, 0.5, 1"/>
|
||||
<property name="ignoreFieldDeclaration" value="true"/>
|
||||
<property name="ignoreAnnotation" value="true"/>
|
||||
</module>
|
||||
</source>
|
||||
<p>
|
||||
results is following violations:
|
||||
</p>
|
||||
<source>
|
||||
@MyAnnotation(6) // no violation
|
||||
class MyClass {
|
||||
private field = 7; // no violation
|
||||
|
||||
void foo() {
|
||||
int i = i + 1; // no violation
|
||||
int j = j + 8; // violation
|
||||
}
|
||||
}
|
||||
</source>
|
||||
|
||||
</subsection>
|
||||
|
||||
<subsection name="Package">
|
||||
|
|
|
|||
Loading…
Reference in New Issue