View Javadoc
1   /*
2   MagicNumber
3   ignoreNumbers =
4   ignoreHashCodeMethod = (default)false
5   ignoreAnnotation = true
6   ignoreFieldDeclaration = (default)false
7   ignoreAnnotationElementDefaults = (default)true
8   constantWaiverParentToken = (default)TYPECAST, METHOD_CALL, EXPR, ARRAY_INIT, UNARY_MINUS, \
9                               UNARY_PLUS, ELIST, STAR, ASSIGN, PLUS, MINUS, DIV, LITERAL_NEW, \
10                              SR, BSR, SL, BXOR, BOR, BAND, BNOT, QUESTION, COLON, EQUAL, NOT_EQUAL, \
11                              GE, GT, LE, LT, MOD
12  tokens = (default)NUM_DOUBLE, NUM_FLOAT, NUM_INT, NUM_LONG
13  
14  
15  */
16  
17  package com.puppycrawl.tools.checkstyle.checks.coding.magicnumber;
18  
19  /* Config:
20   *
21   * ignoreNumbers ={}
22   * ignoreAnnotation =  true
23   */
24  
25  /**
26   * Describe class InputMagicNumber
27   * @author Rick Giles
28   * @version 6-May-2003
29   */
30  public class InputMagicNumberIgnoreNone1 {
31      public void magicMethod() {
32  
33          final int INT_CONST = 101_000;
34          final long LONG_CONST1 = 100_000L;
35          final long LONG_CONST2 = 100l;
36          final float FLOAT_CONST1 = 1.500_0F;
37          final float FLOAT_CONST2 = 1.5f;
38          final double DOUBLE_CONST1 = 1.500_0D;
39          final double DOUBLE_CONST2 = 1.5d;
40          final double DOUBLE_CONST3 = 1.5;
41  
42  
43          int int_var1 = 1; // violation
44          int int_var2 = (2); // violation
45          long long_var1 = 0L; // violation
46          long long_var2 = 0l; // violation
47          double double_var1 = 0D; // violation
48          double double_var2 = 0d; // violation
49  
50          int[] int_array = new int[2]; // violation
51  
52          int_var1 = 1 + 2; // 2 violations
53          int_var1 += 1;  // violation
54          double_var1 = 1.0 + 2.0; // 2 violations
55  
56          for (int i = 0; i < 2; i++); // 2 violations
57  
58          if (1 < 2); // 2 violations
59  
60          if (1.0 < 2.0); // 2 violations
61  
62  
63          int int_magic1 = 3_000; // violation
64          double double_magic1 = 1.5_0; // violation
65          int int_magic2 = (3 + 4); // 2 violations
66  
67          int_array = new int[3]; // violation
68  
69          int_magic1 += 3; // violation
70          double_magic1 *= 1.5; // violation
71  
72          for (int j = 3; j < 5; j += 3) { // 3 violations
73              int_magic1++;
74          }
75  
76          if (int_magic1 < 3) { // violation
77              int_magic1 = int_magic1 + 3; // violation
78          }
79  
80  
81          int octalVar0 = 00;  // violation
82          int octalVar8 = 010; // violation
83          int octalVar9 = 011; // violation
84  
85          long longOctalVar8 = 0_10L; // violation
86          long longOctalVar9 = 011l; // violation
87  
88  
89          int hexVar0 = 0x0; // violation
90          int hexVar16 = 0x10; // violation
91          int hexVar17 = 0X011;  // violation
92          long longHexVar0 = 0x0L; // violation
93          long longHexVar16 = 0x10L; // violation
94          long longHexVar17 = 0X11l; // violation
95      }
96  }
97  
98  interface Blah2IgnoreNone1
99  {
100   int LOW = 5;
101   int HIGH = 78;
102 }
103 
104 class ArrayMagicTestIgnoreNone1
105 {
106     private static final int[] NONMAGIC = {3};
107     private int[] magic = {3}; // violation
108     private static final int[][] NONMAGIC2 = {{1}, {2}, {3}};
109 }