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 package test.java.lang.invoke.lib;
25
26 import java.lang.classfile.ClassBuilder;
27 import java.lang.classfile.ClassFile;
28 import java.lang.classfile.TypeKind;
29
30 import java.lang.constant.*;
31 import java.lang.invoke.MethodHandle;
32 import java.lang.invoke.MethodHandles;
33 import java.lang.invoke.MethodType;
34 import java.util.concurrent.atomic.AtomicInteger;
35
36 import static java.lang.invoke.MethodType.fromMethodDescriptorString;
37
38 public class InstructionHelper {
39
40 static final AtomicInteger COUNT = new AtomicInteger();
41
42 private static void commonBuild(ClassBuilder classBuilder) {
43 classBuilder
44 .withVersion(55, 0)
45 .withSuperclass(ConstantDescs.CD_Object)
46 .withMethod(ConstantDescs.INIT_NAME, ConstantDescs.MTD_void, ClassFile.ACC_PUBLIC,
47 methodBuilder -> methodBuilder
48 .withCode(codeBuilder -> codeBuilder
49 .aload(0)
50 .invokespecial(ConstantDescs.CD_Object, ConstantDescs.INIT_NAME,
51 ConstantDescs.MTD_void, false)
52 .return_()));
53 }
54
55 public static MethodHandle invokedynamic(MethodHandles.Lookup l, String name, MethodType type, String bsmMethodName,
56 MethodType bsmType, ConstantDesc... boostrapArgs) throws Exception {
118 bootstrapArgs))
119 .return_(TypeKind.fromDescriptor(type))));
120 });
121 Class<?> gc = l.defineClass(bytes);
122 return l.findStatic(gc, "m", fromMethodDescriptorString(methodType, l.lookupClass().getClassLoader()));
123 }
124
125 public static String csym(Class<?> c) {
126 return c.getCanonicalName().replace('.', '/');
127 }
128
129 public static ClassDesc classDesc(Class<?> c) {
130 return ClassDesc.ofDescriptor(c.descriptorString());
131 }
132
133 public static ClassDesc classDesc(Class<?> c, String suffix) {
134 StringBuilder sb = new StringBuilder(c.descriptorString());
135 String classDescStr = sb.insert(sb.length() - 1, suffix).toString();
136 return ClassDesc.ofDescriptor(classDescStr);
137 }
138 }
|
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 package test.java.lang.invoke.lib;
25
26 import java.lang.classfile.ClassBuilder;
27 import java.lang.classfile.ClassFile;
28 import java.lang.classfile.CodeBuilder;
29 import java.lang.classfile.TypeKind;
30
31 import java.lang.constant.*;
32 import java.lang.invoke.MethodHandle;
33 import java.lang.invoke.MethodHandles;
34 import java.lang.invoke.MethodType;
35 import static java.lang.invoke.MethodType.fromMethodDescriptorString;
36 import java.util.concurrent.atomic.AtomicInteger;
37 import java.util.function.Consumer;
38
39 public class InstructionHelper {
40
41 static final AtomicInteger COUNT = new AtomicInteger();
42
43 private static void commonBuild(ClassBuilder classBuilder) {
44 classBuilder
45 .withVersion(55, 0)
46 .withSuperclass(ConstantDescs.CD_Object)
47 .withMethod(ConstantDescs.INIT_NAME, ConstantDescs.MTD_void, ClassFile.ACC_PUBLIC,
48 methodBuilder -> methodBuilder
49 .withCode(codeBuilder -> codeBuilder
50 .aload(0)
51 .invokespecial(ConstantDescs.CD_Object, ConstantDescs.INIT_NAME,
52 ConstantDescs.MTD_void, false)
53 .return_()));
54 }
55
56 public static MethodHandle invokedynamic(MethodHandles.Lookup l, String name, MethodType type, String bsmMethodName,
57 MethodType bsmType, ConstantDesc... boostrapArgs) throws Exception {
119 bootstrapArgs))
120 .return_(TypeKind.fromDescriptor(type))));
121 });
122 Class<?> gc = l.defineClass(bytes);
123 return l.findStatic(gc, "m", fromMethodDescriptorString(methodType, l.lookupClass().getClassLoader()));
124 }
125
126 public static String csym(Class<?> c) {
127 return c.getCanonicalName().replace('.', '/');
128 }
129
130 public static ClassDesc classDesc(Class<?> c) {
131 return ClassDesc.ofDescriptor(c.descriptorString());
132 }
133
134 public static ClassDesc classDesc(Class<?> c, String suffix) {
135 StringBuilder sb = new StringBuilder(c.descriptorString());
136 String classDescStr = sb.insert(sb.length() - 1, suffix).toString();
137 return ClassDesc.ofDescriptor(classDescStr);
138 }
139
140
141 public static MethodHandle buildMethodHandle(MethodHandles.Lookup l, String methodName, MethodType methodType, Consumer<? super CodeBuilder> builder) {
142 ClassDesc genClassDesc = classDesc(l.lookupClass(), "$Code_" + COUNT.getAndIncrement());
143 return buildMethodHandle(l, genClassDesc, methodName, methodType, builder);
144 }
145
146 public static MethodHandle buildMethodHandle(MethodHandles.Lookup l, ClassDesc classDesc, String methodName, MethodType methodType, Consumer<? super CodeBuilder> builder) {
147 try {
148 byte[] bytes = ClassFile.of().build(classDesc, classBuilder -> {
149 classBuilder.withMethod(methodName,
150 MethodTypeDesc.ofDescriptor(methodType.toMethodDescriptorString()),
151 ClassFile.ACC_PUBLIC + ClassFile.ACC_STATIC,
152 methodBuilder -> methodBuilder.withCode(builder));
153 });
154 Class<?> clazz = l.defineClass(bytes);
155 return l.findStatic(clazz, methodName, methodType);
156 } catch (Throwable t) {
157 t.printStackTrace();
158 throw new RuntimeException("Failed to buildMethodHandle: " + methodName + " type " + methodType);
159 }
160 }
161
162 }
|