27
28 import jdk.internal.org.objectweb.asm.ClassWriter;
29 import jdk.internal.org.objectweb.asm.FieldVisitor;
30 import jdk.internal.org.objectweb.asm.Label;
31 import jdk.internal.org.objectweb.asm.MethodVisitor;
32 import jdk.internal.org.objectweb.asm.Opcodes;
33 import jdk.internal.org.objectweb.asm.Type;
34 import sun.invoke.util.VerifyAccess;
35 import sun.invoke.util.VerifyType;
36 import sun.invoke.util.Wrapper;
37 import sun.reflect.misc.ReflectUtil;
38
39 import java.io.File;
40 import java.io.FileOutputStream;
41 import java.io.IOException;
42 import java.lang.reflect.Modifier;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Set;
48 import java.util.stream.Stream;
49
50 import static java.lang.invoke.LambdaForm.BasicType;
51 import static java.lang.invoke.LambdaForm.BasicType.*;
52 import static java.lang.invoke.LambdaForm.*;
53 import static java.lang.invoke.MethodHandleNatives.Constants.*;
54 import static java.lang.invoke.MethodHandleStatics.*;
55 import static java.lang.invoke.MethodHandles.Lookup.*;
56
57 /**
58 * Code generation backend for LambdaForm.
59 * <p>
60 * @author John Rose, JSR 292 EG
61 */
62 class InvokerBytecodeGenerator {
63 /** Define class names for convenience. */
64 private static final String MH = "java/lang/invoke/MethodHandle";
65 private static final String MHI = "java/lang/invoke/MethodHandleImpl";
66 private static final String LF = "java/lang/invoke/LambdaForm";
67 private static final String LFN = "java/lang/invoke/LambdaForm$Name";
298
299 /**
300 * Extract the number of constant pool entries from a given class file.
301 *
302 * @param classFile the bytes of the class file in question.
303 * @return the number of entries in the constant pool.
304 */
305 private static int getConstantPoolSize(byte[] classFile) {
306 // The first few bytes:
307 // u4 magic;
308 // u2 minor_version;
309 // u2 major_version;
310 // u2 constant_pool_count;
311 return ((classFile[8] & 0xFF) << 8) | (classFile[9] & 0xFF);
312 }
313
314 /**
315 * Extract the MemberName of a newly-defined method.
316 */
317 private MemberName loadMethod(byte[] classFile) {
318 Class<?> invokerClass = LOOKUP.makeHiddenClassDefiner(classFile, Set.of(ClassOption.WEAK))
319 .defineClass(true, classDataValues());
320 return resolveInvokerMember(invokerClass, invokerName, invokerType);
321 }
322
323 private static MemberName resolveInvokerMember(Class<?> invokerClass, String name, MethodType type) {
324 MemberName member = new MemberName(invokerClass, name, type, REF_invokeStatic);
325 try {
326 member = MEMBERNAME_FACTORY.resolveOrFail(REF_invokeStatic, member, HOST_CLASS, ReflectiveOperationException.class);
327 } catch (ReflectiveOperationException e) {
328 throw newInternalError(e);
329 }
330 return member;
331 }
332
333 /**
334 * Set up class file generation.
335 */
336 private ClassWriter classFilePrologue() {
337 final int NOT_ACC_PUBLIC = 0; // not ACC_PUBLIC
338 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
|
27
28 import jdk.internal.org.objectweb.asm.ClassWriter;
29 import jdk.internal.org.objectweb.asm.FieldVisitor;
30 import jdk.internal.org.objectweb.asm.Label;
31 import jdk.internal.org.objectweb.asm.MethodVisitor;
32 import jdk.internal.org.objectweb.asm.Opcodes;
33 import jdk.internal.org.objectweb.asm.Type;
34 import sun.invoke.util.VerifyAccess;
35 import sun.invoke.util.VerifyType;
36 import sun.invoke.util.Wrapper;
37 import sun.reflect.misc.ReflectUtil;
38
39 import java.io.File;
40 import java.io.FileOutputStream;
41 import java.io.IOException;
42 import java.lang.reflect.Modifier;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.stream.Stream;
48
49 import static java.lang.invoke.LambdaForm.BasicType;
50 import static java.lang.invoke.LambdaForm.BasicType.*;
51 import static java.lang.invoke.LambdaForm.*;
52 import static java.lang.invoke.MethodHandleNatives.Constants.*;
53 import static java.lang.invoke.MethodHandleStatics.*;
54 import static java.lang.invoke.MethodHandles.Lookup.*;
55
56 /**
57 * Code generation backend for LambdaForm.
58 * <p>
59 * @author John Rose, JSR 292 EG
60 */
61 class InvokerBytecodeGenerator {
62 /** Define class names for convenience. */
63 private static final String MH = "java/lang/invoke/MethodHandle";
64 private static final String MHI = "java/lang/invoke/MethodHandleImpl";
65 private static final String LF = "java/lang/invoke/LambdaForm";
66 private static final String LFN = "java/lang/invoke/LambdaForm$Name";
297
298 /**
299 * Extract the number of constant pool entries from a given class file.
300 *
301 * @param classFile the bytes of the class file in question.
302 * @return the number of entries in the constant pool.
303 */
304 private static int getConstantPoolSize(byte[] classFile) {
305 // The first few bytes:
306 // u4 magic;
307 // u2 minor_version;
308 // u2 major_version;
309 // u2 constant_pool_count;
310 return ((classFile[8] & 0xFF) << 8) | (classFile[9] & 0xFF);
311 }
312
313 /**
314 * Extract the MemberName of a newly-defined method.
315 */
316 private MemberName loadMethod(byte[] classFile) {
317 Class<?> invokerClass = LOOKUP.makeHiddenClassDefiner(classFile)
318 .defineClass(true, classDataValues());
319 return resolveInvokerMember(invokerClass, invokerName, invokerType);
320 }
321
322 private static MemberName resolveInvokerMember(Class<?> invokerClass, String name, MethodType type) {
323 MemberName member = new MemberName(invokerClass, name, type, REF_invokeStatic);
324 try {
325 member = MEMBERNAME_FACTORY.resolveOrFail(REF_invokeStatic, member, HOST_CLASS, ReflectiveOperationException.class);
326 } catch (ReflectiveOperationException e) {
327 throw newInternalError(e);
328 }
329 return member;
330 }
331
332 /**
333 * Set up class file generation.
334 */
335 private ClassWriter classFilePrologue() {
336 final int NOT_ACC_PUBLIC = 0; // not ACC_PUBLIC
337 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
|