JDK-6507317 : Problem when casting from parametrized type to concrete class
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6
  • Priority: P5
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-12-21
  • Updated: 2010-08-23
  • Resolved: 2010-08-23
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7Resolved
Related Reports
Relates :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Windows XP Proffesional

A DESCRIPTION OF THE PROBLEM :
Can not compile class containing casting from the parametrized type to a concrete type.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile following class:

--------------------------------------------------------------

import java.util.Comparator;

public class GenericsComparator <T extends Comparable<T>> implements Comparator<T>{
	public int compare(T o1, T o2) {
		if (o1 instanceof String)
		{
			String s = (String)o1;
		}
		return 0;
	}
}

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Class should be compilable.
ACTUAL -
>javac deleteme\GenericsComparator.java

deleteme\GenericsComparator.java:8: inconvertible types
found   : T
required: java.lang.String
                if (o1 instanceof String)
                    ^
deleteme\GenericsComparator.java:10: inconvertible types
found   : T
required: java.lang.String
                        String s = (String)o1;
                                           ^
2 errors

ERROR MESSAGES/STACK TRACES THAT OCCUR :
inconvertible types

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.Comparator;

public class GenericsComparator <T extends Comparable<T>> implements Comparator<T>{
	public int compare(T o1, T o2) {
		if (o1 instanceof String)
		{
			String s = (String)o1;
		}
		return 0;
	}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Explicit cast to Object:

------------------------------------------------

public class GenericsComparator <T extends Comparable<T>> implements Comparator<T>{

	public int compare(T o1, T o2) {
		if ((Object)o1 instanceof String)
		{
			String s = (String)(Object)o1;
		}
		return 0;
	}
}

Comments
EVALUATION Not reproducible in 7, after the fix of 6932571.
23-08-2010