JDK-7194897 : JSR 292: Cannot create more than 16 instances of an anonymous class
  • Type: Bug
  • Component: core-libs
  • Affected Version: 7u6
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris_11
  • CPU: x86
  • Submitted: 2012-08-29
  • Updated: 2013-11-22
  • Resolved: 2013-11-04
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 Other
8 b117Fixed port-stage-ppc-aixFixed
Related Reports
Duplicate :  
Relates :  
Relates :  
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

Comments
A workaround is to use MethodHandles.findConstructor instead of newInstance. R��mi
12-10-2013

EVALUATION The bug is in the reflection code. There seems to be a problem when reflection spins bytecodes ("inflation"). Bumping the threshold to 20 makes the test pass: $ java -Dsun.reflect.inflationThreshold=20 AnonClassTest 0: AnonClassTest/28434154@a1e2da 1: AnonClassTest/28434154@347448 2: AnonClassTest/28434154@1be513c 3: AnonClassTest/28434154@1c74f8d 4: AnonClassTest/28434154@37b90c 5: AnonClassTest/28434154@55cf3f 6: AnonClassTest/28434154@1996bbd 7: AnonClassTest/28434154@1707653 8: AnonClassTest/28434154@686cea 9: AnonClassTest/28434154@1c68e73 10: AnonClassTest/28434154@b245dc 11: AnonClassTest/28434154@87400 12: AnonClassTest/28434154@f7cbd1 13: AnonClassTest/28434154@661532 14: AnonClassTest/28434154@1f9aeda 15: AnonClassTest/28434154@1469a69 16: AnonClassTest/28434154@437dcb 17: AnonClassTest/28434154@f7cbcc 18: AnonClassTest/28434154@64f6fe 19: AnonClassTest/28434154@93c5e7 $ Please note the ClassNotFoundException message: Caused by: java.lang.ClassNotFoundException: AnonClassTest.28180264 Obviously the problem is the "/" in the anonymous class name since it gets replaced with a ".".
29-08-2012