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