View Javadoc
1   /*
2   MagicNumber
3   ignoreNumbers = 0, 1, 3.0, 8, 16, 3000
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  /**
20   * Describe class InputMagicNumber
21   * @author Rick Giles
22   * @version 6-May-2003
23   */
24  public class InputMagicNumberIgnoreSome1 {
25      public void magicMethod() {
26          //constants, ignore
27          final int INT_CONST = 101_000;
28          final long LONG_CONST1 = 100_000L;
29          final long LONG_CONST2 = 100l;
30          final float FLOAT_CONST1 = 1.500_0F;
31          final float FLOAT_CONST2 = 1.5f;
32          final double DOUBLE_CONST1 = 1.500_0D;
33          final double DOUBLE_CONST2 = 1.5d;
34          final double DOUBLE_CONST3 = 1.5;
35  
36          //ignore by default
37          int int_var1 = 1;
38          int int_var2 = (2); // violation
39          long long_var1 = 0L;
40          long long_var2 = 0l;
41          double double_var1 = 0D;
42          double double_var2 = 0d;
43  
44          int[] int_array = new int[2]; // violation
45  
46          int_var1 = 1 + 2; // violation
47          int_var1 += 1;
48          double_var1 = 1.0 + 2.0; // violation
49  
50          for (int i = 0; i < 2; i++); // violation
51  
52          if (1 < 2); // violation
53  
54          if (1.0 < 2.0); // violation
55  
56          //magic numbers
57          int int_magic1 = 3_000;
58          double double_magic1 = 1.5_0; // violation
59          int int_magic2 = (3 + 4); // violation
60  
61          int_array = new int[3];
62  
63          int_magic1 += 3;
64          double_magic1 *= 1.5; // violation
65  
66          for (int j = 3; j < 5; j += 3) { // violation
67              int_magic1++;
68          }
69  
70          if (int_magic1 < 3) {
71              int_magic1 = int_magic1 + 3;
72          }
73  
74  
75          int octalVar0 = 00;
76          int octalVar8 = 010;
77          int octalVar9 = 011; // violation
78  
79          long longOctalVar8 = 0_10L;
80          long longOctalVar9 = 011l; // violation
81  
82  
83          int hexVar0 = 0x0;
84          int hexVar16 = 0x10;
85          int hexVar17 = 0X011;  // violation
86          long longHexVar0 = 0x0L;
87          long longHexVar16 = 0x10L;
88          long longHexVar17 = 0X11l; // violation
89      }
90  }
91  
92  interface Blah2IgnoreSome1
93  {
94    int LOW = 5;
95    int HIGH = 78;
96  }
97  
98  class ArrayMagicTestIgnoreSome1
99  {
100     private static final int[] NONMAGIC = {3};
101     private int[] magic = {3};
102     private static final int[][] NONMAGIC2 = {{1}, {2}, {3}};
103 }