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

(cl) Need new ClassLoader.defineClass API to improve class loading speed.

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3
    • 5.0
    • 1.4.1, 5.0
    • core-libs

    Description

      The current versions of defineClass require that the class data be copied to a java byte array. But in many cases, the class data is copied from a C buffer to a Java array and back to a C buffer in defineClass0(), without being examined by Java code.

      By adding a version of defineClass that accepts a java.nio.ByteBuffer instead of a byte array, redundant copying can be avoided.

      This change, plus a change to enhance support for mapped zip/jar files via ByteBuffers, will allow application-level class loaders to eliminate copying data in uncompressed jar files.

      Propsed API:

         /**
           * Converts a {@link java.nio.ByteBuffer <tt>ByteBuffer</tt>}
           * into an instance of class <tt>Class</tt>. The new class will use a default
           * {@link java.security.ProtectionDomain <tt>ProtectionDomain</tt>} as
           * specified in the documentation for {@link #defineClass(String, byte[],
           * int, int)}.
           * Before the <tt>Class</tt> can be used it must be resolved.
           *
           * <p> An invocation of this method of the form
           * <i>cl</i><tt>.defineClass(</tt><i>name</i><tt>,</tt> <i>bBuffer</i><tt>)</tt>
           * yields exactly the same result as the expression
           *
           * <blockquote><tt>
           * {@link #defineClass(String, java.nio.ByteBuffer,
           * ProtectionDomain)}(</tt><i>name</i><tt>,</tt><i>bBuffer</i><tt>,
           * null)</tt></blockquote>
           *
           * @param name
           * The expected name of the class, or <tt>null</tt>
           * if not known, using '<tt>.</tt>' and not '<tt>/</tt>' as the
           * separator and without a trailing <tt>.class</tt> suffix.
           *
           * @param b
           * The bytes that make up the class data.
           *
           * @return The <tt>Class</tt> object that was created from the specified
           * class data.
           *
           * @throws ClassFormatError
           * If the data did not contain a valid class
           *
           * @throws SecurityException
           * If an attempt is made to add this class to a package that
           * contains classes that were signed by a different set of
           * certificates than this class (which is unsigned), or if the
           * class name begins with "<tt>java.</tt>".
           *
           * @see #defineClass(String, java.nio.ByteBuffer,
           * ProtectionDomain)
           * @see #loadClass(String, boolean)
           * @see #resolveClass(Class)
           * @see java.nio.ByteBuffer
           *
           * @since 1.5
           */
         protected final Class defineClass(String name, java.nio.ByteBuffer b)
      throws ClassFormatError

          /**
           * Converts a {@link java.nio.ByteBuffer <tt>ByteBuffer</tt>}
           * into an instance of class <tt>Class</tt>,
           * with an optional <tt>ProtectionDomain</tt>. If the domain is
           * <tt>null</tt>, then a default domain will be assigned to the class as
           * specified in the documentation for {@link #defineClass(String, byte[],
           * int, int)}. Before the class can be used it must be resolved.
           *
           * <p> An invocation of this method of the form
           * <i>cl</i><tt>.defineClass(</tt><i>name</i><tt>,</tt> <i>bBuffer</i><tt>,</tt> <i>pd</i><tt>)</tt>
           * yields exactly the same result as the statements
           *
           * <blockquote><tt>
           * {
           * byte[] temp = new byte[</tt><i>bBuffer</i><tt>.{@link java.nio.ByteBuffer#len
           * len}()];
           * </tt><i>bBuffer</i><tt>.{@link java.nio.ByteBuffer#get(byte[])
           * get}(temp);
           * return {@link #defineClass(String, byte[], int, int, ProtectionDomain)
           * defineClass}(</tt><i>name</i><tt>, temp, 0, temp.length, </tt><i>pd</i><tt>);
           * }
           * </tt></blockquote>
           *
           * @param name
           * The expected name of the class, or <tt>null</tt> if not known,
           * using '<tt>.</tt>' and not '<tt>/</tt>' as the separator and
           * without a trailing "<tt>.class</tt>" suffix.
           *
           * @param b
           * The bytes that make up the class data. The bytes from positions
           * <tt>b.position()</tt> through <tt>b.position() + b.limit() -1 </tt>
           * should have the format of a valid class file as defined by the <a
           * href="http://java.sun.com/docs/books/vmspec/">Java Virtual
           * Machine Specification</a>.
           *
           * @param protectionDomain
           * The ProtectionDomain of the class, or <tt>null</tt>.
           *
           * @return The <tt>Class</tt> object created from the data,
           * and optional <tt>ProtectionDomain</tt>.
           *
           * @throws ClassFormatError
           * If the data did not contain a valid class.
           *
           * @throws NoClassDefFoundError
           * If <tt>name</tt> is not equal to the name of the class
           * specified by <tt>bbuffer</tt>
           *
           * @throws NoClassDefFoundError
           * If <tt>name</tt> is not equal to the name of the class
           * specified by <tt>bbuffer</tt>
           *
           * @throws SecurityException
           * If an attempt is made to add this class to a package that
           * contains classes that were signed by a different set of
           * certificates than this class, or if the class name begins with
           * "<tt>java.</tt>".
           *
           * see #defineClass(String, byte[], int, int, ProtectionDomain)
           *
           * @since 1.5
           */
       protected final Class defineClass(String name, java.nio.ByteBuffer b,
      ProtectionDomain protectionDomain)

      Attachments

        Issue Links

          Activity

            People

              drwhite Derek White
              drwhite Derek White
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: