rev 55117 : 8223350: [lworld] Use inline classes instead of value classes
1 /*
2 * Copyright (c) 2018, 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 /*
26 * @test
27 * @summary test value bootstrap methods
28 * @modules java.base/jdk.internal.org.objectweb.asm
29 * @compile -XDallowWithFieldOperator ValueBootstrapMethods.java
30 * @run main/othervm -XX:+EnableValhalla -Dvalue.bsm.salt=1 ValueBootstrapMethods
31 */
32
33 import java.io.IOException;
34 import java.lang.invoke.CallSite;
35 import java.lang.invoke.MethodHandles;
36 import java.lang.invoke.MethodType;
37 import java.lang.reflect.*;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.util.HashSet;
42 import java.util.List;
43 import java.util.Objects;
44 import java.util.Set;
45
46 import jdk.internal.org.objectweb.asm.Attribute;
47 import jdk.internal.org.objectweb.asm.ByteVector;
48 import jdk.internal.org.objectweb.asm.ClassReader;
49 import jdk.internal.org.objectweb.asm.ClassWriter;
50 import jdk.internal.org.objectweb.asm.Handle;
51 import jdk.internal.org.objectweb.asm.Label;
52 import jdk.internal.org.objectweb.asm.MethodVisitor;
53 import jdk.internal.org.objectweb.asm.Type;
54 import static jdk.internal.org.objectweb.asm.Opcodes.*;
55
56 public class ValueBootstrapMethods {
57 private static final String TEST_CLASSES = System.getProperty("test.classes", ".");
58
59 public static void main(String... args) throws Throwable {
60 Class<?> test = valueTestClass();
61 Value value = Value.make(10, 5.03, "foo", "bar", "goo");
62
63 Class<?> valueClass = Value.class.asValueType();
64 Method hashCode = test.getMethod("hashCode", valueClass);
65 int hash = (int)hashCode.invoke(null, value);
66 assertEquals(hash, value.hashCode());
67
68 Method toString = test.getMethod("toString", valueClass);
69 String s = (String)toString.invoke(null, value);
70 assertEquals(s, value.toString());
71
72 Method equals = test.getMethod("equals", valueClass, Object.class);
73 boolean rc = (boolean)equals.invoke(null, value, value);
74 if (!rc) {
75 throw new RuntimeException("expected equals");
76 }
77 }
78
79 public static final value class Value {
80 private final int i;
81 private final double d;
82 private final String s;
83 private final List<String> l;
84 Value() {
85 this.i = 0;
86 this.d = 0;
87 this.s = "default";
88 this.l = List.of();
89 }
90 public static Value make(int i, double d, String s, String... items) {
91 Value v = Value.default;
92 v = __WithField(v.i, i);
93 v = __WithField(v.d, d);
94 v = __WithField(v.s, s);
95 v = __WithField(v.l, List.of(items));
96 return v;
97 }
98
99 List<Object> values() {
100 return List.of(Value.class.asValueType(), i, d, s, l);
101 }
102
103 public int hashCode() {
104 return values().hashCode();
105 }
106
107 public String toString() {
108 System.out.println(l);
109 return String.format("[%s i=%s d=%s s=%s l=%s]", Value.class.getName(),
110 i, String.valueOf(d), s, l.toString());
111 }
112 }
113
114 /*
115 * Generate ValueTest class
116 */
117 private static Class<?> valueTestClass() throws Exception {
118 Path path = Paths.get(TEST_CLASSES, "ValueTest.class");
119 generate(Value.class.asValueType(), "ValueTest", path);
120 return Class.forName("ValueTest");
121 }
122
123 private static void assertEquals(Object o1, Object expected) {
124 if (!Objects.equals(o1, expected)) {
125 throw new RuntimeException(o1 + " expected: " + expected);
126 }
127 }
128
129 static final int CLASSFILE_VERSION = 56;
130 static void generate(Class<?> c, String className, Path path) throws IOException {
131 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
132
133 cw.visit(CLASSFILE_VERSION,
134 ACC_SUPER + ACC_PUBLIC + ACC_FINAL + ACC_SYNTHETIC,
135 className,
136 null,
137 "java/lang/Object",
138 null
139 );
140
141
142 MethodType mt = MethodType.methodType(CallSite.class,
143 MethodHandles.Lookup.class, String.class, MethodType.class, Class.class);
144 Handle bootstrap = new Handle(H_INVOKESTATIC, Type.getInternalName(java.lang.invoke.ValueBootstrapMethods.class),
145 "makeBootstrapMethod", mt.toMethodDescriptorString(), false);
146
147 Type type = Type.getType(c);
148 MethodVisitor mv = cw.visitMethod(
149 ACC_PUBLIC + ACC_STATIC + ACC_FINAL,
150 "hashCode",
151 Type.getMethodDescriptor(Type.INT_TYPE, type),
152 null,
153 null);
154
155 mv.visitVarInsn(ALOAD, 0);
156 mv.visitInvokeDynamicInsn("hashCode",
157 Type.getMethodDescriptor(Type.INT_TYPE, type),
158 bootstrap, type);
159 mv.visitInsn(IRETURN);
160
161 mv.visitMaxs(-1, -1);
162 mv.visitEnd();
163
164 mv = cw.visitMethod(
165 ACC_PUBLIC + ACC_STATIC + ACC_FINAL,
166 "equals",
167 Type.getMethodDescriptor(Type.BOOLEAN_TYPE, type, Type.getType(Object.class)),
168 null,
169 null);
170
171 mv.visitVarInsn(ALOAD, 0);
172 mv.visitVarInsn(ALOAD, 1);
173 mv.visitInvokeDynamicInsn("equals",
174 Type.getMethodDescriptor(Type.BOOLEAN_TYPE, type, Type.getType(Object.class)),
175 bootstrap, type);
176 mv.visitInsn(IRETURN);
177 mv.visitMaxs(-1, -1);
178 mv.visitEnd();
179
180 mv = cw.visitMethod(
181 ACC_PUBLIC + ACC_STATIC + ACC_FINAL,
182 "toString",
183 Type.getMethodDescriptor(Type.getType(String.class), type),
184 null,
185 null);
186
187 mv.visitVarInsn(ALOAD, 0);
188 mv.visitInvokeDynamicInsn("toString",
189 Type.getMethodDescriptor(Type.getType(String.class), type),
190 bootstrap, type);
191 mv.visitInsn(ARETURN);
192 mv.visitMaxs(-1, -1);
193 mv.visitEnd();
194
195 cw.visitEnd();
196
197 byte[] classBytes = cw.toByteArray();
198 System.out.println("writing " + path);
199 Files.write(path, classBytes);
200 }
201 }
--- EOF ---