View Javadoc
1   package com.puppycrawl.tools.checkstyle.checks.coding.magicnumber;
2   
3   /**
4    * Describe class InputMagicNumber
5    * @author Rick Giles
6    * @version 6-May-2003
7    */
8   public class InputMagicNumber {
9       public void magicMethod() {
10          //constants, ignore
11          final int INT_CONST = 101_000;
12          final long LONG_CONST1 = 100_000L;
13          final long LONG_CONST2 = 100l;
14          final float FLOAT_CONST1 = 1.500_0F;
15          final float FLOAT_CONST2 = 1.5f;
16          final double DOUBLE_CONST1 = 1.500_0D;
17          final double DOUBLE_CONST2 = 1.5d;
18          final double DOUBLE_CONST3 = 1.5;
19  
20          //ignore by default
21          int int_var1 = 1;
22          int int_var2 = (2);
23          long long_var1 = 0L;
24          long long_var2 = 0l;
25          double double_var1 = 0D;
26          double double_var2 = 0d;
27  
28          int[] int_array = new int[2];
29  
30          int_var1 = 1 + 2;
31          int_var1 += 1;
32          double_var1 = 1.0 + 2.0;
33  
34          for (int i = 0; i < 2; i++);
35  
36          if (1 < 2);
37  
38          if (1.0 < 2.0);
39  
40          //magic numbers
41          int int_magic1 = 3_000;
42          double double_magic1 = 1.5_0;
43          int int_magic2 = (3 + 4);
44  
45          int_array = new int[3];
46  
47          int_magic1 += 3;
48          double_magic1 *= 1.5;
49  
50          for (int j = 3; j < 5; j += 3) {
51              int_magic1++;
52          }
53  
54          if (int_magic1 < 3) {
55              int_magic1 = int_magic1 + 3;
56          }
57  
58          //octal
59          int octalVar0 = 00;
60          int octalVar8 = 010;
61          int octalVar9 = 011;
62  
63          long longOctalVar8 = 0_10L;
64          long longOctalVar9 = 011l;
65  
66          //hex
67          int hexVar0 = 0x0;
68          int hexVar16 = 0x10;
69          int hexVar17 = 0X011;
70          long longHexVar0 = 0x0L;
71          long longHexVar16 = 0x10L;
72          long longHexVar17 = 0X11l;
73      }
74  }
75  
76  interface Blah2
77  {
78    int LOW = 5;
79    int HIGH = 78;
80  }
81  
82  class ArrayMagicTest
83  {
84      private static final int[] NONMAGIC = {3};
85      private int[] magic = {3};
86      private static final int[][] NONMAGIC2 = {{1}, {2}, {3}};
87  }
88  
89  /** test long hex */
90  class LongHex
91  {
92      long l = 0xffffffffL;
93  }
94  
95  /** test signed values */
96  class Signed
97  {
98      public static final int CONST_PLUS_THREE = +3;
99      public static final int CONST_MINUS_TWO = -2;
100     private int mPlusThree = +3;
101     private int mMinusTwo = -2;
102     private double mPlusDecimal = +3.5;
103     private double mMinusDecimal = -2.5;
104 }
105 
106 /** test octal and hex negative values */
107 class NegativeOctalHex
108 {
109     private int hexIntMinusOne = 0xffffffff;
110     private long hexLongMinusOne = 0xffffffffffffffffL;
111     private long hexIntMinValue = 0x80000000;
112     private long hexLongMinValue = 0x8000000000000000L;
113     private int octalIntMinusOne = 037777777777;
114     private long octalLongMinusOne = 01777777777777777777777L;
115     private long octalIntMinValue = 020000000000;
116     private long octalLongMinValue = 01000000000000000000000L;
117 }
118 
119 class Cast
120 {
121     public static final int TESTINTVAL = (byte) 0x80;
122 }
123 
124 class ComplexAndFlagged
125 {
126     public static final java.util.List MYLIST = new java.util.ArrayList()
127     {
128         public int size()
129         {
130             // should be flagged although technically inside const definition
131             return 378;
132         }
133     };
134 }
135 
136 class ComplexButNotFlagged
137 {
138     // according to user feedback this is typical code that should not be flagged
139     public final double SpecialSum = 2 + 1e10, SpecialDifference = 4 - java.lang.Math.PI;
140     public final Integer DefaultInit = new Integer(27);
141     public final int SpecsPerDay = 24 * 60 * 60, SpecialRatio = 4 / 3;
142     public final javax.swing.border.Border StdBorder =
143         javax.swing.BorderFactory.createEmptyBorder(3, 3, 3, 3);
144 }
145 
146 enum MyEnum2
147 {
148     A(3),
149     B(54);
150 
151     private MyEnum2(int value)
152     {
153 
154     }
155 }
156 
157 class TestHashCodeMethod {
158     // valid hash code method
159     public int hashCode() {
160         return 31;
161     }
162 
163     // invalid hash code method: has parameters
164     public int hashCode(int val) {
165         return 42;
166     }
167 
168     // invalid hash code method: misspelled
169     public int hashcode() {
170         return 13;
171     }
172 
173     static {
174         int x=21;
175     }
176 
177     {
178         int y=37;
179     }
180 
181     public TestHashCodeMethod() {
182         int z=101;
183     }
184 
185     @InputMagicNumberIntMethodAnnotation(42)
186     public void another() {
187     }
188 
189     @InputMagicNumberIntMethodAnnotation(value=43)
190     public void another2() {
191     }
192     
193     @InputMagicNumberIntMethodAnnotation(-44)
194     public void anotherNegative() {
195     }
196 
197     @InputMagicNumberIntMethodAnnotation(value=-45)
198     public void anotherNegative2() {
199     }
200 }
201 
202 class TestMethodCall {
203 
204 	public TestMethodCall(int x){
205 
206     }
207 
208 	public void method2() {
209         final TestMethodCall dummyObject = new TestMethodCall(62);
210 	}
211 }