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

Details
Type:
Enhancement
Submit Date:
2007-01-24
Status:
Resolved
Updated Date:
2014-02-21
Project Name:
JDK
Resolved Date:
2014-01-23
Component:
core-libs
OS:
windows_xp
Sub-Component:
java.lang:class_loading
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports
Duplicate:
Relates:
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
URL:   http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/c8c4f441fc76
User:  lana
Date:  2014-02-11 21:05:42 +0000

                                     
2014-02-11
URL:   http://hg.openjdk.java.net/jdk9/dev/jdk/rev/c8c4f441fc76
User:  mchung
Date:  2014-01-23 22:16:27 +0000

                                     
2014-01-23
The "Binary Name" section of the ClassLoader class description will be updated for clarification:

+  * {@code Class} objects for array classes are not created by {@code ClassLoader};
+  * use the {@link Class#forName} method instead.

In addition, ClassLoader.loadClass doesn't allow array syntax since JDK 6.  The workaround "sun.lang.ClassLoader.allowArraySyntax" system property will be removed in JDK 9.
                                     
2014-01-14
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