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                              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 InputMagicNumberDefaults
21   * @author Rick Giles
22   * @version 6-May-2003
23   */
24  public class InputMagicNumberDefault1 {
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          int int_var1 = 1;
37          int int_var2 = (2);
38          long long_var1 = 0L;
39          long long_var2 = 0l;
40          double double_var1 = 0D;
41          double double_var2 = 0d;
42  
43          int[] int_array = new int[2];
44  
45          int_var1 = 1 + 2;
46          int_var1 += 1;
47          double_var1 = 1.0 + 2.0;
48  
49          for (int i = 0; i < 2; i++);
50  
51          if (1 < 2);
52  
53          if (1.0 < 2.0);
54  
55  
56          int int_magic1 = 3_000; // violation
57          double double_magic1 = 1.5_0; // violation
58          int int_magic2 = (3 + 4); // 2 violations
59  
60          int_array = new int[3]; // violation
61  
62          int_magic1 += 3;  // violation
63          double_magic1 *= 1.5; // violation
64  
65          for (int j = 3; j < 5; j += 3) { // 3 violations
66              int_magic1++;
67          }
68  
69          if (int_magic1 < 3) { // violation
70              int_magic1 = int_magic1 + 3; // violation
71          }
72  
73          int octalVar0 = 00;
74          int octalVar8 = 010; // violation
75          int octalVar9 = 011; // violation
76  
77          long longOctalVar8 = 0_10L; // violation
78          long longOctalVar9 = 011l;  // violation
79  
80          int hexVar0 = 0x0;
81          int hexVar16 = 0x10; // violation
82          int hexVar17 = 0X011; // violation
83          long longHexVar0 = 0x0L;
84          long longHexVar16 = 0x10L; // violation
85          long longHexVar17 = 0X11l; // violation
86      }
87  }
88  
89  interface Blah2Default1
90  {
91    int LOW = 5;
92    int HIGH = 78;
93  }
94  
95  class ArrayMagicTestDefault1
96  {
97      private static final int[] NONMAGIC = {3};
98      private int[] magic = {3}; // violation
99      private static final int[][] NONMAGIC2 = {{1}, {2}, {3}};
100 }