JDK-5029430 : request a conditional typecast function to java.lang.Class
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-04-09
  • Updated: 2012-09-28
  • Resolved: 2006-11-13
Related Reports
Relates :  
Relates :  
Description
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) 
======================================================================

Comments
EVALUATION Developers are free to provide themselves with a helper method that does what the submitter desires (reminiscent of Eifel's "assignment attempt" operator, ?=), and this is very straight forward: public class Util { public static <T> T as(Class<T> c, Object x) { if (c.isInstance(x)) return c.cast(x); else return null; } } Also, modern dynamic compilers will typically eliminate common subexpressions and, for Sun's Hotspot compilers at least, a second usage of instanceof in a given code path such as the submitter describes results in zero additonal code to execute at runtime: the compiler exactly tracks the type information in cases like this.
13-11-2006