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 package runtime.valhalla.valuetypes;
24
25 import java.lang.reflect.Field;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import static java.lang.reflect.Modifier.*;
29 import java.net.URL;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32
33 import jdk.experimental.bytecode.*;
34 import jdk.experimental.value.ValueType;
35
36 import jdk.internal.org.objectweb.asm.*;
37 import static jdk.internal.org.objectweb.asm.Opcodes.*;
38
39 import static jdk.test.lib.Asserts.*;
40
41 /*
42 * @test DeriveValueTypeCreation
43 * @summary Derive Value Type creation test
44 * @library /test/lib
45 * @compile DeriveValueTypeCreation.java
46 * @modules java.base/jdk.internal.org.objectweb.asm
47 * @build runtime.valhalla.valuetypes.ValueCapableClass
48 * @run main/othervm -Xint -XX:+EnableMVT runtime.valhalla.valuetypes.DeriveValueTypeCreation
49 * @run main/othervm -Xcomp -XX:+EnableMVT runtime.valhalla.valuetypes.DeriveValueTypeCreation
50 */
51 public class DeriveValueTypeCreation {
52
53 public static final String VCC_CLASSNAME = "runtime.valhalla.valuetypes.ValueCapableClass";
54 public static final String DVT_SUFFIX = "$Value";
55
56 public static final int TEST_DEF_CLASS_ACCESS = ACC_SUPER | ACC_PUBLIC | ACC_FINAL;
57 public static final int TEST_DEF_FIELD_ACCESS = ACC_PUBLIC | ACC_FINAL;
58 public static final String OBJECT_CLASS_DESC = "java/lang/Object";
59
60 public static void main(String[] args) {
61 DeriveValueTypeCreation test = new DeriveValueTypeCreation();
62 test.run();
63 }
64
65 public void run() {
66 loadAndRunTest();
79 catch (Throwable t) { fail("Failed to invoke VCC.test()", t); }
80
81 checkValueCapableClass(clazz);
82 }
83
84 void checkValueCapableClass(Class<?> clazz) {
85 if (!ValueType.classHasValueType(clazz)) {
86 fail("!classHasValueType: " + clazz);
87 }
88
89 ValueType<?> vt = ValueType.forClass(clazz);
90 if (vt == null) {
91 fail("ValueType.forClass failed");
92 }
93
94 System.out.println("ValueType: " + vt);
95
96 if (vt.boxClass() != clazz) {
97 fail("ValueType.boxClass() failed");
98 }
99 if (vt.sourceClass() != clazz) {
100 fail("ValueType.sourceClass() failed");
101 }
102
103 // DVT class matches our expectations for the current implementation...
104 Class<?> vtClass = vt.valueClass();
105 if (!vtClass.getName().equals(clazz.getName() + DVT_SUFFIX)) {
106 fail("ValueType.valueClass() failed");
107 }
108 if (!vtClass.getSuperclass().getName().equals("java.lang.__Value")) {
109 fail("ValueType.valueClass() isn't a Value Type class");
110 }
111
112 // Exercise "Class.getSimpleName()", we've cause problems with it before
113 String sn = vtClass.getSimpleName();
114 System.out.println("SimpleName: " + sn);
115
116 if (clazz.getClassLoader() != vtClass.getClassLoader()) {
117 fail("ClassLoader mismatch");
118 }
119 if (clazz.getProtectionDomain() != vtClass.getProtectionDomain()) {
120 fail("ProtectionDomain mismatch");
121 }
162 return createTestVccClassBytes(name, TEST_DEF_CLASS_ACCESS, vccAnnotation, OBJECT_CLASS_DESC, "I", TEST_DEF_FIELD_ACCESS);
163 }
164
165 byte[] createTestVccClassBytes(String name,
166 int klassAccess,
167 String superKlass,
168 String fieldType,
169 int fieldAccess) {
170 return createTestVccClassBytes(name, klassAccess, true, superKlass, fieldType, fieldAccess);
171 }
172
173 byte[] createTestVccClassBytes(String name,
174 int klassAccess,
175 boolean vccAnnotation,
176 String superKlass,
177 String fieldType,
178 int fieldAccess) {
179 ClassWriter cw = new ClassWriter(0);
180 cw.visit(52, klassAccess, name, null, superKlass, null);
181 if (vccAnnotation ) {
182 cw.visitAnnotation("Ljvm/internal/value/ValueCapableClass;", true);
183 }
184 if (fieldType != null) {
185 cw.visitField(fieldAccess, "x", fieldType, null, null);
186 }
187 cw.visitEnd();
188 return cw.toByteArray();
189 }
190
191 Class<?> createTestVccClass(String name,
192 int klassAccess,
193 String superKlass,
194 String fieldType,
195 int fieldAccess) throws ClassNotFoundException {
196 return new TestClassLoader(name,
197 createTestVccClassBytes(name, klassAccess, superKlass, fieldType, fieldAccess)).loadClass(name);
198 }
199
200 class TestClassLoader extends ClassLoader {
201
202 HashMap<String, byte[]> namedBytes = new HashMap<>();
|
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 package runtime.valhalla.valuetypes;
24
25 import jdk.incubator.mvt.ValueType;
26 import java.lang.reflect.Field;
27 import java.lang.reflect.InvocationTargetException;
28 import java.lang.reflect.Method;
29 import static java.lang.reflect.Modifier.*;
30 import java.net.URL;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33
34 import jdk.experimental.bytecode.*;
35
36 import jdk.internal.org.objectweb.asm.*;
37 import static jdk.internal.org.objectweb.asm.Opcodes.*;
38
39 import static jdk.test.lib.Asserts.*;
40
41 /*
42 * @test DeriveValueTypeCreation
43 * @summary Derive Value Type creation test
44 * @library /test/lib
45 * @compile DeriveValueTypeCreation.java
46 * @modules java.base/jdk.experimental.bytecode
47 * java.base/jdk.internal.org.objectweb.asm
48 * jdk.incubator.mvt
49 * @build runtime.valhalla.valuetypes.ValueCapableClass
50 * @run main/othervm -Xint -XX:+EnableMVT runtime.valhalla.valuetypes.DeriveValueTypeCreation
51 * @run main/othervm -Xcomp -XX:+EnableMVT runtime.valhalla.valuetypes.DeriveValueTypeCreation
52 */
53 public class DeriveValueTypeCreation {
54
55 public static final String VCC_CLASSNAME = "runtime.valhalla.valuetypes.ValueCapableClass";
56 public static final String DVT_SUFFIX = "$Value";
57
58 public static final int TEST_DEF_CLASS_ACCESS = ACC_SUPER | ACC_PUBLIC | ACC_FINAL;
59 public static final int TEST_DEF_FIELD_ACCESS = ACC_PUBLIC | ACC_FINAL;
60 public static final String OBJECT_CLASS_DESC = "java/lang/Object";
61
62 public static void main(String[] args) {
63 DeriveValueTypeCreation test = new DeriveValueTypeCreation();
64 test.run();
65 }
66
67 public void run() {
68 loadAndRunTest();
81 catch (Throwable t) { fail("Failed to invoke VCC.test()", t); }
82
83 checkValueCapableClass(clazz);
84 }
85
86 void checkValueCapableClass(Class<?> clazz) {
87 if (!ValueType.classHasValueType(clazz)) {
88 fail("!classHasValueType: " + clazz);
89 }
90
91 ValueType<?> vt = ValueType.forClass(clazz);
92 if (vt == null) {
93 fail("ValueType.forClass failed");
94 }
95
96 System.out.println("ValueType: " + vt);
97
98 if (vt.boxClass() != clazz) {
99 fail("ValueType.boxClass() failed");
100 }
101
102 // DVT class matches our expectations for the current implementation...
103 Class<?> vtClass = vt.valueClass();
104 if (!vtClass.getName().equals(clazz.getName() + DVT_SUFFIX)) {
105 fail("ValueType.valueClass() failed");
106 }
107 if (!vtClass.getSuperclass().getName().equals("java.lang.__Value")) {
108 fail("ValueType.valueClass() isn't a Value Type class");
109 }
110
111 // Exercise "Class.getSimpleName()", we've cause problems with it before
112 String sn = vtClass.getSimpleName();
113 System.out.println("SimpleName: " + sn);
114
115 if (clazz.getClassLoader() != vtClass.getClassLoader()) {
116 fail("ClassLoader mismatch");
117 }
118 if (clazz.getProtectionDomain() != vtClass.getProtectionDomain()) {
119 fail("ProtectionDomain mismatch");
120 }
161 return createTestVccClassBytes(name, TEST_DEF_CLASS_ACCESS, vccAnnotation, OBJECT_CLASS_DESC, "I", TEST_DEF_FIELD_ACCESS);
162 }
163
164 byte[] createTestVccClassBytes(String name,
165 int klassAccess,
166 String superKlass,
167 String fieldType,
168 int fieldAccess) {
169 return createTestVccClassBytes(name, klassAccess, true, superKlass, fieldType, fieldAccess);
170 }
171
172 byte[] createTestVccClassBytes(String name,
173 int klassAccess,
174 boolean vccAnnotation,
175 String superKlass,
176 String fieldType,
177 int fieldAccess) {
178 ClassWriter cw = new ClassWriter(0);
179 cw.visit(52, klassAccess, name, null, superKlass, null);
180 if (vccAnnotation ) {
181 cw.visitAnnotation("Ljdk/incubator/mvt/ValueCapableClass;", true);
182 }
183 if (fieldType != null) {
184 cw.visitField(fieldAccess, "x", fieldType, null, null);
185 }
186 cw.visitEnd();
187 return cw.toByteArray();
188 }
189
190 Class<?> createTestVccClass(String name,
191 int klassAccess,
192 String superKlass,
193 String fieldType,
194 int fieldAccess) throws ClassNotFoundException {
195 return new TestClassLoader(name,
196 createTestVccClassBytes(name, klassAccess, superKlass, fieldType, fieldAccess)).loadClass(name);
197 }
198
199 class TestClassLoader extends ClassLoader {
200
201 HashMap<String, byte[]> namedBytes = new HashMap<>();
|