VMAnonymousClasses.java revision 11707:ad7af1afda7a
1/*
2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
26 * @bug 8058828
27 * @modules java.base/jdk.internal.org.objectweb.asm
28 *          java.base/jdk.internal.misc
29 *
30 * @run main/bootclasspath/othervm -Xbatch compiler.jsr292.VMAnonymousClasses
31 */
32
33package compiler.jsr292;
34
35import jdk.internal.misc.Unsafe;
36import jdk.internal.org.objectweb.asm.ClassWriter;
37import jdk.internal.org.objectweb.asm.MethodVisitor;
38import jdk.internal.org.objectweb.asm.Opcodes;
39
40import java.lang.invoke.ConstantCallSite;
41import java.lang.invoke.MethodHandle;
42import java.lang.invoke.MethodHandles;
43import java.lang.invoke.MethodType;
44import java.lang.invoke.MutableCallSite;
45import java.lang.invoke.VolatileCallSite;
46
47public class VMAnonymousClasses {
48    static final String TEST_METHOD_NAME = "constant";
49
50    static final Unsafe UNSAFE = Unsafe.getUnsafe();
51
52    static int getConstantPoolSize(byte[] classFile) {
53        // The first few bytes:
54        // u4 magic;
55        // u2 minor_version;
56        // u2 major_version;
57        // u2 constant_pool_count;
58        return ((classFile[8] & 0xFF) << 8) | (classFile[9] & 0xFF);
59    }
60
61    static void test(Object value) throws ReflectiveOperationException {
62        System.out.printf("Test: %s", value != null ? value.getClass() : "null");
63
64        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
65        cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, "Test", null, "java/lang/Object", null);
66
67        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC, TEST_METHOD_NAME, "()Ljava/lang/Object;", null, null);
68
69        String placeholder = "CONSTANT";
70        int index = cw.newConst(placeholder);
71        mv.visitLdcInsn(placeholder);
72        mv.visitInsn(Opcodes.ARETURN);
73
74        mv.visitMaxs(0, 0);
75        mv.visitEnd();
76
77        byte[] classFile = cw.toByteArray();
78
79        Object[] cpPatches = new Object[getConstantPoolSize(classFile)];
80        cpPatches[index] = value;
81
82        Class<?> test = UNSAFE.defineAnonymousClass(VMAnonymousClasses.class, classFile, cpPatches);
83
84        Object expectedResult = (value != null) ? value : placeholder;
85        for (int i = 0; i<15000; i++) {
86            Object result = test.getMethod(TEST_METHOD_NAME).invoke(null);
87            if (result != expectedResult) {
88                throw new AssertionError(String.format("Wrong value returned: %s != %s", value, result));
89            }
90        }
91        System.out.println(" PASSED");
92    }
93
94    public static void main(String[] args) throws ReflectiveOperationException  {
95        // Objects
96        test(new Object());
97        test("TEST");
98        test(new VMAnonymousClasses());
99        test(null);
100
101        // Class
102        test(String.class);
103
104        // Arrays
105        test(new boolean[0]);
106        test(new byte[0]);
107        test(new char[0]);
108        test(new short[0]);
109        test(new int[0]);
110        test(new long[0]);
111        test(new float[0]);
112        test(new double[0]);
113        test(new Object[0]);
114
115        // Multi-dimensional arrays
116        test(new byte[0][0]);
117        test(new Object[0][0]);
118
119        // MethodHandle-related
120        MethodType   mt = MethodType.methodType(void.class, String[].class);
121        MethodHandle mh = MethodHandles.lookup().findStatic(VMAnonymousClasses.class, "main", mt);
122        test(mt);
123        test(mh);
124        test(new ConstantCallSite(mh));
125        test(new MutableCallSite(MethodType.methodType(void.class)));
126        test(new VolatileCallSite(MethodType.methodType(void.class)));
127
128        System.out.println("TEST PASSED");
129    }
130}
131