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