United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 6516909 (cl spec) ClassLoader.loadClass() clarification to indiate it shouldn't be used for array classes
6516909 : (cl spec) ClassLoader.loadClass() clarification to indiate it shouldn't be used for array classes

Details
Type:
Enhancement
Submit Date:
2007-01-24
Status:
Open
Updated Date:
2013-05-09
Project Name:
JDK
Resolved Date:
Component:
core-libs
OS:
windows_xp
Sub-Component:
java.lang:class_loading
CPU:
x86
Priority:
P3
Resolution:
Unresolved
Affected Versions:
6
Targeted Versions:
8

Related Reports
Relates:
Relates:

Sub Tasks

Description
A DESCRIPTION OF THE PROBLEM :
As a follow up to the evaluation in bug #6446627 I am requesting that the Javadoc for ClassLoader.loadClass(String, boolean) be updated to explicitly note that parameter "name" does not accept array classes and Class.forName() should be used instead.


URL OF FAULTY DOCUMENTATION :
http://java.sun.com/javase/6/docs/api/java/lang/ClassLoader.html#loadClass(java.lang.String,%20boolean)

                                    

Comments
EVALUATION

Generally speaking reflective loading of a class by name should be accomplished by using this static method in java.lang.Class:

  public static Class<?> forName(String name, boolean initialize, ClassLoader loader)
      throws ClassNotFoundException

The ClassLoader.loadClass() method is more typically used for class loader delegation.  Invocation of Class.forName() may eventually invoke ClassLoader.loadClass() after handling VM name resolution.  In particular, for array classes, this would involve loading the array's component type.  

Thus, we highly recommend replacement of this code:

  myClassLoader.loadClass(className);

With this code:

  Class.forName(className,false,myClassLoader);

An appropriate usage note describing the above recommended practice should be added to the ClassLoader javadoc.
                                     
2007-07-31



Hardware and Software, Engineered to Work Together