### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core.tests.compiler Index: src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java,v retrieving revision 1.625 diff -u -r1.625 GenericTypeTest.java --- src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java 7 May 2007 15:59:14 -0000 1.625 +++ src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java 7 May 2007 18:00:46 -0000 @@ -1757,7 +1757,6 @@ } // raw type: assignments public void test0065() { - Map customOptions = getCompilerOptions(); this.runNegativeTest( new String[] { "X.java", @@ -1794,10 +1793,7 @@ " X xioe2 = x; // unsafe\n" + " ^\n" + "Type safety: The expression of type X needs unchecked conversion to conform to X\n" + - "----------\n", - null, - true, - customOptions); + "----------\n"); } // JSR14-v10[2.1,2.2]: Invalid PT declaration (mix with reference) @@ -2543,7 +2539,6 @@ } public void test0086() { - Map customOptions = getCompilerOptions(); this.runNegativeTest( new String[] { "X.java", @@ -2581,12 +2576,9 @@ " ax.p = new AX();\n" + " ^\n" + "Type safety: The field p from the raw type AX is assigned a value of type AX. References to generic type AX

should be parameterized\n" + - "----------\n", - null, - true, - customOptions); - } - + "----------\n"); + } + public void test0087() { Map customOptions = getCompilerOptions(); // check no unsafe type operation problem is issued @@ -3259,7 +3251,6 @@ } // unsafe assignment thru binaries public void test0107() { - Map customOptions = getCompilerOptions(); this.runNegativeTest( new String[] { "X.java", @@ -3285,10 +3276,7 @@ " Iterable is = new ArrayList();\n" + " ^^^^^^^^^\n" + "ArrayList is a raw type. References to generic type ArrayList should be parameterized\n" + - "----------\n", - null, - true, - customOptions); + "----------\n"); } // class literal: Integer.class of type Class public void test0108() { @@ -5198,7 +5186,6 @@ } // unsafe raw return value public void test0176() { - Map customOptions = getCompilerOptions(); this.runNegativeTest( new String[] { "X.java", @@ -5230,14 +5217,10 @@ " Vector v = (Vector) data.elementAt(0);\n" + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + "Type safety: Unchecked cast from Object to Vector\n" + - "----------\n", - null, - true, - customOptions); + "----------\n"); } // cast to type variable allowed, can be diagnosed as unnecessary public void test0177() { - Map customOptions = getCompilerOptions(); this.runNegativeTest( new String[] { "X.java", @@ -5253,14 +5236,10 @@ " return (T) t;\n" + " ^^^^^\n" + "Unnecessary cast from T to T\n" + - "----------\n", - null, - true, - customOptions); + "----------\n"); } // reject instanceof type variable or parameterized type public void test0178() { - Map customOptions = getCompilerOptions(); this.runNegativeTest( new String[] { "X.java", @@ -5297,10 +5276,7 @@ " } else if (t instanceof T) {\n" + " ^^^^^^^^^^^^^^\n" + "Cannot perform instanceof check against type parameter T. Use instead its erasure Object since generic type information will be erased at runtime\n" + - "----------\n", - null, - true, - customOptions); + "----------\n"); } // 61507 public void test0179() { @@ -6309,7 +6285,6 @@ } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=69135 - unnecessary cast operation public void test0217() { - Map customOptions = getCompilerOptions(); this.runNegativeTest( new String[] { "X.java", @@ -6326,10 +6301,7 @@ " String string = (String) l.get(0);\n" + " ^^^^^^^^^^^^^^^^^\n" + "Unnecessary cast from String to String\n" + - "----------\n", - null, - true, - customOptions); + "----------\n"); } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=64154 visibility issue due to invalid use of parameterized binding public void test0218() { @@ -6448,7 +6420,6 @@ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=69251- instantiating wildcards public void test0223() { - Map customOptions = getCompilerOptions(); this.runNegativeTest( new String[] { "X.java", @@ -6498,10 +6469,7 @@ " mx1.foo(mx2.get());\n" + " ^^^^^^^^^\n" + "Type safety: The expression of type Class needs unchecked conversion to conform to Class\n" + - "----------\n", - null, - true, - customOptions); + "----------\n"); } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=68998 variation public void test0224() { @@ -37896,15 +37864,48 @@ "}\n", // ================= }, "----------\n" + - "1. ERROR in X.java (at line 20)\n" + + "1. WARNING in X.java (at line 19)\n" + + " Collection result2 = (Collection)foo();\n" + + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + + "Unnecessary cast from Collection to Collection\n" + + "----------\n" + + "2. ERROR in X.java (at line 20)\n" + " String result3 = foo();\n" + " ^^^^^\n" + "Type mismatch: cannot convert from Collection to String\n" + "----------\n" + - "2. ERROR in X.java (at line 21)\n" + + "3. ERROR in X.java (at line 21)\n" + " String result4 = (String) foo(); \n" + " ^^^^^^^^^^^^^^\n" + "Cannot cast from Collection to String\n" + "----------\n"); } +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=185787 +public void test1138() { + Map customOptions = getCompilerOptions(); + customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR); + this.runNegativeTest( + new String[] { + "X.java", + "import java.awt.Container;\n" + + "import java.util.Collection;\n" + + "public class X {\n" + + " private Collection foo() {\n" + + " return null;\n" + + " }\n" + + " private void showProblem() {\n" + + " Collection result = (Collection)foo(); //unnecessary cast\n" + + " }\n" + + "}", + }, + "----------\n" + + "1. ERROR in X.java (at line 8)\n" + + " Collection result = (Collection)foo(); //unnecessary cast\n" + + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + + "Unnecessary cast from Collection to Collection\n" + + "----------\n", + null, + true, + customOptions); +} } #P org.eclipse.jdt.core Index: compiler/org/eclipse/jdt/internal/compiler/lookup/ParameterizedGenericMethodBinding.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ParameterizedGenericMethodBinding.java,v retrieving revision 1.55 diff -u -r1.55 ParameterizedGenericMethodBinding.java --- compiler/org/eclipse/jdt/internal/compiler/lookup/ParameterizedGenericMethodBinding.java 4 Dec 2006 09:06:42 -0000 1.55 +++ compiler/org/eclipse/jdt/internal/compiler/lookup/ParameterizedGenericMethodBinding.java 7 May 2007 18:00:46 -0000 @@ -60,16 +60,22 @@ // 15.12.2.8 - inferring unresolved type arguments if (inferenceContext.hasUnresolvedTypeArgument()) { - TypeBinding expectedType = null; if (methodSubstitute.returnType != TypeBinding.VOID) { + TypeBinding expectedType = null; // if message invocation has expected type if (invocationSite instanceof MessageSend) { MessageSend message = (MessageSend) invocationSite; expectedType = message.expectedType; } - if (expectedType == null) expectedType = scope.getJavaLangObject(); // assume Object by default + if (expectedType != null) { + // record it was explicit from context, as opposed to assumed by default (see below) + inferenceContext.hasExplicitExpectedType = true; + } else { + expectedType = scope.getJavaLangObject(); // assume Object by default + } + inferenceContext.expectedType = expectedType; } - methodSubstitute = methodSubstitute.inferFromExpectedType(scope, expectedType, inferenceContext); + methodSubstitute = methodSubstitute.inferFromExpectedType(scope, inferenceContext); if (methodSubstitute == null) return null; } @@ -356,14 +362,14 @@ * Given some type expectation, and type variable bounds, perform some inference. * Returns true if still had unresolved type variable at the end of the operation */ - private ParameterizedGenericMethodBinding inferFromExpectedType(Scope scope, TypeBinding expectedType, InferenceContext inferenceContext) { + private ParameterizedGenericMethodBinding inferFromExpectedType(Scope scope, InferenceContext inferenceContext) { TypeVariableBinding[] originalVariables = this.originalMethod.typeVariables; // immediate parent (could be a parameterized method) int varLength = originalVariables.length; computeSubstitutes: { // infer from expected return type - if (expectedType != null) { - this.returnType.collectSubstitutes(scope, expectedType, inferenceContext, TypeConstants.CONSTRAINT_SUPER); + if (inferenceContext.expectedType != null) { + this.returnType.collectSubstitutes(scope, inferenceContext.expectedType, inferenceContext, TypeConstants.CONSTRAINT_SUPER); if (inferenceContext.status == InferenceContext.FAILED) return null; // impossible substitution } // infer from bounds of type parameters @@ -422,7 +428,7 @@ // adjust method types to reflect latest inference TypeBinding oldReturnType = this.returnType; this.returnType = Scope.substitute(this, this.returnType); - this.inferredReturnType = this.returnType != oldReturnType; + this.inferredReturnType = inferenceContext.hasExplicitExpectedType && this.returnType != oldReturnType; this.parameters = Scope.substitute(this, this.parameters); this.thrownExceptions = Scope.substitute(this, this.thrownExceptions); return this; Index: compiler/org/eclipse/jdt/internal/compiler/lookup/InferenceContext.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/InferenceContext.java,v retrieving revision 1.1 diff -u -r1.1 InferenceContext.java --- compiler/org/eclipse/jdt/internal/compiler/lookup/InferenceContext.java 25 Jun 2006 21:33:33 -0000 1.1 +++ compiler/org/eclipse/jdt/internal/compiler/lookup/InferenceContext.java 7 May 2007 18:00:46 -0000 @@ -19,6 +19,8 @@ MethodBinding genericMethod; int depth; int status; + TypeBinding expectedType; + boolean hasExplicitExpectedType; // indicates whether the expectedType (if set) was explicit in code, or set by default TypeBinding[] substitutes; final static int FAILED = 1; final static int RAW_SUBSTITUTION = 2;