JDK-6184887 : getClass() doen't take into account that some classes are final
  • Type: Bug
  • Component: specification
  • Sub-Component: language
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-10-26
  • Updated: 2010-05-08
  • Resolved: 2006-10-31
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
The JLS3 said that for a variable x of type X,
X.getClass()  return a type Class<? extends Erasure(X)>.

I think this rule doesn't take into account of classes declared final.
Indeed, final class could not have sub-classes, so

"toto".getClass() could be typed Class<String>.

This is a straighforward change because Class<? extends String> is a
subtype of Class<String>

PS: i have reported this as a bug but perhaps it's an RFE.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
try to compile the code

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
compile withour error
ACTUAL -
the compiler emit an erreor "incompatible types"

ERROR MESSAGES/STACK TRACES THAT OCCUR :
incompatible types :
found   : java.lang.Class<capture of ? extends java.lang.String>
required: java.lang.Class<java.lang.String>

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
class Test {

Class<String> clazz="toto".getClass();

}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Class<String> clazz=(Class<String>)"toto".getClass();
###@###.### 10/26/04 22:55 GMT

Comments
EVALUATION In the description's use case, it would be neat to have X.getClass() return Class<X>, but class literals suffice for getting Class<X> instances.
31-10-2006

WORK AROUND Use String.class instead. ###@###.### 10/27/04 00:10 GMT
27-10-2004