View | Details | Raw Unified | Return to bug 185787 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java (-44 / +45 lines)
Lines 1757-1763 Link Here
1757
	}
1757
	}
1758
	// raw type: assignments 
1758
	// raw type: assignments 
1759
	public void test0065() {
1759
	public void test0065() {
1760
		Map customOptions = getCompilerOptions();
1761
		this.runNegativeTest(
1760
		this.runNegativeTest(
1762
			new String[] {
1761
			new String[] {
1763
				"X.java",
1762
				"X.java",
Lines 1794-1803 Link Here
1794
			"	X<IOException> xioe2 = x; // unsafe\n" + 
1793
			"	X<IOException> xioe2 = x; // unsafe\n" + 
1795
			"	                       ^\n" + 
1794
			"	                       ^\n" + 
1796
			"Type safety: The expression of type X needs unchecked conversion to conform to X<IOException>\n" + 
1795
			"Type safety: The expression of type X needs unchecked conversion to conform to X<IOException>\n" + 
1797
			"----------\n",
1796
			"----------\n");
1798
			null,
1799
			true,
1800
			customOptions);
1801
	}
1797
	}
1802
1798
1803
	// JSR14-v10[2.1,2.2]: Invalid PT declaration (mix with reference)
1799
	// JSR14-v10[2.1,2.2]: Invalid PT declaration (mix with reference)
Lines 2543-2549 Link Here
2543
	}		
2539
	}		
2544
2540
2545
	public void test0086() {
2541
	public void test0086() {
2546
		Map customOptions = getCompilerOptions();
2547
		this.runNegativeTest(
2542
		this.runNegativeTest(
2548
			new String[] {
2543
			new String[] {
2549
				"X.java",
2544
				"X.java",
Lines 2581-2592 Link Here
2581
			"	ax.p = new AX<String>();\n" + 
2576
			"	ax.p = new AX<String>();\n" + 
2582
			"	   ^\n" + 
2577
			"	   ^\n" + 
2583
			"Type safety: The field p from the raw type AX is assigned a value of type AX<String>. References to generic type AX<P> should be parameterized\n" + 
2578
			"Type safety: The field p from the raw type AX is assigned a value of type AX<String>. References to generic type AX<P> should be parameterized\n" + 
2584
			"----------\n",
2579
			"----------\n");
2585
		null,
2580
	}
2586
		true,
2581
2587
		customOptions);		
2588
	}		
2589
	
2590
	public void test0087() {
2582
	public void test0087() {
2591
		Map customOptions = getCompilerOptions();
2583
		Map customOptions = getCompilerOptions();
2592
		// check no unsafe type operation problem is issued
2584
		// check no unsafe type operation problem is issued
Lines 3259-3265 Link Here
3259
	}			
3251
	}			
3260
	// unsafe assignment thru binaries
3252
	// unsafe assignment thru binaries
3261
	public void test0107() {
3253
	public void test0107() {
3262
		Map customOptions = getCompilerOptions();
3263
		this.runNegativeTest(
3254
		this.runNegativeTest(
3264
			new String[] {
3255
			new String[] {
3265
				"X.java",
3256
				"X.java",
Lines 3285-3294 Link Here
3285
			"	Iterable<String> is = new ArrayList();\n" + 
3276
			"	Iterable<String> is = new ArrayList();\n" + 
3286
			"	                          ^^^^^^^^^\n" + 
3277
			"	                          ^^^^^^^^^\n" + 
3287
			"ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized\n" + 
3278
			"ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized\n" + 
3288
			"----------\n",
3279
			"----------\n");
3289
			null,
3290
			true,
3291
			customOptions);		
3292
	}			
3280
	}			
3293
	// class literal: Integer.class of type Class<Integer>
3281
	// class literal: Integer.class of type Class<Integer>
3294
	public void test0108() {
3282
	public void test0108() {
Lines 5198-5204 Link Here
5198
	}
5186
	}
5199
	// unsafe raw return value
5187
	// unsafe raw return value
5200
	public void test0176() {
5188
	public void test0176() {
5201
		Map customOptions = getCompilerOptions();
5202
		this.runNegativeTest(
5189
		this.runNegativeTest(
5203
			new String[] {
5190
			new String[] {
5204
				"X.java",
5191
				"X.java",
Lines 5230-5243 Link Here
5230
			"	Vector<Object> v = (Vector<Object>) data.elementAt(0);\n" + 
5217
			"	Vector<Object> v = (Vector<Object>) data.elementAt(0);\n" + 
5231
			"	                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
5218
			"	                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
5232
			"Type safety: Unchecked cast from Object to Vector<Object>\n" + 
5219
			"Type safety: Unchecked cast from Object to Vector<Object>\n" + 
5233
			"----------\n",
5220
			"----------\n");
5234
			null,
5235
			true,
5236
			customOptions);
5237
	}
5221
	}
5238
	// cast to type variable allowed, can be diagnosed as unnecessary
5222
	// cast to type variable allowed, can be diagnosed as unnecessary
5239
	public void test0177() {
5223
	public void test0177() {
5240
		Map customOptions = getCompilerOptions();
5241
		this.runNegativeTest(
5224
		this.runNegativeTest(
5242
			new String[] {
5225
			new String[] {
5243
				"X.java",
5226
				"X.java",
Lines 5253-5266 Link Here
5253
			"	return (T) t;\n" + 
5236
			"	return (T) t;\n" + 
5254
			"	       ^^^^^\n" + 
5237
			"	       ^^^^^\n" + 
5255
			"Unnecessary cast from T to T\n" + 
5238
			"Unnecessary cast from T to T\n" + 
5256
			"----------\n",
5239
			"----------\n");
5257
			null,
5258
			true,
5259
			customOptions);
5260
	}
5240
	}
5261
	// reject instanceof type variable or parameterized type
5241
	// reject instanceof type variable or parameterized type
5262
	public void test0178() {
5242
	public void test0178() {
5263
		Map customOptions = getCompilerOptions();
5264
		this.runNegativeTest(
5243
		this.runNegativeTest(
5265
			new String[] {
5244
			new String[] {
5266
				"X.java",
5245
				"X.java",
Lines 5297-5306 Link Here
5297
			"	} else 	if (t instanceof T) {\n" + 
5276
			"	} else 	if (t instanceof T) {\n" + 
5298
			"	       	    ^^^^^^^^^^^^^^\n" + 
5277
			"	       	    ^^^^^^^^^^^^^^\n" + 
5299
			"Cannot perform instanceof check against type parameter T. Use instead its erasure Object since generic type information will be erased at runtime\n" + 
5278
			"Cannot perform instanceof check against type parameter T. Use instead its erasure Object since generic type information will be erased at runtime\n" + 
5300
			"----------\n",
5279
			"----------\n");
5301
			null,
5302
			true,
5303
			customOptions);
5304
	}
5280
	}
5305
	// 61507
5281
	// 61507
5306
	public void test0179() {
5282
	public void test0179() {
Lines 6309-6315 Link Here
6309
	}
6285
	}
6310
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=69135 - unnecessary cast operation
6286
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=69135 - unnecessary cast operation
6311
	public void test0217() {
6287
	public void test0217() {
6312
		Map customOptions = getCompilerOptions();
6313
		this.runNegativeTest(
6288
		this.runNegativeTest(
6314
			new String[] {
6289
			new String[] {
6315
				"X.java",
6290
				"X.java",
Lines 6326-6335 Link Here
6326
			"	String string = (String) l.get(0);\n" + 
6301
			"	String string = (String) l.get(0);\n" + 
6327
			"	                ^^^^^^^^^^^^^^^^^\n" + 
6302
			"	                ^^^^^^^^^^^^^^^^^\n" + 
6328
			"Unnecessary cast from String to String\n" + 
6303
			"Unnecessary cast from String to String\n" + 
6329
			"----------\n",
6304
			"----------\n");
6330
			null,
6331
			true,
6332
			customOptions);
6333
	}
6305
	}
6334
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=64154 visibility issue due to invalid use of parameterized binding
6306
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=64154 visibility issue due to invalid use of parameterized binding
6335
	public void test0218() {
6307
	public void test0218() {
Lines 6448-6454 Link Here
6448
	
6420
	
6449
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=69251- instantiating wildcards
6421
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=69251- instantiating wildcards
6450
	public void test0223() {
6422
	public void test0223() {
6451
		Map customOptions = getCompilerOptions();
6452
		this.runNegativeTest(
6423
		this.runNegativeTest(
6453
			new String[] {
6424
			new String[] {
6454
				"X.java",
6425
				"X.java",
Lines 6498-6507 Link Here
6498
			"	mx1.foo(mx2.get());\n" + 
6469
			"	mx1.foo(mx2.get());\n" + 
6499
			"	        ^^^^^^^^^\n" + 
6470
			"	        ^^^^^^^^^\n" + 
6500
			"Type safety: The expression of type Class needs unchecked conversion to conform to Class<? extends Object>\n" + 
6471
			"Type safety: The expression of type Class needs unchecked conversion to conform to Class<? extends Object>\n" + 
6501
			"----------\n",
6472
			"----------\n");
6502
			null,
6503
			true,
6504
			customOptions);
6505
	}
6473
	}
6506
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=68998 variation
6474
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=68998 variation
6507
	public void test0224() { 
6475
	public void test0224() { 
Lines 37896-37910 Link Here
37896
			"}\n", // =================
37864
			"}\n", // =================
37897
		},
37865
		},
37898
		"----------\n" + 
37866
		"----------\n" + 
37899
		"1. ERROR in X.java (at line 20)\n" + 
37867
		"1. WARNING in X.java (at line 19)\n" + 
37868
		"	Collection<?> result2 = (Collection<Container>)foo();\n" + 
37869
		"	                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
37870
		"Unnecessary cast from Collection<Container> to Collection<Container>\n" + 
37871
		"----------\n" + 
37872
		"2. ERROR in X.java (at line 20)\n" + 
37900
		"	String result3 = foo();\n" + 
37873
		"	String result3 = foo();\n" + 
37901
		"	                 ^^^^^\n" + 
37874
		"	                 ^^^^^\n" + 
37902
		"Type mismatch: cannot convert from Collection<Container> to String\n" + 
37875
		"Type mismatch: cannot convert from Collection<Container> to String\n" + 
37903
		"----------\n" + 
37876
		"----------\n" + 
37904
		"2. ERROR in X.java (at line 21)\n" + 
37877
		"3. ERROR in X.java (at line 21)\n" + 
37905
		"	String result4 = (String) foo();		\n" + 
37878
		"	String result4 = (String) foo();		\n" + 
37906
		"	                 ^^^^^^^^^^^^^^\n" + 
37879
		"	                 ^^^^^^^^^^^^^^\n" + 
37907
		"Cannot cast from Collection<Container> to String\n" + 
37880
		"Cannot cast from Collection<Container> to String\n" + 
37908
		"----------\n");
37881
		"----------\n");
37909
}
37882
}
37883
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=185787
37884
public void test1138() {
37885
	Map customOptions = getCompilerOptions();
37886
	customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
37887
	this.runNegativeTest(
37888
		new String[] {
37889
			"X.java",
37890
			"import java.awt.Container;\n" + 
37891
			"import java.util.Collection;\n" + 
37892
			"public class X {\n" + 
37893
			"        private <T extends Container> Collection<T> foo() {\n" + 
37894
			"                return null;\n" + 
37895
			"        }\n" + 
37896
			"        private void showProblem() {\n" + 
37897
			"                Collection<?> result = (Collection<Container>)foo(); //unnecessary cast\n" + 
37898
			"        }\n" + 
37899
			"}", 
37900
		},
37901
		"----------\n" + 
37902
		"1. ERROR in X.java (at line 8)\n" + 
37903
		"	Collection<?> result = (Collection<Container>)foo(); //unnecessary cast\n" + 
37904
		"	                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
37905
		"Unnecessary cast from Collection<Container> to Collection<Container>\n" + 
37906
		"----------\n",
37907
		null,
37908
		true,
37909
		customOptions);
37910
}
37910
}
37911
}
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/ParameterizedGenericMethodBinding.java (-7 / +13 lines)
Lines 60-75 Link Here
60
			
60
			
61
			// 15.12.2.8 - inferring unresolved type arguments
61
			// 15.12.2.8 - inferring unresolved type arguments
62
			if (inferenceContext.hasUnresolvedTypeArgument()) {
62
			if (inferenceContext.hasUnresolvedTypeArgument()) {
63
				TypeBinding expectedType = null;
64
				if (methodSubstitute.returnType != TypeBinding.VOID) {
63
				if (methodSubstitute.returnType != TypeBinding.VOID) {
64
					TypeBinding expectedType = null;
65
					// if message invocation has expected type
65
					// if message invocation has expected type
66
					if (invocationSite instanceof MessageSend) {
66
					if (invocationSite instanceof MessageSend) {
67
						MessageSend message = (MessageSend) invocationSite;
67
						MessageSend message = (MessageSend) invocationSite;
68
						expectedType = message.expectedType;
68
						expectedType = message.expectedType;
69
					}
69
					}
70
					if (expectedType == null) expectedType = scope.getJavaLangObject(); // assume Object by default
70
					if (expectedType != null) {
71
						// record it was explicit from context, as opposed to assumed by default (see below)
72
						inferenceContext.hasExplicitExpectedType = true; 
73
					} else {
74
						expectedType = scope.getJavaLangObject(); // assume Object by default
75
					}
76
					inferenceContext.expectedType = expectedType;
71
				}
77
				}
72
				methodSubstitute = methodSubstitute.inferFromExpectedType(scope, expectedType, inferenceContext);
78
				methodSubstitute = methodSubstitute.inferFromExpectedType(scope, inferenceContext);
73
				if (methodSubstitute == null) 
79
				if (methodSubstitute == null) 
74
					return null;
80
					return null;
75
			}
81
			}
Lines 356-369 Link Here
356
	 * Given some type expectation, and type variable bounds, perform some inference.
362
	 * Given some type expectation, and type variable bounds, perform some inference.
357
	 * Returns true if still had unresolved type variable at the end of the operation
363
	 * Returns true if still had unresolved type variable at the end of the operation
358
	 */
364
	 */
359
	private ParameterizedGenericMethodBinding inferFromExpectedType(Scope scope, TypeBinding expectedType, InferenceContext inferenceContext) {
365
	private ParameterizedGenericMethodBinding inferFromExpectedType(Scope scope, InferenceContext inferenceContext) {
360
	    TypeVariableBinding[] originalVariables = this.originalMethod.typeVariables; // immediate parent (could be a parameterized method)
366
	    TypeVariableBinding[] originalVariables = this.originalMethod.typeVariables; // immediate parent (could be a parameterized method)
361
		int varLength = originalVariables.length;
367
		int varLength = originalVariables.length;
362
		
368
		
363
		computeSubstitutes: {
369
		computeSubstitutes: {
364
		    // infer from expected return type
370
		    // infer from expected return type
365
			if (expectedType != null) {
371
			if (inferenceContext.expectedType != null) {
366
			    this.returnType.collectSubstitutes(scope, expectedType, inferenceContext, TypeConstants.CONSTRAINT_SUPER);
372
			    this.returnType.collectSubstitutes(scope, inferenceContext.expectedType, inferenceContext, TypeConstants.CONSTRAINT_SUPER);
367
			    if (inferenceContext.status == InferenceContext.FAILED) return null; // impossible substitution
373
			    if (inferenceContext.status == InferenceContext.FAILED) return null; // impossible substitution
368
			}
374
			}
369
		    // infer from bounds of type parameters
375
		    // infer from bounds of type parameters
Lines 422-428 Link Here
422
		// adjust method types to reflect latest inference
428
		// adjust method types to reflect latest inference
423
		TypeBinding oldReturnType = this.returnType;
429
		TypeBinding oldReturnType = this.returnType;
424
		this.returnType = Scope.substitute(this, this.returnType);
430
		this.returnType = Scope.substitute(this, this.returnType);
425
		this.inferredReturnType = this.returnType != oldReturnType;
431
		this.inferredReturnType = inferenceContext.hasExplicitExpectedType && this.returnType != oldReturnType;
426
	    this.parameters = Scope.substitute(this, this.parameters);
432
	    this.parameters = Scope.substitute(this, this.parameters);
427
	    this.thrownExceptions = Scope.substitute(this, this.thrownExceptions);
433
	    this.thrownExceptions = Scope.substitute(this, this.thrownExceptions);
428
	    return this;
434
	    return this;
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/InferenceContext.java (+2 lines)
Lines 19-24 Link Here
19
	MethodBinding genericMethod;
19
	MethodBinding genericMethod;
20
	int depth;
20
	int depth;
21
	int status;
21
	int status;
22
	TypeBinding expectedType;
23
	boolean hasExplicitExpectedType; // indicates whether the expectedType (if set) was explicit in code, or set by default
22
	TypeBinding[] substitutes;
24
	TypeBinding[] substitutes;
23
	final static int FAILED = 1;
25
	final static int FAILED = 1;
24
	final static int RAW_SUBSTITUTION = 2;
26
	final static int RAW_SUBSTITUTION = 2;

Return to bug 185787