JDK-6658004 : Add a cast() method to Object
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 7
  • Priority: P5
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2008-02-01
  • Updated: 2011-02-16
  • Resolved: 2008-02-01
Related Reports
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
Although generics have reduced the need for casting there are still occasions where you need to do it. A cast method that uses type argument inference would make casting much easier.

So if this method existed in Object :

   @SuppressWarnings("unchecked")
   public <T> T cast()
   {
      return (T)this;
   }

then most casts would be simplified to

// very contrived example
Object o = new MyType();

MyType myObject = o.cast();

instead of the current

MyType myObject = (MyType)o;



JUSTIFICATION :
This means less code to type and read. That has to be good!

Comments
EVALUATION There is a cast method on Class objects for this purpose since JDK 1.5; see bug 4881275.
01-02-2008