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

(cl) ClassLoader.defineClass() accepts class names with '/' as separator

XMLWordPrintable



      Name: vrR10176 Date: 02/12/2003



      The API spec for method ClassLoader.defineClass(String name, byte[] b, int off, int len, ProtectionDomain protectionDomain) says:

      "protected final Class defineClass(String name, byte[] b, int off, int len, ProtectionDomain protectionDomain) throws ClassFormatError
      ...
           The specified class name cannot begin with "java.", since all classes in the "java.* packages can only
      be defined by the bootstrap class loader. If the name parameter is not null, it must be equal to the name of
      the class specified by the byte array "b", otherwise a NoClassDefFoundError will be thrown.

      Parameters:
      name - The expected name of the class, or null if not known, using '.' and not '/' as the separator and
      without a trailing ".class" suffix.
      ..."

      But in JDK1.4.2-b16 (and also in JDKs 1.4.0, 1.4.1) defineClass() accepts invalid class names that use
      '/' as the separator. In this case NoClassDefFoundError should be thrown.

      To reproduce the issue execute the following test.

      ------------ testDC.java -------------------------------
      public class testDC {

          public static void main(String argv[]) {
              myClassLoader cl = new myClassLoader();
              try {
                  Class cls = cl.loadClass("p/testclass");
                  System.out.println("Class name of cls: " + cls.getName());
              } catch (Throwable e) {
                  System.out.println("Exception: " + e);
              }
          }
      }

      //=====================================================================

      class myClassLoader extends ClassLoader {

          public myClassLoader() {
              super();
          }

          public Class findClass(String name) throws ClassNotFoundException {

              int len = 0;
              byte data[] = { // array contains class p.testclass
                  (byte)0xca, (byte)0xfe, (byte)0xba, (byte)0xbe, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x2e,
                  (byte)0x00, (byte)0x05, (byte)0x07, (byte)0x00, (byte)0x02, (byte)0x01, (byte)0x00, (byte)0x10,
                  (byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c, (byte)0x61, (byte)0x6e,
                  (byte)0x67, (byte)0x2f, (byte)0x4f, (byte)0x62, (byte)0x6a, (byte)0x65, (byte)0x63, (byte)0x74,
                  (byte)0x07, (byte)0x00, (byte)0x04, (byte)0x01, (byte)0x00, (byte)0x0b, (byte)0x70, (byte)0x2f,
                  (byte)0x74, (byte)0x65, (byte)0x73, (byte)0x74, (byte)0x63, (byte)0x6c, (byte)0x61, (byte)0x73,
                  (byte)0x73, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x03, (byte)0x00, (byte)0x01, (byte)0x00,
                  (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00
              };

              System.out.println("defineClass():name: " + name);
              return defineClass(name, data, 0, data.length, null);
          }
      }

      ------------ Logs ---------------------------------------------
      $javac -d . testDC.java
      $
      $java -version
      java version "1.4.2-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-beta-b16)
      Java HotSpot(TM) Client VM (build 1.4.2-beta-b16, mixed mode)
      $
      $java -Xfuture testDC
      defineClass():name: p/testclass
      Class name of cls: p.testclass
      $
      $
      -------------------------------------------------------------------

      ======================================================================

            iris Iris Clark
            rslasunw Rsla Rsla (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: