KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > InputMagicNumber


1 package com.puppycrawl.tools.checkstyle;
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;
12         final long LONG_CONST1 = 100L;
13         final long LONG_CONST2 = 100l;
14         final float FLOAT_CONST1 = 1.5F;
15         final float FLOAT_CONST2 = 1.5f;
16         final double DOUBLE_CONST1 = 1.5D;
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;
42         double double_magic1 = 1.5;
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 = 010L;
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 Blah
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 JavaDoc MYLIST = new java.util.ArrayList JavaDoc()
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
// (at least in the default configuration of MagicNumberCheck)
140
public static final Integer JavaDoc DEFAULT_INT = new Integer JavaDoc(27);
141     public static final int SECS_PER_DAY = 24 * 60 * 60;
142     public static final javax.swing.border.Border JavaDoc STD_BORDER =
143         javax.swing.BorderFactory.createEmptyBorder(3, 3, 3, 3);
144 }
145
146 enum MyEnum
147 {
148     A(3),
149     B(54);
150
151     private MyEnum(int value)
152     {
153         
154     }
155 }
156
Popular Tags