|
| 1 | +import java.io.*; |
| 2 | + |
| 3 | +public class Test { |
| 4 | + public static void main(String[] args) { |
| 5 | + test1(); |
| 6 | + test2(); |
| 7 | + test3(); |
| 8 | + } |
| 9 | + |
| 10 | + static void test1() { |
| 11 | + Byte.parseByte("123"); |
| 12 | + Byte.decode("123"); |
| 13 | + Byte.valueOf("123"); |
| 14 | + Byte.valueOf("123", 10); |
| 15 | + Byte.valueOf("7f", 16); |
| 16 | + new Byte("123"); |
| 17 | + new Byte((byte) 123); // don't flag: wrong constructor |
| 18 | + |
| 19 | + Short.parseShort("123"); |
| 20 | + Short.decode("123"); |
| 21 | + Short.valueOf("123"); |
| 22 | + Short.valueOf("123", 10); |
| 23 | + Short.valueOf("7abc", 16); |
| 24 | + new Short("123"); |
| 25 | + new Short((short) 123); // don't flag: wrong constructor |
| 26 | + |
| 27 | + Integer.parseInt("123"); |
| 28 | + Integer.decode("123"); |
| 29 | + Integer.valueOf("123"); |
| 30 | + Integer.valueOf("123", 10); |
| 31 | + Integer.valueOf("1234beef", 16); |
| 32 | + new Integer("123"); |
| 33 | + new Integer(123); // don't flag: wrong constructor |
| 34 | + |
| 35 | + Long.parseLong("123"); |
| 36 | + Long.decode("123"); |
| 37 | + Long.valueOf("123"); |
| 38 | + Long.valueOf("123", 10); |
| 39 | + Long.valueOf("deadbeef", 16); |
| 40 | + new Long("123"); |
| 41 | + new Long(123l); // don't flag: wrong constructor |
| 42 | + |
| 43 | + Float.parseFloat("2.7818281828"); |
| 44 | + Float.valueOf("2.7818281828"); |
| 45 | + new Float("2.7818281828"); |
| 46 | + new Float(2.7818281828f); // don't flag: wrong constructor |
| 47 | + |
| 48 | + Double.parseDouble("2.7818281828"); |
| 49 | + Double.valueOf("2.7818281828"); |
| 50 | + new Double("2.7818281828"); |
| 51 | + new Double(2.7818281828); // don't flag: wrong constructor |
| 52 | + } |
| 53 | + |
| 54 | + static void test2() { |
| 55 | + // Don't flag any of these. The exception is caught. |
| 56 | + try { |
| 57 | + Byte.parseByte("123"); |
| 58 | + Byte.decode("123"); |
| 59 | + Byte.valueOf("123"); |
| 60 | + Byte.valueOf("123", 10); |
| 61 | + Byte.valueOf("7f", 16); |
| 62 | + new Byte("123"); |
| 63 | + |
| 64 | + Short.parseShort("123"); |
| 65 | + Short.decode("123"); |
| 66 | + Short.valueOf("123"); |
| 67 | + Short.valueOf("123", 10); |
| 68 | + Short.valueOf("7abc", 16); |
| 69 | + new Short("123"); |
| 70 | + |
| 71 | + Integer.parseInt("123"); |
| 72 | + Integer.decode("123"); |
| 73 | + Integer.valueOf("123"); |
| 74 | + Integer.valueOf("123", 10); |
| 75 | + Integer.valueOf("1234beef", 16); |
| 76 | + new Integer("123"); |
| 77 | + |
| 78 | + Long.parseLong("123"); |
| 79 | + Long.decode("123"); |
| 80 | + Long.valueOf("123"); |
| 81 | + Long.valueOf("123", 10); |
| 82 | + Long.valueOf("deadbeef", 16); |
| 83 | + new Long("123"); |
| 84 | + |
| 85 | + Float.parseFloat("2.7818281828"); |
| 86 | + Float.valueOf("2.7818281828"); |
| 87 | + new Float("2.7818281828"); |
| 88 | + |
| 89 | + Double.parseDouble("2.7818281828"); |
| 90 | + Double.valueOf("2.7818281828"); |
| 91 | + new Double("2.7818281828"); |
| 92 | + } |
| 93 | + catch (NumberFormatException e) { |
| 94 | + // parse error |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + static void test3() throws NumberFormatException { |
| 99 | + // Don't flag any of these: the exception is explcitly declared |
| 100 | + Byte.parseByte("123"); |
| 101 | + Byte.decode("123"); |
| 102 | + Byte.valueOf("123"); |
| 103 | + Byte.valueOf("123", 10); |
| 104 | + Byte.valueOf("7f", 16); |
| 105 | + new Byte("123"); |
| 106 | + |
| 107 | + Short.parseShort("123"); |
| 108 | + Short.decode("123"); |
| 109 | + Short.valueOf("123"); |
| 110 | + Short.valueOf("123", 10); |
| 111 | + Short.valueOf("7abc", 16); |
| 112 | + new Short("123"); |
| 113 | + |
| 114 | + Integer.parseInt("123"); |
| 115 | + Integer.decode("123"); |
| 116 | + Integer.valueOf("123"); |
| 117 | + Integer.valueOf("123", 10); |
| 118 | + Integer.valueOf("1234beef", 16); |
| 119 | + new Integer("123"); |
| 120 | + |
| 121 | + Long.parseLong("123"); |
| 122 | + Long.decode("123"); |
| 123 | + Long.valueOf("123"); |
| 124 | + Long.valueOf("123", 10); |
| 125 | + Long.valueOf("deadbeef", 16); |
| 126 | + new Long("123"); |
| 127 | + |
| 128 | + Float.parseFloat("2.7818281828"); |
| 129 | + Float.valueOf("2.7818281828"); |
| 130 | + new Float("2.7818281828"); |
| 131 | + |
| 132 | + Double.parseDouble("2.7818281828"); |
| 133 | + Double.valueOf("2.7818281828"); |
| 134 | + new Double("2.7818281828"); |
| 135 | + } |
| 136 | +} |
0 commit comments