Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 1 | // Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | package invokecustom; |
| 5 | |
| 6 | import java.io.FileInputStream; |
| 7 | import java.io.FileOutputStream; |
| 8 | import java.io.IOException; |
| 9 | import java.lang.invoke.CallSite; |
| 10 | import java.lang.invoke.MethodHandle; |
| 11 | import java.lang.invoke.MethodHandles; |
| 12 | import java.lang.invoke.MethodType; |
| 13 | import java.nio.file.Path; |
| 14 | import java.nio.file.Paths; |
| 15 | import org.objectweb.asm.ClassReader; |
| 16 | import org.objectweb.asm.ClassVisitor; |
| 17 | import org.objectweb.asm.ClassWriter; |
| 18 | import org.objectweb.asm.Handle; |
| 19 | import org.objectweb.asm.MethodVisitor; |
| 20 | import org.objectweb.asm.Opcodes; |
| 21 | import org.objectweb.asm.Type; |
| 22 | |
| 23 | public class TestGenerator { |
| 24 | |
| 25 | private final Path classNamePath; |
| 26 | |
| 27 | public static void main(String[] args) throws IOException { |
| 28 | assert args.length == 1; |
| 29 | TestGenerator testGenerator = new TestGenerator(Paths.get(args[0], |
| 30 | TestGenerator.class.getPackage().getName(), InvokeCustom.class.getSimpleName() + ".class")); |
| 31 | testGenerator.generateTests(); |
| 32 | } |
| 33 | |
| 34 | public TestGenerator(Path classNamePath) { |
| 35 | this.classNamePath = classNamePath; |
| 36 | } |
| 37 | |
| 38 | private void generateTests() throws IOException { |
| 39 | ClassReader cr = new ClassReader(new FileInputStream(classNamePath.toFile())); |
| 40 | ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); |
| 41 | cr.accept( |
Alan Leung | ef6ba68 | 2017-10-02 19:39:13 +0000 | [diff] [blame] | 42 | new ClassVisitor(Opcodes.ASM6, cw) { |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 43 | @Override |
| 44 | public void visitEnd() { |
| 45 | generateMethodTest1(cw); |
| 46 | generateMethodTest2(cw); |
| 47 | generateMethodTest3(cw); |
| 48 | generateMethodTest4(cw); |
| 49 | generateMethodTest5(cw); |
| 50 | generateMethodTest6(cw); |
| 51 | generateMethodTest7(cw); |
| 52 | generateMethodTest8(cw); |
| 53 | generateMethodTest9(cw); |
| 54 | generateMethodTest10(cw); |
| 55 | generateMethodTest11(cw); |
| 56 | generateMethodTest12(cw); |
| 57 | generateMethodTest13(cw); |
| 58 | generateMethodMain(cw); |
| 59 | super.visitEnd(); |
| 60 | } |
| 61 | }, 0); |
| 62 | new FileOutputStream(classNamePath.toFile()).write(cw.toByteArray()); |
| 63 | } |
| 64 | |
| 65 | /* generate main method that only call all test methods. */ |
| 66 | private void generateMethodMain(ClassVisitor cv) { |
| 67 | MethodVisitor mv = cv.visitMethod( |
| 68 | Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); |
| 69 | mv.visitMethodInsn( |
| 70 | Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test1", "()V", false); |
| 71 | mv.visitMethodInsn( |
| 72 | Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test2", "()V", false); |
| 73 | mv.visitMethodInsn( |
| 74 | Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test3", "()V", false); |
| 75 | mv.visitMethodInsn( |
| 76 | Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test4", "()V", false); |
| 77 | mv.visitMethodInsn( |
| 78 | Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test5", "()V", false); |
| 79 | mv.visitMethodInsn( |
| 80 | Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test6", "()V", false); |
| 81 | mv.visitMethodInsn( |
| 82 | Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test7", "()V", false); |
| 83 | mv.visitMethodInsn( |
| 84 | Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test8", "()V", false); |
| 85 | mv.visitMethodInsn( |
| 86 | Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test9", "()V", false); |
| 87 | mv.visitMethodInsn( |
| 88 | Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test10", "()V", false); |
| 89 | mv.visitMethodInsn( |
| 90 | Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test11", "()V", false); |
| 91 | mv.visitMethodInsn( |
| 92 | Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test12", "()V", false); |
| 93 | mv.visitMethodInsn( |
| 94 | Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test13", "()V", false); |
| 95 | mv.visitInsn(Opcodes.RETURN); |
| 96 | mv.visitMaxs(-1, -1); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Generate test with an invokedynamic, a static bootstrap method without extra args and no arg |
| 101 | * to the target method. |
| 102 | */ |
| 103 | private void generateMethodTest1(ClassVisitor cv) { |
| 104 | MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test1", "()V", |
| 105 | null, null); |
| 106 | MethodType mt = |
| 107 | MethodType.methodType( |
| 108 | CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class); |
| 109 | Handle bootstrap = new Handle( Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class), |
| 110 | "bsmLookupStatic", mt.toMethodDescriptorString(), false); |
| 111 | mv.visitInvokeDynamicInsn("targetMethodTest1", "()V", bootstrap); |
| 112 | mv.visitInsn(Opcodes.RETURN); |
| 113 | mv.visitMaxs(-1, -1); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Generate test with an invokedynamic, a static bootstrap method without extra args and |
| 118 | * args to the target method. |
| 119 | */ |
| 120 | private void generateMethodTest2(ClassVisitor cv) { |
| 121 | MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test2", "()V", |
| 122 | null, null); |
| 123 | MethodType mt = MethodType.methodType( |
| 124 | CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class); |
| 125 | Handle bootstrap = new Handle( Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class), |
| 126 | "bsmLookupStatic", mt.toMethodDescriptorString(), false); |
| 127 | mv.visitLdcInsn(new Boolean(true)); |
| 128 | mv.visitLdcInsn(new Byte((byte) 127)); |
| 129 | mv.visitLdcInsn(new Character('c')); |
| 130 | mv.visitLdcInsn(new Short((short) 1024)); |
| 131 | mv.visitLdcInsn(new Integer(123456)); |
| 132 | mv.visitLdcInsn(new Float(1.2f)); |
| 133 | mv.visitLdcInsn(new Long(123456789)); |
| 134 | mv.visitLdcInsn(new Double(3.5123456789)); |
| 135 | mv.visitLdcInsn("String"); |
| 136 | mv.visitInvokeDynamicInsn("targetMethodTest2", "(ZBCSIFJDLjava/lang/String;)V", bootstrap); |
| 137 | mv.visitInsn(Opcodes.RETURN); |
| 138 | mv.visitMaxs(-1, -1); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Generate test with an invokedynamic, a static bootstrap method with extra args and no arg |
| 143 | * to the target method. |
| 144 | */ |
| 145 | private void generateMethodTest3(ClassVisitor cv) { |
| 146 | MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test3", "()V", |
| 147 | null, null); |
| 148 | MethodType mt = MethodType.methodType( |
| 149 | CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class, int.class, |
| 150 | long.class, float.class, double.class); |
| 151 | Handle bootstrap = new Handle( Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class), |
| 152 | "bsmLookupStaticWithExtraArgs", mt.toMethodDescriptorString(), false); |
| 153 | mv.visitInvokeDynamicInsn("targetMethodTest3", "()V", bootstrap, new Integer(1), |
| 154 | new Long(123456789), new Float(123.456), new Double(123456.789123)); |
| 155 | mv.visitInsn(Opcodes.RETURN); |
| 156 | mv.visitMaxs(-1, -1); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a |
| 161 | * MethodHandle of kind invokespecial. |
| 162 | */ |
| 163 | private void generateMethodTest4(ClassVisitor cv) { |
| 164 | MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test4", "()V", |
| 165 | null, null); |
| 166 | MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, |
| 167 | MethodType.class, MethodHandle.class); |
| 168 | Handle bootstrap = new Handle( Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class), |
| 169 | "bsmCreateCallSite", mt.toMethodDescriptorString(), false); |
| 170 | mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(InvokeCustom.class)); |
| 171 | mv.visitInsn(Opcodes.DUP); |
| 172 | mv.visitMethodInsn( |
| 173 | Opcodes.INVOKESPECIAL, Type.getInternalName(InvokeCustom.class), "<init>", "()V", false); |
| 174 | mv.visitInvokeDynamicInsn("targetMethodTest5", "(Linvokecustom/InvokeCustom;)V", bootstrap, |
| 175 | new Handle( Opcodes.H_INVOKESPECIAL, Type.getInternalName(Super.class), |
| 176 | "targetMethodTest5", "()V", false)); |
| 177 | mv.visitInsn(Opcodes.RETURN); |
| 178 | mv.visitMaxs(-1, -1); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a |
| 183 | * MethodHandle of kind invoke interface. The target method is a default method into an interface |
| 184 | * that shadows another default method from a super interface. |
| 185 | */ |
| 186 | private void generateMethodTest5(ClassVisitor cv) { |
| 187 | MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test5", "()V", |
| 188 | null, null); |
| 189 | MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, |
| 190 | MethodType.class, MethodHandle.class); |
| 191 | Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class), |
mikaelpeltier | a05d3d5 | 2017-08-21 15:03:22 +0200 | [diff] [blame] | 192 | "bsmCreateCallSite", mt.toMethodDescriptorString(), false); |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 193 | mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(InvokeCustom.class)); |
| 194 | mv.visitInsn(Opcodes.DUP); |
| 195 | mv.visitMethodInsn( |
| 196 | Opcodes.INVOKESPECIAL, Type.getInternalName(InvokeCustom.class), "<init>", "()V", false); |
| 197 | mv.visitInvokeDynamicInsn("targetMethodTest6", "(Linvokecustom/I;)V", bootstrap, |
| 198 | new Handle(Opcodes.H_INVOKEINTERFACE, Type.getInternalName(I.class), |
| 199 | "targetMethodTest6", "()V", true)); |
| 200 | mv.visitInsn(Opcodes.RETURN); |
| 201 | mv.visitMaxs(-1, -1); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a |
| 206 | * MethodHandle of kind invoke interface. The target method is a default method into an interface |
| 207 | * that is at the end of a chain of interfaces. |
| 208 | */ |
| 209 | private void generateMethodTest6(ClassVisitor cv) { |
| 210 | MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test6", "()V", |
| 211 | null, null); |
| 212 | MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, |
| 213 | MethodType.class, MethodHandle.class); |
| 214 | Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class), |
mikaelpeltier | a05d3d5 | 2017-08-21 15:03:22 +0200 | [diff] [blame] | 215 | "bsmCreateCallSite", mt.toMethodDescriptorString(), false); |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 216 | mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(InvokeCustom.class)); |
| 217 | mv.visitInsn(Opcodes.DUP); |
| 218 | mv.visitMethodInsn( |
| 219 | Opcodes.INVOKESPECIAL, Type.getInternalName(InvokeCustom.class), "<init>", "()V", false); |
| 220 | mv.visitInvokeDynamicInsn("targetMethodTest7", "(Linvokecustom/J;)V", bootstrap, |
| 221 | new Handle(Opcodes.H_INVOKEINTERFACE, Type.getInternalName(J.class), |
| 222 | "targetMethodTest7", "()V", true)); |
| 223 | mv.visitInsn(Opcodes.RETURN); |
| 224 | mv.visitMaxs(-1, -1); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a |
| 229 | * MethodHandle of kind invoke interface. The target method is a method into an interface |
| 230 | * that is shadowed by another definition into a sub interfaces. |
| 231 | */ |
| 232 | private void generateMethodTest7(ClassVisitor cv) { |
| 233 | MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test7", "()V", |
| 234 | null, null); |
| 235 | MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, |
| 236 | MethodType.class, MethodHandle.class); |
| 237 | Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class), |
mikaelpeltier | a05d3d5 | 2017-08-21 15:03:22 +0200 | [diff] [blame] | 238 | "bsmCreateCallSite", mt.toMethodDescriptorString(), false); |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 239 | mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(InvokeCustom.class)); |
| 240 | mv.visitInsn(Opcodes.DUP); |
| 241 | mv.visitMethodInsn( |
| 242 | Opcodes.INVOKESPECIAL, Type.getInternalName(InvokeCustom.class), "<init>", "()V", false); |
| 243 | mv.visitInvokeDynamicInsn("targetMethodTest8", "(Linvokecustom/J;)V", bootstrap, |
| 244 | new Handle(Opcodes.H_INVOKEINTERFACE, Type.getInternalName(J.class), |
| 245 | "targetMethodTest8", "()V", true)); |
| 246 | mv.visitInsn(Opcodes.RETURN); |
| 247 | mv.visitMaxs(-1, -1); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a |
| 252 | * MethodHandle of kind invoke virtual. The target method is a method into an interface that is |
| 253 | * not shadowed by an implementation into a classes implementing the interface. |
| 254 | */ |
| 255 | private void generateMethodTest8(ClassVisitor cv) { |
| 256 | MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test8", "()V", |
| 257 | null, null); |
| 258 | MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, |
| 259 | MethodType.class, MethodHandle.class); |
| 260 | Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class), |
mikaelpeltier | a05d3d5 | 2017-08-21 15:03:22 +0200 | [diff] [blame] | 261 | "bsmCreateCallSite", mt.toMethodDescriptorString(), false); |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 262 | mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(InvokeCustom.class)); |
| 263 | mv.visitInsn(Opcodes.DUP); |
| 264 | mv.visitMethodInsn( |
| 265 | Opcodes.INVOKESPECIAL, Type.getInternalName(InvokeCustom.class), "<init>", "()V", false); |
| 266 | mv.visitInvokeDynamicInsn("targetMethodTest9", "(Linvokecustom/InvokeCustom;)V", bootstrap, |
| 267 | new Handle(Opcodes.H_INVOKEVIRTUAL, Type.getInternalName(InvokeCustom.class), |
| 268 | "targetMethodTest9", "()V", false)); |
| 269 | mv.visitInsn(Opcodes.RETURN); |
| 270 | mv.visitMaxs(-1, -1); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a |
| 275 | * MethodHandle of kind invoke virtual. The target method is a method into a class implementing |
| 276 | * an abstract method and that shadows a default method from an interface. |
| 277 | */ |
| 278 | private void generateMethodTest9(ClassVisitor cv) { |
| 279 | MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test9", "()V", |
| 280 | null, null); |
| 281 | MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, |
| 282 | MethodType.class, MethodHandle.class); |
| 283 | Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class), |
mikaelpeltier | a05d3d5 | 2017-08-21 15:03:22 +0200 | [diff] [blame] | 284 | "bsmCreateCallSite", mt.toMethodDescriptorString(), false); |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 285 | mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(InvokeCustom.class)); |
| 286 | mv.visitInsn(Opcodes.DUP); |
| 287 | mv.visitMethodInsn( |
| 288 | Opcodes.INVOKESPECIAL, Type.getInternalName(InvokeCustom.class), "<init>", "()V", false); |
| 289 | mv.visitInvokeDynamicInsn("targetMethodTest10", "(Linvokecustom/InvokeCustom;)V", bootstrap, |
| 290 | new Handle(Opcodes.H_INVOKEVIRTUAL, Type.getInternalName(InvokeCustom.class), |
| 291 | "targetMethodTest10", "()V", false)); |
| 292 | mv.visitInsn(Opcodes.RETURN); |
| 293 | mv.visitMaxs(-1, -1); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a |
| 298 | * MethodHandle of kind get static. The method handle read a static field from a class. |
| 299 | */ |
| 300 | private void generateMethodTest10(ClassVisitor cv) { |
| 301 | MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test10", "()V", |
| 302 | null, null); |
| 303 | MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, |
| 304 | MethodType.class, MethodHandle.class); |
| 305 | Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class), |
mikaelpeltier | a05d3d5 | 2017-08-21 15:03:22 +0200 | [diff] [blame] | 306 | "bsmCreateCallSite", mt.toMethodDescriptorString(), false); |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 307 | mv.visitFieldInsn(Opcodes.GETSTATIC, |
| 308 | "java/lang/System", |
| 309 | "out", |
| 310 | "Ljava/io/PrintStream;"); |
| 311 | mv.visitInvokeDynamicInsn("staticField1", "()Ljava/lang/String;", bootstrap, |
| 312 | new Handle(Opcodes.H_GETSTATIC, Type.getInternalName(InvokeCustom.class), |
| 313 | "staticField1", "Ljava/lang/String;", false)); |
| 314 | mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, |
| 315 | "java/io/PrintStream", |
| 316 | "println", |
| 317 | "(Ljava/lang/String;)V", false); |
| 318 | mv.visitInsn(Opcodes.RETURN); |
| 319 | mv.visitMaxs(-1, -1); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a |
| 324 | * MethodHandle of kind put static. The method handle write a static field in a class and then |
| 325 | * print its value. |
| 326 | */ |
| 327 | private void generateMethodTest11(ClassVisitor cv) { |
| 328 | MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test11", "()V", |
| 329 | null, null); |
| 330 | MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, |
| 331 | MethodType.class, MethodHandle.class); |
| 332 | Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class), |
mikaelpeltier | a05d3d5 | 2017-08-21 15:03:22 +0200 | [diff] [blame] | 333 | "bsmCreateCallSite", mt.toMethodDescriptorString(), false); |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 334 | mv.visitLdcInsn("Write static field"); |
| 335 | mv.visitInvokeDynamicInsn("staticField1", "(Ljava/lang/String;)V", bootstrap, |
| 336 | new Handle(Opcodes.H_PUTSTATIC, Type.getInternalName(InvokeCustom.class), |
| 337 | "staticField1", "Ljava/lang/String;", false)); |
| 338 | mv.visitFieldInsn(Opcodes.GETSTATIC, |
| 339 | "java/lang/System", |
| 340 | "out", |
| 341 | "Ljava/io/PrintStream;"); |
| 342 | mv.visitFieldInsn(Opcodes.GETSTATIC, |
| 343 | Type.getInternalName(InvokeCustom.class), |
| 344 | "staticField1", |
| 345 | "Ljava/lang/String;"); |
| 346 | mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, |
| 347 | "java/io/PrintStream", |
| 348 | "println", |
| 349 | "(Ljava/lang/String;)V", false); |
| 350 | mv.visitInsn(Opcodes.RETURN); |
| 351 | mv.visitMaxs(-1, -1); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a |
| 356 | * MethodHandle of kind get instance. The method handle read an instance field from a class. |
| 357 | */ |
| 358 | private void generateMethodTest12(ClassVisitor cv) { |
| 359 | MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test12", "()V", |
| 360 | null, null); |
| 361 | MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, |
| 362 | MethodType.class, MethodHandle.class); |
| 363 | Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class), |
mikaelpeltier | a05d3d5 | 2017-08-21 15:03:22 +0200 | [diff] [blame] | 364 | "bsmCreateCallSite", mt.toMethodDescriptorString(), false); |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 365 | mv.visitFieldInsn(Opcodes.GETSTATIC, |
| 366 | "java/lang/System", |
| 367 | "out", |
| 368 | "Ljava/io/PrintStream;"); |
| 369 | mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(InvokeCustom.class)); |
| 370 | mv.visitInsn(Opcodes.DUP); |
| 371 | mv.visitMethodInsn( |
| 372 | Opcodes.INVOKESPECIAL, Type.getInternalName(InvokeCustom.class), "<init>", "()V", false); |
| 373 | mv.visitInvokeDynamicInsn("instanceField1", "(Linvokecustom/InvokeCustom;)Ljava/lang/String;", |
| 374 | bootstrap, new Handle(Opcodes.H_GETFIELD, Type.getInternalName(InvokeCustom.class), |
| 375 | "instanceField1", "Ljava/lang/String;", false)); |
| 376 | mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, |
| 377 | "java/io/PrintStream", |
| 378 | "println", |
| 379 | "(Ljava/lang/String;)V", false); |
| 380 | mv.visitInsn(Opcodes.RETURN); |
| 381 | mv.visitMaxs(-1, -1); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a |
| 386 | * MethodHandle of kind put instance. The method handle write an instance field in a class and |
| 387 | * then print its value. |
| 388 | */ |
| 389 | private void generateMethodTest13(ClassVisitor cv) { |
| 390 | MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test13", "()V", |
| 391 | null, null); |
| 392 | MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, |
| 393 | MethodType.class, MethodHandle.class); |
| 394 | Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class), |
mikaelpeltier | a05d3d5 | 2017-08-21 15:03:22 +0200 | [diff] [blame] | 395 | "bsmCreateCallSite", mt.toMethodDescriptorString(), false); |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 396 | mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(InvokeCustom.class)); |
| 397 | mv.visitInsn(Opcodes.DUP); |
| 398 | mv.visitMethodInsn( |
| 399 | Opcodes.INVOKESPECIAL, Type.getInternalName(InvokeCustom.class), "<init>", "()V", false); |
| 400 | mv.visitVarInsn(Opcodes.ASTORE, 0); |
| 401 | mv.visitVarInsn(Opcodes.ALOAD, 0); |
| 402 | mv.visitLdcInsn("Write instance field"); |
| 403 | mv.visitInvokeDynamicInsn("instanceField1", "(Linvokecustom/InvokeCustom;Ljava/lang/String;)V", |
| 404 | bootstrap, new Handle(Opcodes.H_PUTFIELD, Type.getInternalName(InvokeCustom.class), |
| 405 | "instanceField1", "Ljava/lang/String;", false)); |
| 406 | mv.visitFieldInsn(Opcodes.GETSTATIC, |
| 407 | "java/lang/System", |
| 408 | "out", |
| 409 | "Ljava/io/PrintStream;"); |
| 410 | mv.visitVarInsn(Opcodes.ALOAD, 0); |
| 411 | mv.visitFieldInsn(Opcodes.GETFIELD, |
| 412 | Type.getInternalName(InvokeCustom.class), |
| 413 | "instanceField1", |
| 414 | "Ljava/lang/String;"); |
| 415 | mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, |
| 416 | "java/io/PrintStream", |
| 417 | "println", |
| 418 | "(Ljava/lang/String;)V", false); |
| 419 | mv.visitInsn(Opcodes.RETURN); |
| 420 | mv.visitMaxs(-1, -1); |
| 421 | } |
| 422 | } |