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

JSR 292: Cannot create more than 16 instances of an anonymous class

    XMLWordPrintable

Details

    • Bug
    • Status: Resolved
    • P3
    • Resolution: Fixed
    • 7u6
    • 8
    • core-libs
    • b117
    • x86
    • solaris_11

    Backports

      Description

        When a class has been loaded with sun.invoke.anon.AnonymousClassLoader, the seventeenth invocation of Class.newInstance() on that class fails with a NoClassDefFoundError.

        Example:

        import java.io.ByteArrayOutputStream;
        import java.io.InputStream;
        import sun.invoke.anon.AnonymousClassLoader;

        public class AnonClassTest {
            public static void main(String[] args) throws Exception {
                AnonymousClassLoader loader = new AnonymousClassLoader();
                Class c = loader.loadClass(readClassFile());

                for (int i = 0; i < 20; i++) {
                    System.out.println(i + ": " + c.newInstance());
                }
            }

            private static byte[] readClassFile() throws Exception {
                try (InputStream in =
                        AnonClassTest.class.getResourceAsStream("AnonClassTest.class");
                     ByteArrayOutputStream out = new ByteArrayOutputStream())
                {

                    int b;
                    while ((b = in.read()) != -1) {
                        out.write(b);
                    }

                    return out.toByteArray();
                }
            }
        }

        Which produces the following output:

        0: AnonClassTest/28180264@4ad14c
        1: AnonClassTest/28180264@1f2e41d
        2: AnonClassTest/28180264@15e4dbe
        3: AnonClassTest/28180264@420e44
        4: AnonClassTest/28180264@b6a5fb
        5: AnonClassTest/28180264@14b1fda
        6: AnonClassTest/28180264@1183871
        7: AnonClassTest/28180264@12192a9
        8: AnonClassTest/28180264@123ce3f
        9: AnonClassTest/28180264@1b98cbb
        10: AnonClassTest/28180264@c68351
        11: AnonClassTest/28180264@d76237
        12: AnonClassTest/28180264@7148e9
        13: AnonClassTest/28180264@169c9a2
        14: AnonClassTest/28180264@34e2cc
        15: AnonClassTest/28180264@1b0edb2
        Exception in thread "main" java.lang.NoClassDefFoundError: AnonClassTest/28180264
                at sun.reflect.GeneratedConstructorAccessor1.newInstance(Unknown Source)
                at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
                at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
                at java.lang.Class.newInstance0(Class.java:372)
                at java.lang.Class.newInstance(Class.java:325)
                at AnonClassTest.main(AnonClassTest.java:11)
        Caused by: java.lang.ClassNotFoundException: AnonClassTest.28180264
                at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
                at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
                at java.security.AccessController.doPrivileged(Native Method)
                at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
                at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
                ... 6 more

        Attachments

          Issue Links

            Activity

              People

                rfield Robert Field
                khatlen Knut Anders Hatlen
                Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: