|
Description
|
A DESCRIPTION OF THE REQUEST :
Method Class.forName() should return specific typed class instead of wildcard typed:
public static <T> Class<T> forName(String name)
public static <T> Class<T> forName(String name, boolean initialize, ClassLoader loader)
JUSTIFICATION :
Saves casting on using newInstance() method after calling Class.forName().
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
No casting on using newInstance() method:
Class<MyClass> c = forName("MyClass");
MyClass cs = c.newInstance();
ACTUAL -
Current implementation requires casting:
Class c = forName("MyClass");
MyClass cs = (MyClass)c.newInstance();
---------- BEGIN SOURCE ----------
public class Class<T> {
...
public static <T> Class<T> forName(String className)
throws ClassNotFoundException {
return forName0(className, true, ClassLoader.getCallerClassLoader());
}
public static <T> Class<T> forName(String name, boolean initialize,
ClassLoader loader)
throws ClassNotFoundException
{
if (loader == null) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
ClassLoader ccl = ClassLoader.getCallerClassLoader();
if (ccl != null) {
sm.checkPermission(
SecurityConstants.GET_CLASSLOADER_PERMISSION);
}
}
}
return forName0(name, initialize, loader);
}
private static native <T> Class<T> forName0(String name, boolean initialize,
ClassLoader loader)
throws ClassNotFoundException;
...
}
---------- END SOURCE ----------
Edited to correct instanceOf() references that should have been newInstance()
Posted Date : 2009-10-06 10:28:57.0
|