-
Enhancement
-
Resolution: Won't Fix
-
P4
-
None
-
1.4.2
-
x86
-
windows_xp
Name: rmT116609 Date: 04/08/2004
A DESCRIPTION OF THE REQUEST :
I propose to add a conditional typecast member function to class java.lang.Class<T>
It shall accept one parameter of type java.lang.Object, return a value of type T and perform the following steps:
If source is null, it returns null. If source is assignable to the runtime type T that this class object represents,it returns source cast to T.
Otherwise, it returns null.
The function never throws.
The function can be defined by the following sample implementation
(as it would appear within java.lang.Class)
public T as(Object source)
{
return this.isInstance(source) ? (T)source : null;
}
JUSTIFICATION :
It is currently not possible to cast an object to a destination type without receiving a ClassCastException if it fails. In cases where you want to check for the implementation of an interface or extension of a base class, this leaves you with two possibilities:
1. (Possibly nested) casts int try-blocks with corresponding catch-blocks.
(Highly unreadable and unscalable)
2. Preceding every cast with an "instanceof" expression.
(showing clearly in the code that, in disguse of the "instanceof" operator, two checks for the same destination type are executed immediately after one another).
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The following code shows the expected behaviour:.
(compare to "Actual behaviour")
import javax.swing.borders.*;
void setTitle(Border border)
{
TitledBorder tb = TitledBorder.class.as(border);
if(tb != null)
tb.setTitle("hello world");
}
ACTUAL -
The following code shows the actual behaviour:.
(compare to "Expected behaviour")
import javax.swing.borders.*;
void setTitle(Border border)
{
TitledBorder tb;
if(border instanceof TitledBorder)
{
tb = (TitledBorder)border;
tb.setTitle("hello world");
}
}
(Incident Review ID: 239653)
======================================================================
A DESCRIPTION OF THE REQUEST :
I propose to add a conditional typecast member function to class java.lang.Class<T>
It shall accept one parameter of type java.lang.Object, return a value of type T and perform the following steps:
If source is null, it returns null. If source is assignable to the runtime type T that this class object represents,it returns source cast to T.
Otherwise, it returns null.
The function never throws.
The function can be defined by the following sample implementation
(as it would appear within java.lang.Class)
public T as(Object source)
{
return this.isInstance(source) ? (T)source : null;
}
JUSTIFICATION :
It is currently not possible to cast an object to a destination type without receiving a ClassCastException if it fails. In cases where you want to check for the implementation of an interface or extension of a base class, this leaves you with two possibilities:
1. (Possibly nested) casts int try-blocks with corresponding catch-blocks.
(Highly unreadable and unscalable)
2. Preceding every cast with an "instanceof" expression.
(showing clearly in the code that, in disguse of the "instanceof" operator, two checks for the same destination type are executed immediately after one another).
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The following code shows the expected behaviour:.
(compare to "Actual behaviour")
import javax.swing.borders.*;
void setTitle(Border border)
{
TitledBorder tb = TitledBorder.class.as(border);
if(tb != null)
tb.setTitle("hello world");
}
ACTUAL -
The following code shows the actual behaviour:.
(compare to "Expected behaviour")
import javax.swing.borders.*;
void setTitle(Border border)
{
TitledBorder tb;
if(border instanceof TitledBorder)
{
tb = (TitledBorder)border;
tb.setTitle("hello world");
}
}
(Incident Review ID: 239653)
======================================================================
- relates to
-
JDK-4881275 (reflect) Class.cast() - typesafe cast desired
- Resolved
-
JDK-5030212 please add a typesafe cast for Class<?> types
- Resolved