Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6459208

Add API for initializing a Class and checking if it has been initialized

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Duplicate
    • P4
    • None
    • 5.0
    • core-libs

    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.

      Attachments

        Issue Links

          Activity

            People

              darcy Joe Darcy
              rmandalasunw Ranjith Mandala (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: