View Javadoc
1   /*
2   MagicNumber
3   ignoreNumbers = (default)-1, 0, 1, 2
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 = 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 InputMagicNumberIntegersOnly1 {
25      public void magicMethod() {
26  
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  
37          int int_var1 = 1;
38          int int_var2 = (2);
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];
45  
46          int_var1 = 1 + 2;
47          int_var1 += 1;
48          double_var1 = 1.0 + 2.0;
49  
50          for (int i = 0; i < 2; i++);
51  
52          if (1 < 2);
53  
54          if (1.0 < 2.0);
55  
56  
57          int int_magic1 = 3_000; // violation
58          double double_magic1 = 1.5_0;
59          int int_magic2 = (3 + 4); // 2 violations
60  
61          int_array = new int[3]; // violation
62  
63          int_magic1 += 3; // violation
64          double_magic1 *= 1.5;
65  
66          for (int j = 3; j < 5; j += 3) { // 3 violations
67              int_magic1++;
68          }
69  
70          if (int_magic1 < 3) { // violation
71              int_magic1 = int_magic1 + 3; // violation
72          }
73  
74  
75          int octalVar0 = 00;
76          int octalVar8 = 010; // violation
77          int octalVar9 = 011; // violation
78  
79          long longOctalVar8 = 0_10L; // violation
80          long longOctalVar9 = 011l; // violation
81  
82  
83          int hexVar0 = 0x0;
84          int hexVar16 = 0x10; // violation
85          int hexVar17 = 0X011;  // violation
86          long longHexVar0 = 0x0L;
87          long longHexVar16 = 0x10L;  // violation
88          long longHexVar17 = 0X11l; // violation
89      }
90  }
91  
92  interface Blah2IntegersOnly1
93  {
94    int LOW = 5;
95    int HIGH = 78;
96  }
97  
98  class ArrayMagicTestIntegersOnly1
99  {
100     private static final int[] NONMAGIC = {3};
101     private int[] magic = {3}; // violation
102     private static final int[][] NONMAGIC2 = {{1}, {2}, {3}};
103 }