United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-5030212 : please add a typesafe cast for Class types

Details
Type:
Enhancement
Submit Date:
2004-04-12
Status:
Resolved
Updated Date:
2012-09-28
Project Name:
JDK
Resolved Date:
2004-04-26
Component:
core-libs
OS:
solaris_8
Sub-Component:
java.lang:reflect
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
5.0
Fixed Versions:
5.0 (b49)

Related Reports
Relates:
Relates:

Sub Tasks

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
EVALUATION

Will aim for Tiger.

###@###.### 2004-04-12
                                     
2004-04-12
PUBLIC COMMENTS

...
                                     
2004-06-10
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);
   }
                                     
2004-06-11
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
tiger-beta2

FIXED IN:
tiger-beta2

INTEGRATED IN:
tiger-b49
tiger-beta2


                                     
2004-06-14



Hardware and Software, Engineered to Work Together