JDK-5030212 : please add a typesafe cast for Class types
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris_8
  • CPU: generic
  • Submitted: 2004-04-12
  • Updated: 2017-05-16
  • Resolved: 2004-04-26
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.
Other
5.0 b49Fixed
Related Reports
Relates :  
Relates :  
Description
There are one or two places where method parameters of type Class will now 
be more specific, for example

     public DragGestureRecognizer createDragGestureRecognizer(
        Class<? extends MyDragGestureRecognizer> abstractRecognizerClass, ...

And Class.forName() now returns Class<?>.

In -source 1.5, this can break a client that uses

    dgr.createDragGestureRecognizer(Class.forName("MyDragGestureRecognizer"));

and yet the programmer has no way to generify the client soundly.  How 
should clients adjust their code?  Adding a cast (in this case to 
Class<? extends DragGestureRecognizer>) provokes an unchecked warning, 
and there is no way to get rid of it.  Given that some API designers 
will want to create factory-like typesafe APIs in which clients will use 
Class.forName(), there needs to be a way for clients to use the API 
safely.  I suggest adding the following method to class Class:

  class Class<T> { ...

    public <U> Class<? extends U> asSubclass(Class<U> clazz) {
      if (clazz.isAssignableFrom(this))
        return (Class<? extends U>) this;
      else
        throw new ClassCastException(this);
    }
  }

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger-beta2 FIXED IN: tiger-beta2 INTEGRATED IN: tiger-b49 tiger-beta2
14-06-2004

SUGGESTED FIX add the following to class Class: /** * Casts this <tt>Class</tt> object to represent a subclass of the class * represented by the specified class object. Checks that that the cast * is valid, and throws a <tt>ClassCastException</tt> if it is not. If * this method succeeds, it always returns a reference to this class object. * * <p>This method is useful when a client needs to "narrow" the type of * a <tt>Class</tt> object to pass it to an API that restricts the * <tt>Class</tt> objects that it is willing to accept. A cast would * generate a compile-time warning, as the correctness of the cast * could not be checked at runtime (because generic types are implemented * by erasure). * * @returns this <tt>Class</tt> object, cast to represent a subclass of * the specified class object. * @throws ClassCastException if this <tt>Class</tt> object does not * represent a subclass of the specified class (here "subclass" includes * the class itself). */ public <U> Class<? extends U> asSubclass(Class<U> clazz) { if (clazz.isAssignableFrom(this)) return (Class<? extends U>) this; else throw new ClassCastException(this); }
11-06-2004

PUBLIC COMMENTS ...
10-06-2004

EVALUATION Will aim for Tiger. ###@###.### 2004-04-12
12-04-2004