-
Enhancement
-
Resolution: Fixed
-
P3
-
5.0
-
b49
-
generic
-
solaris_8
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);
}
}
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);
}
}
- relates to
-
JDK-4881275 (reflect) Class.cast() - typesafe cast desired
- Resolved
-
JDK-5029430 request a conditional typecast function to java.lang.Class
- Closed