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

(reflect) ConstructorAccessorImpl restricts set of viable custom classloaders

XMLWordPrintable

      sun.reflect.ConstructorAccessorImpl needs to be loaded in custom class loaders which only delegate public API class loads to their parent class loader.

      OPERATING SYSTEM(S): Solaris 2.8
      FULL JDK VERSION(S): java version "1.5.0_03"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_03-b07)
      Java HotSpot(TM) Server VM (build 1.5.0_03-b07, mixed mode)
      DESCRIPTION:
      'CLTest' is the launcher program. It uses 'MyClassLoader' to load the class 'p1.LoadMe'. When 'CLTest'
      then tries to create a new instance of 'p1.LoadMe', via reflection, it fails with the following error:
      Exception in thread "main" java.lang.NoClassDefFoundError: sun/reflect/ConstructorAccessorImpl
              at sun.misc.Unsafe.defineClass(Native Method)
              at sun.reflect.ClassDefiner.defineClass(ClassDefiner.java:45)
              at sun.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:381)
              at java.security.AccessController.doPrivileged(Native Method)
              at sun.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:377)
              at sun.reflect.MethodAccessorGenerator.generateConstructor(MethodAccessorGenerator.java:76)
              at sun.reflect.ReflectionFactory.newConstructorAccessor(ReflectionFactory.java:166)
              at java.lang.reflect.Constructor.acquireConstructorAccessor(Constructor.java:537)
              at java.lang.reflect.Constructor.newInstance(Constructor.java:493)
              at java.lang.Class.newInstance0(Class.java:350)
              at java.lang.Class.newInstance(Class.java:303)
              at CLTest.main(CLTest.java:10)
      To reproduce, cd to the directory containing CLTest.class, and use the following command:
       java -Dsun.reflect.noInflation=true CLTest
      Note that -Dsun.reflect.noInflation=true forces the error to occur predictably. The error can also occur without this setting, but it depends on an internal threshold being exceeded.
      TESTCASE:
      CLTest.java:
      import java.lang.reflect.Method;
      public class CLTest {
       public static void main(String[] args) {
        try {
         MyClassLoader mcl = new MyClassLoader();
         Class c = mcl.loadClass("p1.LoadMe");
         c.newInstance();
        } catch(Exception e) {
         e.printStackTrace();
        }
       }
      }
      MyClassLoader.java:
      import java.net.URL;
      import java.net.URLClassLoader;
      public class MyClassLoader extends URLClassLoader {
       public MyClassLoader() throws Exception {
        super(new URL[]{new URL("file:///" +
              System.getProperty("user.dir") +
              System.getProperty("file.separator") +
              "mclClassPath" +
              System.getProperty("file.separator"))});
       }
       
       public Class loadClass(String name) throws ClassNotFoundException {
        if(name.startsWith("java.")) {
         try {
          Class c = getParent().loadClass(name);
          if(c != null)
           return c;
         } catch(Exception e) {
         }
        }
        return findClass(name);
       }
      }
      LoadMe.java:
      package p1;
      import java.lang.reflect.Method;
      public class LoadMe {
       public static void method1() {
        System.out.println("HELLO!");
       }
      }
      ###@###.### 2005-05-05 16:46:23 GMT

            Unassigned Unassigned
            elarsen Erik Larsen (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: