/
TargetTypeTest.java
422 lines (354 loc) · 12.1 KB
/
TargetTypeTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* Copyright 2018 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.errorprone.util.testdata;
import java.io.Serializable;
import java.util.List;
abstract class TargetTypeTest {
void unary() {
System.out.println(
// BUG: Diagnostic contains: boolean
!detectWrappedBoolean());
System.out.println(
// BUG: Diagnostic contains: boolean
!detectPrimitiveBoolean());
System.out.println(
// BUG: Diagnostic contains: int
~detectPrimitiveInt());
System.out.println(
// BUG: Diagnostic contains: int
~detectWrappedInteger());
System.out.println(
// BUG: Diagnostic contains: int
~detectPrimitiveByte());
}
void binary(boolean b) {
// BUG: Diagnostic contains: int
long a1 = detectPrimitiveInt() + 20;
// BUG: Diagnostic contains: int
long a2 = detectWrappedInteger() - 20;
// BUG: Diagnostic contains: long
long a3 = detectPrimitiveInt() + 20L;
// BUG: Diagnostic contains: long
long a4 = detectWrappedInteger() - 20L;
// BUG: Diagnostic contains: boolean
boolean b1 = detectPrimitiveBoolean() & b;
// BUG: Diagnostic contains: boolean
boolean b2 = b || detectWrappedBoolean();
// BUG: Diagnostic contains: java.lang.String
String s1 = detectString() + "";
// BUG: Diagnostic contains: java.lang.String
String s2 = null + detectString();
// BUG: Diagnostic contains: java.lang.String
String s3 = 0 + detectString();
// BUG: Diagnostic contains: java.lang.String
boolean eq1 = detectString() == "";
// BUG: Diagnostic contains: java.lang.String
boolean eq2 = null != detectString();
// BUG: Diagnostic contains: int
boolean eq3 = detectPrimitiveInt() == 0;
// BUG: Diagnostic contains: int
boolean eq4 = 0 == detectWrappedInteger();
}
void binary_shift() {
// The shift operator is unusual in terms of binary operators, in that the operands undergo
// unary numeric promotion separately.
// BUG: Diagnostic contains: int
System.out.println(detectPrimitiveInt() << 1L);
// BUG: Diagnostic contains: int
System.out.println(1L << detectPrimitiveInt());
int i = 1;
// BUG: Diagnostic contains: int
i >>>= detectPrimitiveInt();
// BUG: Diagnostic contains: int
i >>>= detectWrappedInteger();
long a = 1;
// BUG: Diagnostic contains: int
a <<= detectPrimitiveInt();
// BUG: Diagnostic contains: int
a >>>= detectWrappedInteger();
// BUG: Diagnostic contains: int
a >>= detectWrappedInteger();
}
void conditional_condition() {
// BUG: Diagnostic contains: boolean
System.out.println(detectPrimitiveBoolean() ? "" : "");
// BUG: Diagnostic contains: boolean
System.out.println(detectWrappedBoolean() ? "" : "");
}
void conditional_trueExpression(boolean b) {
// BUG: Diagnostic contains: int
System.out.println(b ? detectWrappedInteger() : 0);
}
void conditional_trueExpression_noUnboxing(boolean b) {
// BUG: Diagnostic contains: java.lang.Integer
System.out.println(b ? detectWrappedInteger() : Integer.valueOf(0));
}
void conditional_conditionalInCondition(boolean b1, boolean b2) {
// BUG: Diagnostic contains: long
System.out.println(((detectPrimitiveInt() != 0L) ? b1 : b2) ? "" : "");
}
void ifStatement() {
if (
// BUG: Diagnostic contains: boolean
detectPrimitiveBoolean()) {}
if (
// BUG: Diagnostic contains: boolean
detectWrappedBoolean()) {}
}
void ifElseStatement() {
if (true) {
} else if (
// BUG: Diagnostic contains: boolean
detectPrimitiveBoolean()) {
}
if (true) {
} else if (
// BUG: Diagnostic contains: boolean
detectWrappedBoolean()) {
}
}
void whileLoop() {
while (
// BUG: Diagnostic contains: boolean
detectPrimitiveBoolean()) {}
while (
// BUG: Diagnostic contains: boolean
detectWrappedBoolean()) {}
}
void doWhileLoop() {
do {} while (
// BUG: Diagnostic contains: boolean
detectPrimitiveBoolean());
do {} while (
// BUG: Diagnostic contains: boolean
detectWrappedBoolean());
}
void forLoop() {
for (;
// BUG: Diagnostic contains: boolean
detectPrimitiveBoolean(); ) {}
for (;
// BUG: Diagnostic contains: boolean
detectWrappedBoolean(); ) {}
}
void typesOfDetectMethods() {
// BUG: Diagnostic contains: byte
byte primitiveByte = detectPrimitiveByte();
// BUG: Diagnostic contains: boolean
boolean primitiveBoolean = detectPrimitiveBoolean();
// BUG: Diagnostic contains: int
int primitiveInt = detectPrimitiveInt();
// BUG: Diagnostic contains: java.lang.Boolean
Boolean wrappedBoolean = detectWrappedBoolean();
// BUG: Diagnostic contains: java.lang.Integer
Integer wrappedInteger = detectWrappedInteger();
}
void arrayAccess(String[] s) {
// BUG: Diagnostic contains: int
System.out.println(s[detectPrimitiveInt()]);
// BUG: Diagnostic contains: int
System.out.println(s[detectWrappedInteger()]);
// BUG: Diagnostic contains: java.lang.String[]
System.out.println(detectStringArray()[0]);
}
void switchStatement() {
// BUG: Diagnostic contains: int
switch (detectPrimitiveInt()) {
}
// BUG: Diagnostic contains: int
switch (detectWrappedInteger()) {
}
// BUG: Diagnostic contains: java.lang.String
switch (detectString()) {
}
// BUG: Diagnostic contains: com.google.errorprone.util.testdata.TargetTypeTest.ThisEnum
switch (detectThisEnum()) {
}
}
int[] array_intInPrimitiveIntArray() {
// BUG: Diagnostic contains: int
int[] array = {detectPrimitiveInt()};
// BUG: Diagnostic contains: int
return new int[] {detectPrimitiveInt()};
}
int[][] array_intInPrimitiveIntArray2D() {
// BUG: Diagnostic contains: int
int[][] array = {{detectPrimitiveInt()}};
// BUG: Diagnostic contains: int
return new int[][] {{detectPrimitiveInt()}};
}
int[][][] array_byteInPrimitiveIntArray3D() {
// BUG: Diagnostic contains: int
int[][][] array = {{{detectPrimitiveByte()}}};
// BUG: Diagnostic contains: int
return new int[][][] {{{detectPrimitiveByte()}}};
}
int[] array_byteInPrimitiveIntArray() {
// BUG: Diagnostic contains: int
int[] array = {detectPrimitiveByte()};
// BUG: Diagnostic contains: int
return new int[] {detectPrimitiveByte()};
}
Integer[] array_intInWrappedIntegerArray() {
// BUG: Diagnostic contains: java.lang.Integer
Integer[] array = {detectPrimitiveInt()};
// BUG: Diagnostic contains: java.lang.Integer
return new Integer[] {detectPrimitiveInt()};
}
Integer[] array_integerInWrappedIntegerArray() {
// BUG: Diagnostic contains: java.lang.Integer
Integer[] array = {detectWrappedInteger()};
// BUG: Diagnostic contains: java.lang.Integer
return new Integer[] {detectWrappedInteger()};
}
int[] array_integerInPrimitiveIntArray() {
// BUG: Diagnostic contains: int
int[] array = {detectWrappedInteger()};
// BUG: Diagnostic contains: int
return new int[] {detectWrappedInteger()};
}
Integer[][] array_integerInWrappedIntegerArray2D() {
// BUG: Diagnostic contains: java.lang.Integer
Integer[][] array = {{detectWrappedInteger()}};
// BUG: Diagnostic contains: java.lang.Integer
return new Integer[][] {{detectWrappedInteger()}};
}
Integer[][][] array_integerInWrappedIntegerArray3D() {
// BUG: Diagnostic contains: java.lang.Integer
Integer[][][] array = {{{detectWrappedInteger()}}};
// BUG: Diagnostic contains: java.lang.Integer
return new Integer[][][] {{{detectWrappedInteger()}}};
}
Serializable[] array_integerInSerializableArray() {
// BUG: Diagnostic contains: java.io.Serializable
Serializable[] array = {detectWrappedInteger()};
// BUG: Diagnostic contains: java.io.Serializable
return new Serializable[] {detectWrappedInteger()};
}
Object[][][] array_integerInObjectArray3D() {
// BUG: Diagnostic contains: java.lang.Object
Object[][][] array = {{{detectWrappedInteger()}}};
// BUG: Diagnostic contains: java.lang.Object
return new Object[][][] {{{detectWrappedInteger()}}};
}
Object[][] array_integerArrayInObjectArray() {
// BUG: Diagnostic contains: java.lang.Integer
Object[][] array = {new Integer[] {detectPrimitiveInt()}};
// BUG: Diagnostic contains: java.lang.Integer
return new Object[][] {new Integer[] {detectPrimitiveInt()}};
}
Object[][] array_arrayHiddenInsideObjectArray() {
// BUG: Diagnostic contains: java.lang.Integer
Object[][] array = {{new Integer[] {detectPrimitiveInt()}}};
// BUG: Diagnostic contains: java.lang.Integer
return new Object[][] {{new Integer[] {detectPrimitiveInt()}}};
}
Integer[][] array_primitiveByteInDimensions() {
// BUG: Diagnostic contains: int
return new Integer[detectPrimitiveByte()][];
}
String[][] array_wrappedIntegerInDimensions() {
// BUG: Diagnostic contains: int
return new String[detectWrappedInteger()][];
}
String[][] array_initializeWithArray() {
// BUG: Diagnostic contains: java.lang.String[]
String[][] s = {detectStringArray()};
return s;
}
String methodChain() {
// BUG: Diagnostic contains: java.lang.Boolean
Boolean b = TargetTypeTest.detectWrappedBoolean();
// BUG: Diagnostic contains: java.lang.Integer
return detectWrappedInteger().toString();
}
void compoundAssignment_numeric(Integer i, int j, Long k) {
// BUG: Diagnostic contains: int
i /= detectWrappedInteger();
// BUG: Diagnostic contains: int
i *= (detectWrappedInteger());
// BUG: Diagnostic contains: int
j -= detectWrappedInteger();
// BUG: Diagnostic contains: long
k /= detectWrappedInteger();
}
void compoundAssignment_string(String s) {
// BUG: Diagnostic contains: java.lang.String
s += detectWrappedInteger();
// BUG: Diagnostic contains: java.lang.String
s += detectPrimitiveInt();
}
void compoundAssignment_boolean(boolean b) {
// BUG: Diagnostic contains: boolean
b &= detectWrappedBoolean();
// BUG: Diagnostic contains: boolean
b |= detectPrimitiveBoolean();
}
void concatenation(String s, Object a) {
// BUG: Diagnostic contains: java.lang.String
a = s + detectWrappedInteger();
// BUG: Diagnostic contains: java.lang.String
a = s + detectPrimitiveByte();
// BUG: Diagnostic contains: java.lang.String
a = s + detectVoid();
// BUG: Diagnostic contains: java.lang.String
a = s + detectStringArray();
}
abstract <T> T id(T t);
abstract <T> List<T> list(List<T> t);
void generic() {
// BUG: Diagnostic contains: java.lang.String
String s = id(detectString());
// BUG: Diagnostic contains: java.lang.Integer
int i = id(detectPrimitiveInt());
// BUG: Diagnostic contains: java.util.List<java.lang.String>
List<String> y = id(detectStringList());
// BUG: Diagnostic contains: java.lang.Integer
Integer z = id(detectPrimitiveInt());
}
// Helper methods that we can search for.
static byte detectPrimitiveByte() {
return 0;
}
static boolean detectPrimitiveBoolean() {
return true;
}
static int detectPrimitiveInt() {
return 0;
}
static Boolean detectWrappedBoolean() {
return true;
}
static Integer detectWrappedInteger() {
return 0;
}
static String detectString() {
return "";
}
static String[] detectStringArray() {
return new String[] {};
}
static ThisEnum detectThisEnum() {
return null;
}
static Void detectVoid() {
return null;
}
static List<String> detectStringList() {
return null;
}
enum ThisEnum {}
}