JDK-6176471 : (reflect) Extend Class.forName with class tokens
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic
  • CPU: generic
  • Submitted: 2004-10-08
  • Updated: 2012-09-28
  • Resolved: 2004-11-05
Related Reports
Relates :  
Relates :  
Description
Please extend the typing of java.lang.Class.forName by adding either a static method:

public static <U> Class<? extends U> forName(String className, Class<U> bound) {
   return bound.cast(forName(className));
}

Or a non-static method:

public Class<? extends T> forSubclassName(String className) {
   return cast(forName(className));
}

This would help in situations like:

import java.util.EventListener;
import javax.swing.event.EventListenerList;

class Test {
    void test(String name, EventListenerList list, EventListener l)
	throws ClassNotFoundException
    {
	list.add((Class)Class.forName(name), l);
    }
}
###@###.### 10/8/04 21:34 GMT

Comments
EVALUATION You can use this instead: Class.forName(className).asSubclass(bound) asSubclass has the advantage of working with both Class.forName methods, and all of the ClassLoader.find*Class and ClassLoader.loadClass methods. . ###@###.### 2004-11-05 22:01:48 GMT
05-11-2004