blob: b08441c398f06234e7eb33bded53763732bde372 [file] [log] [blame]
Mikaël Peltier7b7b53a2017-10-09 13:33:21 +02001// 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.
4package invokecustom;
5
Rico Windc0016d12017-10-26 13:39:32 +02006
Mikaël Peltier7b7b53a2017-10-09 13:33:21 +02007import java.io.IOException;
Rico Windc0016d12017-10-26 13:39:32 +02008import java.io.InputStream;
9import java.io.OutputStream;
Mikaël Peltier7b7b53a2017-10-09 13:33:21 +020010import java.lang.invoke.CallSite;
Mikaël Peltier7b7b53a2017-10-09 13:33:21 +020011import java.lang.invoke.MethodHandles;
12import java.lang.invoke.MethodType;
Rico Windc0016d12017-10-26 13:39:32 +020013import java.nio.file.Files;
Mikaël Peltier7b7b53a2017-10-09 13:33:21 +020014import java.nio.file.Path;
15import java.nio.file.Paths;
16import org.objectweb.asm.ClassReader;
17import org.objectweb.asm.ClassVisitor;
18import org.objectweb.asm.ClassWriter;
19import org.objectweb.asm.Handle;
20import org.objectweb.asm.MethodVisitor;
21import org.objectweb.asm.Opcodes;
22import org.objectweb.asm.Type;
23
24public class TestGenerator {
25
26 private final Path classNamePath;
27
28 public static void main(String[] args) throws IOException {
29 assert args.length == 1;
30 TestGenerator testGenerator = new TestGenerator(Paths.get(args[0],
31 TestGenerator.class.getPackage().getName(), InvokeCustom.class.getSimpleName() + ".class"));
32 testGenerator.generateTests();
33 }
34
35 public TestGenerator(Path classNamePath) {
36 this.classNamePath = classNamePath;
37 }
38
39 private void generateTests() throws IOException {
Rico Windc0016d12017-10-26 13:39:32 +020040 try (InputStream inputStream = Files.newInputStream(classNamePath)) {
41 ClassReader cr = new ClassReader(inputStream);
42 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
43 cr.accept(
clementbera6c91c652019-04-15 17:28:42 +020044 new ClassVisitor(Opcodes.ASM7, cw) {
Rico Windc0016d12017-10-26 13:39:32 +020045 @Override
46 public void visitEnd() {
47 generateMethodTest1(cw);
48 generateMethodTest2(cw);
Mads Agerab4dc702018-09-24 09:43:52 +020049 generateMethodTest3(cw);
50 generateMethodTest4(cw);
Rico Windc0016d12017-10-26 13:39:32 +020051 generateMethodMain(cw);
52 super.visitEnd();
53 }
54 }, 0);
55 try (OutputStream output = Files.newOutputStream(classNamePath)) {
56 output.write(cw.toByteArray());
57 }
58 }
Mikaël Peltier7b7b53a2017-10-09 13:33:21 +020059 }
60
61 /* Generate main method that only call all test methods. */
62 private void generateMethodMain(ClassVisitor cv) {
63 MethodVisitor mv = cv.visitMethod(
64 Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
65 mv.visitMethodInsn(
66 Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test1", "()V", false);
Mikaël Peltiercfd6dac2017-10-10 13:45:55 +020067 mv.visitMethodInsn(
68 Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test2", "()V", false);
Mads Agerab4dc702018-09-24 09:43:52 +020069 mv.visitMethodInsn(
70 Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test3", "()V", false);
71 mv.visitMethodInsn(
72 Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test4", "()V", false);
Mikaël Peltier7b7b53a2017-10-09 13:33:21 +020073 mv.visitInsn(Opcodes.RETURN);
74 mv.visitMaxs(-1, -1);
75 }
76
77 /**
Mads Agerab4dc702018-09-24 09:43:52 +020078 * Generate test with an invokedynamic, a static bootstrap method without extra args and
79 * args to the target method.
Mikaël Peltier7b7b53a2017-10-09 13:33:21 +020080 */
81 private void generateMethodTest1(ClassVisitor cv) {
82 MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test1", "()V",
83 null, null);
84 MethodType mt = MethodType.methodType(
85 CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class);
Mads Agerab4dc702018-09-24 09:43:52 +020086 Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class),
Mikaël Peltier7b7b53a2017-10-09 13:33:21 +020087 "bsmLookupStatic", mt.toMethodDescriptorString(), false);
88 mv.visitLdcInsn(new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class),
89 "targetMethodTest1", "()V", false));
90 mv.visitLdcInsn(new Handle(Opcodes.H_GETSTATIC, Type.getInternalName(InvokeCustom.class),
91 "staticField1", "Ljava/lang/String;", false));
92 mv.visitInvokeDynamicInsn("targetMethodTest2",
93 "(Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;)V",
94 bootstrap);
95 mv.visitInsn(Opcodes.RETURN);
96 mv.visitMaxs(-1, -1);
97 }
Mikaël Peltiercfd6dac2017-10-10 13:45:55 +020098
99 /**
Mads Agerab4dc702018-09-24 09:43:52 +0200100 * Generate test with an invokedynamic, a static bootstrap method without extra args and
101 * args to the target method.
Mikaël Peltiercfd6dac2017-10-10 13:45:55 +0200102 */
103 private void generateMethodTest2(ClassVisitor cv) {
104 MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test2", "()V",
105 null, null);
106 MethodType mt = MethodType.methodType(
107 CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class);
Mads Agerab4dc702018-09-24 09:43:52 +0200108 Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class),
Mikaël Peltiercfd6dac2017-10-10 13:45:55 +0200109 "bsmLookupStatic", mt.toMethodDescriptorString(), false);
110 mv.visitLdcInsn(Type.getMethodType("(ZBSCIFJDLjava/lang/String;)Ljava/lang/Object;"));
111 mv.visitInvokeDynamicInsn("targetMethodTest3", "(Ljava/lang/invoke/MethodType;)V",
112 bootstrap);
113 mv.visitInsn(Opcodes.RETURN);
114 mv.visitMaxs(-1, -1);
115 }
Mads Agerab4dc702018-09-24 09:43:52 +0200116
117 /**
118 * Generate test with a const method handle pointing to the middle of a class hierarchy.
119 * Call a static method with the method handle which will do a MethodHandle.invoke call on
120 * a sub class instance.
121 *
122 * Tests that the const method handle is rewritten when renaming. Also tests that the
123 * middle class does not disappear (for instance via class merging).
124 */
125 private void generateMethodTest3(ClassVisitor cv) {
126 MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test3", "()V",
127 null, null);
128 MethodType mt = MethodType.methodType(ReturnType.class, ArgumentType.class);
129 Handle invokeWithArg = new Handle(
130 Opcodes.H_INVOKEVIRTUAL,
131 Type.getInternalName(Middle.class),
132 "targetMethodTest4",
133 mt.toMethodDescriptorString(),
134 false);
135 mv.visitLdcInsn(invokeWithArg);
136 mv.visitMethodInsn(
137 Opcodes.INVOKESTATIC,
138 Type.getInternalName(InvokeCustom.class),
139 "doInvokeSubWithArg",
140 "(Ljava/lang/invoke/MethodHandle;)V",
141 false);
142 mv.visitInsn(Opcodes.RETURN);
143 mv.visitMaxs(-1, -1);
144 }
145
146 /**
147 * Generate test with a const method handle pointing to a class which inherits the method from
148 * the super class. Call a static method with the method handle which will do a
149 * MethodHandle.invokeExact.
150 */
151 private void generateMethodTest4(ClassVisitor cv) {
152 MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test4", "()V",
153 null, null);
154 MethodType mt = MethodType.methodType(ReturnType.class, ArgumentType.class);
155 Handle invokeExactWithArg = new Handle(
156 Opcodes.H_INVOKEVIRTUAL,
157 Type.getInternalName(Impl.class),
158 "targetMethodTest5",
159 mt.toMethodDescriptorString(),
160 false);
161 mv.visitLdcInsn(invokeExactWithArg);
162 mv.visitMethodInsn(
163 Opcodes.INVOKESTATIC,
164 Type.getInternalName(InvokeCustom.class),
165 "doInvokeExactImplWithArg",
166 "(Ljava/lang/invoke/MethodHandle;)V",
167 false);
168 mv.visitInsn(Opcodes.RETURN);
169 mv.visitMaxs(-1, -1);
170 }
Mikaël Peltier7b7b53a2017-10-09 13:33:21 +0200171}