Skip to main content
aboutsummaryrefslogblamecommitdiffstats
blob: 882416d49f01ada647519418846e17a8c1663be6 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470



                                                       
                     






                                                            
                                                              












                                                                      
                                                                   























































                                                                                                                                                          


































































































































































































































































































































































































                                                                                                                                              
 






































































































































































































































































































































                                                                                                                                           




























                                                                                                                                          
















































                                                                                                    





























                                                                         


























































                                                                         










































































































































                                                                                                                     



                                 
package org.eclipse.jdt.core.tests.compiler.regression;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.tests.util.Util;
import org.eclipse.jdt.core.util.ClassFileBytesDisassembler;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;

public class CastTest extends AbstractRegressionTest {
	
public CastTest(String name) {
	super(name);
}
public static Test suite() {

	if (false) {
	   	TestSuite ts;
		//some of the tests depend on the order of this suite.
		ts = new TestSuite();
		ts.addTest(new CastTest("test221"));
		return new RegressionTestSetup(ts, COMPLIANCE_1_4);
	}
	return setupSuite(testClass());
}

/*
 * check extra checkcast (interface->same interface)
 */
public void test001() {
	this.runConformTest(
		new String[] {
			"X.java",
			"public class X {	\n" +
			"    public static void main(String[] args) {	\n" +
			"       Cloneable c1 = new int[0]; \n"+
			"		Cloneable c2 = (Cloneable)c1; \n" +
			"		System.out.print(\"SUCCESS\");	\n" +
			"    }	\n" +
			"}	\n",
		},
		"SUCCESS");

	ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler();
	String actualOutput = null;
	try {
		byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(new File(OUTPUT_DIR + File.separator  +"X.class"));
		actualOutput =
			disassembler.disassemble(
				classFileBytes,
				"\n",
				ClassFileBytesDisassembler.DETAILED); 
	} catch (org.eclipse.jdt.core.util.ClassFormatException e) {
		assertTrue("ClassFormatException", false);
	} catch (IOException e) {
		assertTrue("IOException", false);
	}

	String expectedOutput =
		"  /*  Method descriptor  #15 ([Ljava/lang/String;)V */\n" + 
		"  public static void main(String[] args);\n" + 
		"    /* Stack: 2, Locals: 3 */\n" + 
		"    Code attribute:\n" + 
		"       0  iconst_0\n" + 
		"       1  newarray #10 int\n" + 
		"       3  astore_1\n" + 
		"       4  aload_1\n" + 
		"       5  astore_2\n" + 
		"       6  getstatic #21 <Field java.lang.System#out java.io.PrintStream>\n" + 
		"       9  ldc #23 <String \"SUCCESS\">\n" + 
		"      11  invokevirtual #29 <Method java.io.PrintStream#print(java.lang.String arg) void>\n" + 
		"      14  return\n";
	if (actualOutput.indexOf(expectedOutput) == -1){
		System.out.println(Util.displayString(actualOutput, 2));
	}
	assertTrue("unexpected bytecode sequence", actualOutput.indexOf(expectedOutput) != -1);
}

public void test002() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	public static void main(String[] args) {\n" + 
			"		// standard expressions\n" + 
			"		String s = (String) null;	// UNnecessary\n" + 
			"		String t = (String) \"hello\";	// UNnecessary\n" + 
			"		float f = (float) 12;			// UNnecessary\n" + 
			"		int i = (int)12.0;				//   necessary\n" + 
			"	}\n" + 
			"}\n"
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 4)\n" + 
		"	String s = (String) null;	// UNnecessary\n" + 
		"	           ^^^^^^^^^^^^^\n" + 
		"Unnecessary cast to type String for expression of type null\n" + 
		"----------\n" + 
		"2. ERROR in X.java (at line 5)\n" + 
		"	String t = (String) \"hello\";	// UNnecessary\n" + 
		"	           ^^^^^^^^^^^^^^^^\n" + 
		"Unnecessary cast to type String for expression of type String\n" + 
		"----------\n" + 
		"3. ERROR in X.java (at line 6)\n" + 
		"	float f = (float) 12;			// UNnecessary\n" + 
		"	          ^^^^^^^^^^\n" + 
		"Unnecessary cast to type float for expression of type int\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}
public void test003() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	public static void main(String[] args) {\n" + 
			"		// message sends		\n" + 
			"		foo((Object) \"hello\");		//   necessary\n" + 
			"		foo((String) \"hello\");			// UNnecessary\n" + 
			"		foo((Object) null);			//   necessary\n" + 
			"		foo((String) null);				// UNnecessary but keep as useful documentation \n" + 
			"	}\n" + 
			"	static void foo(String s) {\n" + 
			"		System.out.println(\"foo(String):\"+s);\n" + 
			"	}\n" + 
			"	static void foo(Object o) {\n" + 
			"		System.out.println(\"foo(Object):\"+o);\n" + 
			"	}\n" + 
			"}\n"
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 5)\n" + 
		"	foo((String) \"hello\");			// UNnecessary\n" + 
		"	    ^^^^^^^^^^^^^^^^\n" + 
		"Unnecessary cast to type String for expression of type String\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}
public void test004() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	public static void main(String[] args) {\n" + 
			"		// constructors\n" + 
			"		new X((Object) \"hello\");	//   necessary\n" + 
			"		new X((String) \"hello\");	// UNnecessary\n" + 
			"		new X((Object) null);		//   necessary\n" + 
			"		new X((String) null);		// UNnecessary but keep as useful documentation\n" + 
			"	}\n" + 
			"	X(){}\n" + 
			"	X(String s){\n" + 
			"		System.out.println(\"new X(String):\"+s);\n" + 
			"	}\n" + 
			"	X(Object o){\n" + 
			"		System.out.println(\"new X(Object):\"+o);\n" + 
			"	}\n" + 
			"}\n"
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 5)\n" + 
		"	new X((String) \"hello\");	// UNnecessary\n" + 
		"	      ^^^^^^^^^^^^^^^^\n" + 
		"Unnecessary cast to type String for expression of type String\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}
public void test005() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	public static void main(String[] args) {\n" + 
			"		// qualified allocations\n" + 
			"		new X().new XM3((Object) \"hello\");	//   necessary\n" + 
			"		new X().new XM3((String) \"hello\");	// UNnecessary\n" + 
			"		new X().new XM3((Object) null);		//   necessary\n" + 
			"		new X().new XM3((String) null);		// UNnecessary but keep as useful documentation\n" + 
			"		new X().new XM3((Object) \"hello\"){};	//   necessary\n" + 
			"		new X().new XM3((String) \"hello\"){};	// UNnecessary\n" + 
			"		new X().new XM3((Object) null){};		//   necessary\n" + 
			"		new X().new XM3((String) null){};		// UNnecessary but keep as useful documentation\n" + 
			"	}\n" + 
			"	X(){}\n" + 
			"	static class XM1 extends X {}\n" + 
			"	static class XM2 extends X {}\n" + 
			"	class XM3 {\n" + 
			"		XM3(String s){\n" + 
			"			System.out.println(\"new XM3(String):\"+s);\n" + 
			"		}\n" + 
			"		XM3(Object o){\n" + 
			"			System.out.println(\"new XM3(Object):\"+o);\n" + 
			"		}\n" + 
			"	}	\n" + 
			"}\n"
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 5)\n" + 
		"	new X().new XM3((String) \"hello\");	// UNnecessary\n" + 
		"	                ^^^^^^^^^^^^^^^^\n" + 
		"Unnecessary cast to type String for expression of type String\n" + 
		"----------\n" + 
		"2. ERROR in X.java (at line 9)\n" + 
		"	new X().new XM3((String) \"hello\"){};	// UNnecessary\n" + 
		"	                ^^^^^^^^^^^^^^^^\n" + 
		"Unnecessary cast to type String for expression of type String\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}
public void _test006() { // TODO (philippe) add support to conditional expression for unnecessary cast
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	public static void main(String[] args) {\n" + 
			"		// ternary operator\n" + 
			"		String s = null, t = null;	\n" +
			"		X x0 = s == t\n" + 
			"			? (X)new XM1()			// UNnecessary\n" + 
			"			: new X();\n" + 
			"		X x1 = s == t \n" + 
			"			? (X)new XM1()			//   necessary\n" + 
			"			: new XM2();\n" + 
			"		X x2 = s == t \n" + 
			"			? new XM1()\n" + 
			"			: (X)new XM2();			//   necessary\n" + 
			"		X x3 = s == t \n" + 
			"			? (X)new XM1()			//   necessary\n" + 
			"			: (X)new XM2();			//   necessary\n" + 
			"	}\n" + 
			"	X(){}\n" + 
			"	static class XM1 extends X {}\n" + 
			"	static class XM2 extends X {}\n" + 
			"}\n"
		},
		"x",
		null,
		true,
		customOptions);
}

public void test007() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	X(){}\n" + 
			"	class XM3 {\n" + 
			"		XM3(String s){\n" + 
			"			System.out.println(\"new XM3(String):\"+s);\n" + 
			"		}\n" + 
			"		XM3(Object o){\n" + 
			"			System.out.println(\"new XM3(Object):\"+o);\n" + 
			"		}\n" + 
			"	}	\n" + 
			"	\n" + 
			"	class XM4 extends XM3 {\n" + 
			"		XM4(String s){\n" + 
			"			super((Object) s); // necessary\n" + 
			"			System.out.println(\"new XM4(String):\"+s);\n" + 
			"		}\n" + 
			"		XM4(Object o){\n" + 
			"			super((String) o); // necessary\n" + 
			"			System.out.println(\"new XM4(Object):\"+o);\n" + 
			"		}\n" + 
			"		XM4(Thread t){\n" + 
			"			super((Object) t); // UNnecessary\n" + 
			"			System.out.println(\"new XM4(Thread):\"+t);\n" + 
			"		}\n" + 
			"		XM4(){\n" + 
			"			super((String)null); // UNnecessary but keep as useful documentation\n" + 
			"			System.out.println(\"new XM4():\");\n" + 
			"		}\n" + 
			"		XM4(int i){\n" + 
			"			super((Object)null); // necessary\n" + 
			"			System.out.println(\"new XM4():\");\n" + 
			"		}\n" + 
			"	}\n" + 
			"}\n"
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 22)\n" + 
		"	super((Object) t); // UNnecessary\n" + 
		"	      ^^^^^^^^^^\n" + 
		"Unnecessary cast to type Object for expression of type Thread. It is already compatible with the argument type Object\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}

public void test008() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	public static void main(String[] args) {\n" + 
			"		boolean b1 = new XM1() instanceof X; // UNnecessary\n" + 
			"		boolean b2 = new X() instanceof XM1; // necessary\n" + 
			"		boolean b3 = null instanceof X; // UNnecessary\n" + 
			"	}\n" + 
			"	static class XM1 extends X {}\n" + 
			"}\n"
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 3)\n" + 
		"	boolean b1 = new XM1() instanceof X; // UNnecessary\n" + 
		"	             ^^^^^^^^^^^^^^^^^^^^^^\n" + 
		"The expression of type X.XM1 is already an instance of type X\n" + 
		"----------\n" + 
		"2. ERROR in X.java (at line 5)\n" + 
		"	boolean b3 = null instanceof X; // UNnecessary\n" + 
		"	             ^^^^^^^^^^^^^^^^^\n" + 
		"The expression of type null is already an instance of type X\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}

public void test009() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	public static void main(String[] args) {\n" + 
			"		boolean b1 = ((X) new XM1()) == new X(); // UNnecessary\n" + 
			"		boolean b2 = ((X) new XM1()) == new XM2(); // necessary\n" + 
			"		boolean b3 = ((X) null) == new X(); // UNnecessary\n" + 
			"	}\n" + 
			"	static class XM1 extends X {}\n" + 
			"	static class XM2 extends X {}\n" + 
			"}\n"
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 3)\n" + 
		"	boolean b1 = ((X) new XM1()) == new X(); // UNnecessary\n" + 
		"	             ^^^^^^^^^^^^^^^\n" + 
		"Unnecessary cast to type X for expression of type X.XM1\n" + 
		"----------\n" + 
		"2. ERROR in X.java (at line 5)\n" + 
		"	boolean b3 = ((X) null) == new X(); // UNnecessary\n" + 
		"	             ^^^^^^^^^^\n" + 
		"Unnecessary cast to type X for expression of type null\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}

public void test010() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	public static void main(String[] args) {\n" + 
			"		long l1 = ((long) 1) + 2L; // UNnecessary\n" + 
			"		long l2 = ((long)1) + 2; // necessary\n" + 
			"		long l3 = 0;" + 
			"		l3 += (long)12; // UNnecessary\n" + 
			"	}\n" + 
			"}\n"
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 3)\n" + 
		"	long l1 = ((long) 1) + 2L; // UNnecessary\n" + 
		"	          ^^^^^^^^^^\n" + 
		"Unnecessary cast to type long for expression of type int. It is already compatible with the argument type long\n" + 
		"----------\n" + 
		"2. ERROR in X.java (at line 5)\n" + 
		"	long l3 = 0;		l3 += (long)12; // UNnecessary\n" + 
		"	            		      ^^^^^^^^\n" + 
		"Unnecessary cast to type long for expression of type int\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}

public void test011() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	public static void main(String[] args) {\n" + 
			"		String s1 = ((long) 1) + \"hello\"; // necessary\n" + 
			"		String s2 = ((String)\"hello\") + 2; // UNnecessary\n" + 
			"		String s3 = ((String)null) + null; // necessary\n" + 
			"		String s4 = ((int) (byte)1) + \"hello\"; // necessary\n" + 
			"	}\n" + 
			"}\n"
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 4)\n" + 
		"	String s2 = ((String)\"hello\") + 2; // UNnecessary\n" + 
		"	            ^^^^^^^^^^^^^^^^^\n" + 
		"Unnecessary cast to type String for expression of type String\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}
public void test012() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	public static void main(String[] args) {\n" + 
			"		// message sends		\n" + 
			"		X x = new YM1();	\n" +
			"		foo((X) x);			// UNnecessary\n" + 
			"		foo((XM1) x);	// UNnecessary\n" + 
			"		foo((YM1) x);	// necessary \n" + 
			"	}\n" + 
			"	static void foo(X x) {}\n" + 
			"	static void foo(YM1 ym1) {}\n" + 
			"  static class XM1 extends X {}\n" +
			"  static class YM1 extends XM1 {}\n" +
			"}\n"
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 5)\n" + 
		"	foo((X) x);			// UNnecessary\n" + 
		"	    ^^^^^\n" + 
		"Unnecessary cast to type X for expression of type X\n" + 
		"----------\n" + 
		"2. ERROR in X.java (at line 6)\n" + 
		"	foo((XM1) x);	// UNnecessary\n" + 
		"	    ^^^^^^^\n" + 
		"Unnecessary cast to type X.XM1 for expression of type X. It is already compatible with the argument type X\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}

// https://bugs.eclipse.org/bugs/show_bug.cgi?id=42289
public void test013() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	\n" + 
			"	public static void main(String[] args) {\n" + 
			"		int a = 0, b = 1;\n" + 
			"		long d;\n" + 
			"		d = (long)a; 				// unnecessary\n" + 
			"		d = (long)a + b; 		// necessary \n" + 
			"		d = d + a + (long)b; 	// unnecessary\n" + 
			"	}\n" + 
			"}\n" + 
			"\n",
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 6)\n" + 
		"	d = (long)a; 				// unnecessary\n" + 
		"	    ^^^^^^^\n" + 
		"Unnecessary cast to type long for expression of type int\n" + 
		"----------\n" + 
		"2. ERROR in X.java (at line 8)\n" + 
		"	d = d + a + (long)b; 	// unnecessary\n" + 
		"	            ^^^^^^^\n" + 
		"Unnecessary cast to type long for expression of type int. It is already compatible with the argument type long\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}
// 39925 - Unnecessary instanceof checking leads to a NullPointerException
public void test014() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	boolean b = new Cloneable() {} instanceof Cloneable;\n" + 
			"}"
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 2)\n" + 
		"	boolean b = new Cloneable() {} instanceof Cloneable;\n" + 
		"	            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
		"The expression of type <anonymous implementation of Cloneable> is already an instance of type Cloneable\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}
// narrowing cast on base types may change value, thus necessary
public void test015() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	void foo() {	\n" +
			"    int lineCount = 10; \n" +
			"    long time = 1000; \n" +
			"    double linePerSeconds1 = ((int) (lineCount * 10000.0 / time)) / 10.0; // necessary \n" +
			"    double linePerSeconds2 = ((double) (lineCount * 10000.0 / time)) / 10.0; // UNnecessary \n" +
			"  } \n" +
			"}"
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 6)\n" + 
		"	double linePerSeconds2 = ((double) (lineCount * 10000.0 / time)) / 10.0; // UNnecessary \n" + 
		"	                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
		"Unnecessary cast to type double for expression of type double\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}
// narrowing cast on base types may change value, thus necessary
public void test016() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	void foo() {	\n" +
			"    int lineCount = 10; \n" +
			"    long time = 1000; \n" +
			"    print((int) (lineCount * 10000.0 / time)); // necessary \n" +
			"    print((double) (lineCount * 10000.0 / time)); // UNnecessary \n" +
			"  } \n" +
			"  void print(double d) {}  \n" +
			"}"
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 6)\n" + 
		"	print((double) (lineCount * 10000.0 / time)); // UNnecessary \n" + 
		"	      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
		"Unnecessary cast to type double for expression of type double\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}
//fault tolerance (40288)
public void test017() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	void bar() {\n" + 
			"		foo((X) this);\n" + 
			"		foo((X) zork());\n" + // unbound #zork() should not cause NPE
			"	}\n" + 
			"	void foo(X x) {\n" + 
			"	}\n" + 
			"}\n"
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 3)\n" + 
		"	foo((X) this);\n" + 
		"	    ^^^^^^^^\n" + 
		"Unnecessary cast to type X for expression of type X\n" + 
		"----------\n" + 
		"2. ERROR in X.java (at line 4)\n" + 
		"	foo((X) zork());\n" + 
		"	        ^^^^\n" + 
		"The method zork() is undefined for the type X\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}
//fault tolerance (40423)
public void test018() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"class Y {\n" +
			"	static Y[] foo(int[] tab) {\n" +
			"		return null;\n" +
			"	}\n" +
			"}\n" +
			"public class X extends Y {\n" + 
			"	Y[] bar() {\n" +
			"		return (Y[]) Y.foo(new double[] {});\n" + 
			"	}\n" +
			"}\n"
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 8)\n" + 
		"	return (Y[]) Y.foo(new double[] {});\n" + 
		"	       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
		"Unnecessary cast to type Y[] for expression of type Y[]\n" + 
		"----------\n" + 
		"2. ERROR in X.java (at line 8)\n" + 
		"	return (Y[]) Y.foo(new double[] {});\n" + 
		"	               ^^^\n" + 
		"The method foo(int[]) in the type Y is not applicable for the arguments (double[])\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}
//fault tolerance (40288)
public void tes019() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	void bar() {\n" + 
			"		X x1 =(X) this;\n" + 
			"		X x2 = (X) zork();\n" + // unbound #zork() should not cause NPE
			"	}\n" + 
			"}\n"
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 3)\n" + 
		"	X x1 =(X) this;\n" + 
		"	      ^^^^^^^^\n" + 
		"Unnecessary cast to type X for expression of type X\n" + 
		"----------\n" + 
		"2. ERROR in X.java (at line 4)\n" + 
		"	X x2 = (X) zork();\n" + 
		"	           ^^^^\n" + 
		"The method zork() is undefined for the type X\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}
//fault tolerance 
public void test020() {
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	void bar() {\n" +
			"		long l = (long)zork() + 2;\n" + 
			"	}\n" +
			"}\n"
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 3)\n" + 
		"	long l = (long)zork() + 2;\n" + 
		"	               ^^^^\n" + 
		"The method zork() is undefined for the type X\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}

// unnecessary cast diagnosis should also consider receiver type (40572)
public void test021() { 
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"p1/A.java",
			"package p1;\n" + 
			"public class A {\n" + 
			"	public class Member1 {}\n" + 
			"	public class Member2 {}\n" + 
			"	class Member3 {}\n" + 
			"   public static class Member4 {\n" +
			"	   public static class M4Member {}\n" + 
			"   }\n" +
			"}\n",
			"p2/B.java",
			"package p2;\n" + 
			"import p1.A;\n" +
			"public class B extends A {\n" + 
			"	public class Member1 {}\n" + 
			"}\n",
			"p1/C.java",
			"package p1;\n" + 
			"import p2.B;\n" +
			"public class C extends B {\n" + 
			"	void baz(B b) {\n" + 
			"		((A)b).new Member1(); // necessary since would bind to B.Member instead\n" +
			"		((A)b).new Member2(); // UNnecessary\n" +
			"		((A)b).new Member3(); // necessary since visibility issue\n" +
			"		((A)b).new Member4().new M4Member(); // fault tolerance\n" +
			"		((A)zork()).new Member1(); // fault-tolerance\n" +
			"		// anonymous\n"+
			"		((A)b).new Member1(){}; // necessary since would bind to B.Member instead\n" +
			"		((A)b).new Member2(){}; // UNnecessary\n" +
			"		((A)b).new Member3(){}; // necessary since visibility issue\n" +
			"		((A)b).new Member4().new M4Member(){}; // fault tolerance\n" +
			"		((A)zork()).new Member1(){}; // fault-tolerance\n" +
			"	}\n" + 
			"}\n",
		},
		"----------\n" + 
		"1. ERROR in p1\\C.java (at line 6)\n" + 
		"	((A)b).new Member2(); // UNnecessary\n" + 
		"	^^^^^^\n" + 
		"Unnecessary cast to type A for expression of type B\n" + 
		"----------\n" + 
		"2. ERROR in p1\\C.java (at line 8)\n" + 
		"	((A)b).new Member4().new M4Member(); // fault tolerance\n" + 
		"	^^^^^^\n" + 
		"Unnecessary cast to type A for expression of type B\n" + 
		"----------\n" + 
		"3. ERROR in p1\\C.java (at line 9)\n" + 
		"	((A)zork()).new Member1(); // fault-tolerance\n" + 
		"	    ^^^^\n" + 
		"The method zork() is undefined for the type C\n" + 
		"----------\n" + 
		"4. ERROR in p1\\C.java (at line 12)\n" + 
		"	((A)b).new Member2(){}; // UNnecessary\n" + 
		"	^^^^^^\n" + 
		"Unnecessary cast to type A for expression of type B\n" + 
		"----------\n" + 
		"5. ERROR in p1\\C.java (at line 14)\n" + 
		"	((A)b).new Member4().new M4Member(){}; // fault tolerance\n" + 
		"	^^^^^^\n" + 
		"Unnecessary cast to type A for expression of type B\n" + 
		"----------\n" + 
		"6. ERROR in p1\\C.java (at line 15)\n" + 
		"	((A)zork()).new Member1(){}; // fault-tolerance\n" + 
		"	    ^^^^\n" + 
		"The method zork() is undefined for the type C\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}
// unnecessary cast diagnosis should tolerate array receiver type (40752)
public void test022() { 
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {	\n" +
			"  void foo(java.util.Map map){ \n" +
			"    int[] fillPattern = new int[0]; \n" +
			"    if (fillPattern.equals((int[])map.get(\"x\"))) { \n" +
			"    }  \n" +
			"  } \n"+
			"} \n",
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 4)\n" + 
		"	if (fillPattern.equals((int[])map.get(\"x\"))) { \n" + 
		"	                       ^^^^^^^^^^^^^^^^^^^\n" + 
		"Unnecessary cast to type int[] for expression of type Object. It is already compatible with the argument type Object\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}
		
// unnecessary cast diagnosis should tolerate array receiver type (40752)
public void test023() { 
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"\n" + 
			"	public static void main(String[] args) {\n" + 
			"		final long lgLow32BitMask1 = ~(~((long) 0) << 32);		// necessary\n" + 
			"		final long lgLow32BitMask2 = ~(~0 << 32);					// necessary\n" + 
			"		final long lgLow32BitMask3 = ~(~((long) 0L) << 32);	// unnecessary\n" + 
			"		final long lgLow32BitMask4 = ~(~((int) 0L) << 32);		// necessary\n" + 
			"		System.out.println(\"lgLow32BitMask1: \"+lgLow32BitMask1);\n" + 
			"		System.out.println(\"lgLow32BitMask2: \"+lgLow32BitMask2);\n" + 
			"	}\n" + 
			"}",
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 6)\n" + 
		"	final long lgLow32BitMask3 = ~(~((long) 0L) << 32);	// unnecessary\n" + 
		"	                                ^^^^^^^^^^^\n" + 
		"Unnecessary cast to type long for expression of type long\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}		

// unnecessary cast diagnosis for message receiver (44400)
public void test024() { 
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	public void foo(Object bar) {\n" + 
			"		System.out.println(((Object) bar).toString());\n" + 
			"	}\n" + 
			"}",
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 3)\n" + 
		"	System.out.println(((Object) bar).toString());\n" + 
		"	                   ^^^^^^^^^^^^^^\n" + 
		"Unnecessary cast to type Object for expression of type Object\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}		

// unnecessary cast diagnosis for message receiver (44400)
// variation with field access
public void test025() { 
	Map customOptions = getCompilerOptions();
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"	int i;\n" + 
			"	public void foo(X bar) {\n" + 
			"		System.out.println(((X) bar).i);\n" + 
			"	}\n" + 
			"}",
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 4)\n" + 
		"	System.out.println(((X) bar).i);\n" + 
		"	                   ^^^^^^^^^\n" + 
		"Unnecessary cast to type X for expression of type X\n" + 
		"----------\n",
		null,
		true,
		customOptions);
}	
/*
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=47074
 */
public void test026() { 
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"  public static void main(String[] args) {\n" + 
			"    A a = null;\n" + 
			"    B b = (B) a;\n" + 
			"  }\n" + 
			"}\n" + 
			"interface A {\n" + 
			"  void doSomething();\n" + 
			"}\n" + 
			"interface B {\n" + 
			"  int doSomething();\n" + 
			"}",
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 4)\n" + 
		"	B b = (B) a;\n" + 
		"	      ^^^^^\n" + 
		"Cannot cast from A to B\n" + 
		"----------\n",
		null,
		true);
}
/*
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=47074
 */
public void test027() { 
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"  public static void main(String[] args) {\n" + 
			"    A a = null;\n" + 
			"    boolean b = a instanceof B;\n" + 
			"  }\n" + 
			"}\n" + 
			"interface A {\n" + 
			"  void doSomething();\n" + 
			"}\n" + 
			"interface B {\n" + 
			"  int doSomething();\n" + 
			"}",
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 4)\n" + 
		"	boolean b = a instanceof B;\n" + 
		"	            ^^^^^^^^^^^^^^\n" + 
		"Incompatible conditional operand types A and B\n" + 
		"----------\n",
		null,
		true);
}
/*
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=47074
 */
public void test028() { 
	this.runNegativeTest(
		new String[] {
			"X.java",
			"public class X {\n" + 
			"  public static void main(String[] args) {\n" + 
			"    A a = null;\n" + 
			"    B b = null;\n" + 
			"    boolean c = a == b;\n" + 
			"  }\n" + 
			"}\n" + 
			"interface A {\n" + 
			"  void doSomething();\n" + 
			"}\n" + 
			"interface B {\n" + 
			"  int doSomething();\n" + 
			"}",
		},
		"----------\n" + 
		"1. ERROR in X.java (at line 5)\n" + 
		"	boolean c = a == b;\n" + 
		"	            ^^^^^^\n" + 
		"Incompatible operand types A and B\n" + 
		"----------\n",
		null,
		true);
}

/*
 * verify error when assigning null to array
 * http://bugs.eclipse.org/bugs/show_bug.cgi?id=26903
 */
public void test029() {
	this.runConformTest(
		new String[] {
			"X.java",
			"public class X {	\n" +
			"    public static void main(String[] args) {	\n" +
			"		try {	\n" +
			"		    char[][] qName;	\n" +
			"			qName = null;	\n" +
			"			qName[0] = new char[1];	\n" +
			"		} catch(Exception e){	\n" +
			"		}	\n" +
			"		try {	\n" +
			"		    char[][] qName;	\n" +
			"			qName = (char[][])null;	\n" +
			"			qName[0] = new char[1];	\n" +
			"		} catch(Exception e){	\n" +
			"		}	\n" +
			"		try {	\n" +
			"		    char[][] qName;	\n" +
			"			qName = (char[][])(char[][])(char[][])null;	\n" +
			"			qName[0] = new char[2];	\n" +
			"		} catch(Exception e){	\n" +
			"		}	\n" +
			"		try {	\n" +
			"		    char[][] qName;	\n" +
			"			qName = args.length > 1 ? new char[1][2] : null;	\n" +
			"			qName[0] = new char[3];	\n" +
			"		} catch(Exception e){	\n" +
			"		}	\n" +
			"		System.out.println(\"SUCCESS\");	\n"+
			"	}	\n" +
			"}	\n",
		},
	"SUCCESS");
}

/*
 * verify error when assigning null to array
 * http://bugs.eclipse.org/bugs/show_bug.cgi?id=26903
 */
public void test030() {
	this.runConformTest(
		new String[] {
			"X.java",
			"public class X {	\n" +
			"    public static void main(String[] args) {	\n" +
			"		try {	\n" +
			"			char[][] qName = null;	\n" +
			"			qName[0] = new char[1];	\n" +
			"		} catch(Exception e){	\n" +
			"		}	\n" +
			"		try {	\n" +
			"			char[][] qName = (char[][])null;	\n" +
			"			qName[0] = new char[1];	\n" +
			"		} catch(Exception e){	\n" +
			"		}	\n" +
			"		try {	\n" +
			"			char[][] qName = (char[][])(char[][])(char[][])null;	\n" +
			"			qName[0] = new char[2];	\n" +
			"		} catch(Exception e){	\n" +
			"		}	\n" +
			"		try {	\n" +
			"			char[][] qName = args.length > 1 ? new char[1][2] : null;	\n" +
			"			qName[0] = new char[3];	\n" +
			"		} catch(Exception e){	\n" +
			"		}	\n" +
			"		System.out.println(\"SUCCESS\");	\n"+
			"	}	\n" +
			"}	\n",
		},
	"SUCCESS");
}

/*
 * verify error when assigning null to array
 * http://bugs.eclipse.org/bugs/show_bug.cgi?id=26903
 */
public void test031() {
	this.runConformTest(
		new String[] {
			"X.java",
			"public class X {	\n" +
			"    public static void main(String[] args) {	\n" +
			"		try {	\n" +
			"			char[][] qName = null;	\n" +
			"			setName(qName[0]);	\n" +
			"		} catch(Exception e){	\n" +
			"		}	\n" +
			"		try {	\n" +
			"			char[][] qName = (char[][])null;	\n" +
			"			setName(qName[0]);	\n" +
			"		} catch(Exception e){	\n" +
			"		}	\n" +
			"		try {	\n" +
			"			char[][] qName = (char[][])(char[][])(char[][])null;	\n" +
			"			setName(qName[0]);	\n" +
			"		} catch(Exception e){	\n" +
			"		}	\n" +
			"		try {	\n" +
			"			char[][] qName = args.length > 1 ? new char[1][2] : null;	\n" +
			"			setName(qName[0]);	\n" +
			"		} catch(Exception e){	\n" +
			"		}	\n" +
			"		System.out.println(\"SUCCESS\");	\n"+
			"	}	\n" +
			"	static void setName(char[] name) {	\n"+
			"	}	\n" +
			"}	\n",
		},
	"SUCCESS");
}
/*
 * verify error when assigning null to array
 * http://bugs.eclipse.org/bugs/show_bug.cgi?id=26903
 */
public void test032() {
	this.runConformTest(
		new String[] {
			"X.java",
			"public class X {\n" +
			"    public static void main(String[] args) {\n" +
			"			try {\n" +
			"				((int[]) null)[0] = 0;\n" +
			"				((int[]) null)[0] += 1;\n" +
			"				((int[]) null)[0] ++;\n" +
			"			} catch (NullPointerException e) {\n" +
			"				System.out.print(\"SUCCESS\");\n" +
			"			}\n" +
			"	}\n" +
			"}\n",
		},
	"SUCCESS");
}
public static Class testClass() {
	return CastTest.class;
}
}

Back to the top