From d58f720c819d92f42a37e8863379de3f410cd573 Mon Sep 17 00:00:00 2001 From: Roman Ivanov Date: Fri, 3 Apr 2015 00:23:18 -0700 Subject: [PATCH] MagicNumberCheck: javadoc and xdoc was updated to describe all options, examples were added. Issue #148 --- .../checks/coding/MagicNumberCheck.java | 66 +++++++++++++++++-- src/xdocs/config_coding.xml | 51 +++++++++++--- 2 files changed, 100 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java index c148e1603..92b723e7e 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java @@ -27,18 +27,67 @@ import java.util.Arrays; /** *

- * Checks for magic numbers. + * 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. *

*

- * 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. + *

+ * To configure the check with default configuration: *

*
- * <module name="MagicNumber">
- *    <property name="ignoreNumbers" value="0, 1, 1.5, 2"/>
- *    <property name="ignoreHashCodeMethod" value="true"/>
- * </module>
+ * <module name="MagicNumber"/>
  * 
+ *

+ * results is following violations: + *

+ *
+ * 
+ *   {@literal @}MyAnnotation(6) // violation
+ *   class MyClass {
+ *       private field = 7; // violation
+ *
+ *       void foo() {
+ *          int i = i + 1; // no violation
+ *          int j = j + 8; // violation
+ *       }
+ *   }
+ * 
+ * 
+ *

+ * To configure the check so that it checks floating-point numbers + * that are not 0, 0.5, or 1: + *

+ *
+ *   <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>
+ * 
+ *

+ * results is following violations: + *

+ *
+ * 
+ *   {@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
+ *       }
+ *   }
+ * 
+ * 
+ * * @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; diff --git a/src/xdocs/config_coding.xml b/src/xdocs/config_coding.xml index 382b77312..3ed69c244 100644 --- a/src/xdocs/config_coding.xml +++ b/src/xdocs/config_coding.xml @@ -925,9 +925,10 @@ class SomeClass

- 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 + "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.

@@ -963,22 +964,36 @@ class SomeClass boolean false - - ignoreFieldDeclaration - ignore magic numbers in field declarations. - boolean - false - + + ignoreFieldDeclaration + ignore magic numbers in field declarations. + boolean + false +

- To configure the check: + To configure the check with default configuration:

<module name="MagicNumber"/> +

+ results is following violations: +

+ +@MyAnnotation(6) // violation +class MyClass { + private field = 7; // violation + + void foo() { + int i = i + 1; // no violation + int j = j + 8; // violation + } +} +

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> +

+ results is following violations: +

+ +@MyAnnotation(6) // no violation +class MyClass { + private field = 7; // no violation + + void foo() { + int i = i + 1; // no violation + int j = j + 8; // violation + } +} + +