1 /*xml 2 <module name="Checker"> 3 <module name="TreeWalker"> 4 <module name="MagicNumber"> 5 <property name="constantWaiverParentToken" value="ASSIGN, ARRAY_INIT, EXPR, 6 UNARY_PLUS, UNARY_MINUS, TYPECAST, ELIST, DIV, PLUS"/> 7 </module> 8 </module> 9 </module> 10 */ 11 12 package com.puppycrawl.tools.checkstyle.checks.coding.magicnumber; 13 14 // xdoc section -- start 15 @Example5.Annotation(6) // violation, ''6' is a magic number.' 16 public class Example5 { 17 private int field = 7; // violation, ''7' is a magic number.' 18 19 void method1() { 20 int i = 1; 21 int j = 8; // violation, ''8' is a magic number.' 22 } 23 public void method2() { 24 final TestClass testObject = new TestClass(62); 25 // violation above, ''62' is a magic number.' 26 final int a = 3; // ok as waiver is ASSIGN 27 final int[] b = {4, 5}; // ok as waiver is ARRAY_INIT 28 final int c = -3; // ok as waiver is UNARY_MINUS 29 final int d = +4; // ok as waiver is UNARY_PLUS 30 final int e = method3(10, 20); 31 // 2 violations above: 32 // ''10' is a magic number.' 33 // ''20' is a magic number.' 34 final int f = 3 * 4; 35 // 2 violations above: 36 // ''3' is a magic number.' 37 // ''4' is a magic number.' 38 final int g = 3 / 4; // ok as waiver is DIV 39 final int h = 3 + 4; // ok as waiver is PLUS 40 final int i = 3 - 4; 41 // 2 violations above: 42 // ''3' is a magic number.' 43 // ''4' is a magic number.' 44 final int j = (int) 3.4; //ok as waiver is TYPECAST 45 } 46 private int method3(int a, int b) { 47 return a + b; 48 } 49 public int hashCode() { 50 return 10; // violation, ''10' is a magic number.' 51 } 52 @interface Annotation { 53 int value() default 10; 54 int[] value2() default {10}; 55 } 56 class TestClass { 57 TestClass(int field) {} 58 } 59 } 60 // xdoc section -- end