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.
- To configure the check: + To configure the check with default configuration:
+ results is following violations: +
+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: +
+