< prev index next >

test/jdk/java/lang/invoke/common/test/java/lang/invoke/lib/InstructionHelper.java

Print this page

  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 package test.java.lang.invoke.lib;
 25 
 26 import jdk.internal.classfile.ClassBuilder;
 27 import jdk.internal.classfile.Classfile;
 28 import jdk.internal.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                                     .returnInstruction(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 }

  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 package test.java.lang.invoke.lib;
 25 
 26 import java.lang.constant.ClassDesc;
 27 import java.lang.constant.ConstantDesc;
 28 import java.lang.constant.ConstantDescs;
 29 import java.lang.constant.DirectMethodHandleDesc;
 30 import java.lang.constant.DynamicCallSiteDesc;
 31 import java.lang.constant.DynamicConstantDesc;
 32 import java.lang.constant.MethodHandleDesc;
 33 import java.lang.constant.MethodTypeDesc;
 34 import java.lang.invoke.MethodHandle;
 35 import java.lang.invoke.MethodHandles;
 36 import java.lang.invoke.MethodType;
 37 import static java.lang.invoke.MethodType.fromMethodDescriptorString;
 38 import java.util.concurrent.atomic.AtomicInteger;
 39 import java.util.function.Consumer;
 40 
 41 import jdk.internal.classfile.ClassBuilder;
 42 import jdk.internal.classfile.Classfile;
 43 import jdk.internal.classfile.CodeBuilder;
 44 import jdk.internal.classfile.TypeKind;
 45 
 46 public class InstructionHelper {
 47 
 48     static final AtomicInteger COUNT = new AtomicInteger();
 49 
 50     private static void commonBuild(ClassBuilder classBuilder) {
 51         classBuilder
 52                 .withVersion(55, 0)
 53                 .withSuperclass(ConstantDescs.CD_Object)
 54                 .withMethod(ConstantDescs.INIT_NAME, ConstantDescs.MTD_void, Classfile.ACC_PUBLIC,
 55                         methodBuilder -> methodBuilder
 56                                 .withCode(codeBuilder -> codeBuilder
 57                                         .aload(0)
 58                                         .invokespecial(ConstantDescs.CD_Object, ConstantDescs.INIT_NAME,
 59                                                 ConstantDescs.MTD_void, false)
 60                                         .return_()));
 61     }
 62 
 63     public static MethodHandle invokedynamic(MethodHandles.Lookup l, String name, MethodType type, String bsmMethodName,
 64                                              MethodType bsmType, ConstantDesc... boostrapArgs) throws Exception {

126                                             bootstrapArgs))
127                                     .returnInstruction(TypeKind.fromDescriptor(type))));
128         });
129         Class<?> gc = l.defineClass(bytes);
130         return l.findStatic(gc, "m", fromMethodDescriptorString(methodType, l.lookupClass().getClassLoader()));
131     }
132 
133     public static String csym(Class<?> c) {
134         return c.getCanonicalName().replace('.', '/');
135     }
136 
137     public static ClassDesc classDesc(Class<?> c) {
138         return ClassDesc.ofDescriptor(c.descriptorString());
139     }
140 
141     public static ClassDesc classDesc(Class<?> c, String suffix) {
142         StringBuilder sb = new StringBuilder(c.descriptorString());
143         String classDescStr = sb.insert(sb.length() - 1, suffix).toString();
144         return ClassDesc.ofDescriptor(classDescStr);
145     }
146 
147 
148     public static MethodHandle buildMethodHandle(MethodHandles.Lookup l, String methodName, MethodType methodType, Consumer<? super CodeBuilder> builder) {
149         ClassDesc genClassDesc = classDesc(l.lookupClass(), "$Code_" + COUNT.getAndIncrement());
150         return buildMethodHandle(l, genClassDesc, methodName, methodType, builder);
151     }
152 
153     public static MethodHandle buildMethodHandle(MethodHandles.Lookup l, ClassDesc classDesc, String methodName, MethodType methodType, Consumer<? super CodeBuilder> builder) {
154         try {
155             byte[] bytes = Classfile.of().build(classDesc, classBuilder -> {
156                 classBuilder.withMethod(methodName,
157                                         MethodTypeDesc.ofDescriptor(methodType.toMethodDescriptorString()),
158                                         Classfile.ACC_PUBLIC + Classfile.ACC_STATIC,
159                                         methodBuilder -> methodBuilder.withCode(builder));
160             });
161             Class<?> clazz = l.defineClass(bytes);
162             return l.findStatic(clazz, methodName, methodType);
163         } catch (Throwable t) {
164             t.printStackTrace();
165             throw new RuntimeException("Failed to buildMethodHandle: " + methodName + " type " + methodType);
166         }
167     }
168 
169 }
< prev index next >