United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6459208 : Add API for initializing a Class and checking if it has been initialized

Details
Type:
Enhancement
Submit Date:
2006-08-10
Status:
Closed
Updated Date:
2012-09-28
Project Name:
JDK
Resolved Date:
2006-08-15
Component:
core-libs
OS:
windows_xp
Sub-Component:
java.lang:reflect
CPU:
x86
Priority:
P4
Resolution:
Duplicate
Affected Versions:
5.0
Fixed Versions:

Related Reports
Duplicate:

Sub Tasks

Description
A DESCRIPTION OF THE REQUEST :
Since Java 5, class literal constructs do not trigger class initialization anymore. Therefore, code depending on this side-effect is broken and needs to explicitly load and initialize classes by its name and classLoader. This is a well-known problem, documented at http://java.sun.com/javase/technologies/compatibility.jsp#literal .

This RFE proposes adding two new methods to java.lang.Class: initialize() and isInitialized() .

JUSTIFICATION :
java.lang.Class.initialize() is needed since existing code depend on classes been initialized. There is no way to force initialization of a Class instance besides loading it using the following idiom:

Class.forName(clazz.getName(), true, clazz.getClassLoader());

which is ugly, requires users to catch an exception that should never been thrown in this situation (ClassNotFoundException) and requires RuntimePermission("getClassLoader") if clazz.getClassLoader() == null.

java.lang.Class.isInitialized() is needed for consistency and in order to be able to emit warnings for the cases where initialization can affect the result of an operation or a method.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
These two methods could be implemented as follows:

/**
 * Initializes the class or interface represented by this instance.
 *<p>Initialization of a class consists of executing its static initializers and the initializers
 * for static fields (class variables) declared in the class. Initialization of an interface
 * consists of executing the initializers for fields (constants) declared there.</p>
 * <p>Before a class is initialized, its superclass must be initialized, but interfaces
 * implemented by the class are not initialized. Similarly, the superinterfaces of an interface
 * are not initialized before the interface is initialized.</p>
 * <p>For more details, please refer to the <a
 * href="http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#44557">JLS
* section 12.4</a>
 * @exception LinkageError if the linkage fails
 * @exception ExceptionInInitializerError if the initialization provoked
 *            by this method fails
 * @see #isInitialized()
 */
public void initialize();

/**
 * Determines if the class or interface represented by this instance has been initialized.
 *<p>Initialization of a class consists of executing its static initializers and the initializers
 * for static fields (class variables) declared in the class. Initialization of an interface
 * consists of executing the initializers for fields (constants) declared there.</p>
 * <p>For more details, please refer to the <a
 * href="http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#44557">JLS
 * section 12.4</a>
 * @return  <code>true</code> if this class has already been initialized;
 *          <code>false</code> otherwise.
 * @see #initialize()
 */
public boolean isInitialized();

CUSTOMER SUBMITTED WORKAROUND :
For initialize(), the above inefficient idiom works. There is no replacement for isInitialized() except for relying on side-effects of a specific Class object been initialized, i.e., it is not always possible to work around it.

                                    

Comments
EVALUATION

Closing as duplicate of 4993813.
                                     
2006-08-15



Hardware and Software, Engineered to Work Together