jdk/test/sun/reflect/AnonymousNewInstance/ManyNewInstanceAnonTest.java
author shurailine
Tue, 27 Oct 2015 20:06:02 -0700
changeset 33402 1156d495a525
parent 30820 0d4717a011d3
child 36966 4209c9e19c45
permissions -rw-r--r--
8140336: Add @modules for exported dependencies to jdk_core tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21609
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
     1
/*
33402
1156d495a525 8140336: Add @modules for exported dependencies to jdk_core tests
shurailine
parents: 30820
diff changeset
     2
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
21609
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
     4
 *
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
     8
 *
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    13
 * accompanied this code).
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    14
 *
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    18
 *
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    21
 * questions.
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    22
 */
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    23
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    24
/*
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    25
 * @test
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    26
 * @bug 7194897
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    27
 * @summary JSR 292: Cannot create more than 16 instances of an anonymous class
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 24366
diff changeset
    28
 * @modules java.base/sun.misc
33402
1156d495a525 8140336: Add @modules for exported dependencies to jdk_core tests
shurailine
parents: 30820
diff changeset
    29
 *          java.management
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 24366
diff changeset
    30
 * @library /lib/testlibrary
21609
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    31
 * @author  Robert Field
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    32
 * @compile -XDignore.symbol.file ManyNewInstanceAnonTest.java
24366
57c0a8b7a936 8034960: Serviceability tests using @library failing with java.lang.NoClassDefFoundError
ykantser
parents: 21672
diff changeset
    33
 * @build jdk.testlibrary.*
21609
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    34
 * @run main ClassFileInstaller ManyNewInstanceAnonTest
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    35
 * @run main/othervm -Xbootclasspath/a:. -Xverify:all ManyNewInstanceAnonTest
21672
33014eb0cb79 8028055: (reflect) invoking Method/Constructor in anonymous classes breaks with -Dsun.reflect.noInflation=true
jfranck
parents: 21609
diff changeset
    36
 * @run main/othervm -Xbootclasspath/a:. -Xverify:all -Dsun.reflection.noInflation=true ManyNewInstanceAnonTest
21609
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    37
 */
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    38
import java.io.ByteArrayOutputStream;
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    39
import java.io.InputStream;
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    40
import sun.misc.Unsafe;
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    41
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    42
public class ManyNewInstanceAnonTest {
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    43
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    44
    static final int REPS = 20;
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    45
    static final Class<?> klass = ManyNewInstanceAnonTest.class;
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    46
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    47
    public static void main(String[] args) throws Exception {
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    48
        Class<?> c = Unsafe.getUnsafe().defineAnonymousClass(klass, readClassFile(), null);
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    49
        for (int i = 0; i < REPS; ++i) {
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    50
            System.out.printf("%d: %s\n", i, c.newInstance());
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    51
        }
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    52
        System.out.println("Passed.");
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    53
    }
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    54
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    55
    private static byte[] readClassFile() throws Exception {
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    56
        try (InputStream in = klass.getResourceAsStream(klass.getSimpleName() + ".class");
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    57
             ByteArrayOutputStream out = new ByteArrayOutputStream())
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    58
        {
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    59
            int b;
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    60
            while ((b = in.read()) != -1) {
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    61
                out.write(b);
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    62
            }
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    63
            return out.toByteArray();
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    64
        }
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    65
    }
f23c375e4baf 7194897: JSR 292: Cannot create more than 16 instances of an anonymous class
rfield
parents:
diff changeset
    66
}