21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang.invoke;
27
28 import jdk.internal.misc.CDS;
29 import jdk.internal.util.ClassFileDumper;
30 import sun.invoke.util.VerifyAccess;
31 import sun.security.action.GetBooleanAction;
32
33 import java.io.Serializable;
34 import java.lang.classfile.ClassBuilder;
35 import java.lang.classfile.ClassFile;
36 import java.lang.classfile.CodeBuilder;
37 import java.lang.classfile.FieldBuilder;
38 import java.lang.classfile.MethodBuilder;
39 import java.lang.classfile.Opcode;
40 import java.lang.classfile.TypeKind;
41 import java.lang.constant.ClassDesc;
42 import java.lang.constant.DynamicConstantDesc;
43 import java.lang.constant.MethodTypeDesc;
44 import java.lang.reflect.Modifier;
45 import java.util.LinkedHashSet;
46 import java.util.List;
47 import java.util.Set;
48 import java.util.function.Consumer;
49
50 import static java.lang.classfile.ClassFile.*;
51 import java.lang.classfile.attribute.ExceptionsAttribute;
52 import java.lang.classfile.constantpool.ClassEntry;
53 import java.lang.classfile.constantpool.ConstantPoolBuilder;
54 import java.lang.classfile.constantpool.MethodRefEntry;
55 import static java.lang.constant.ConstantDescs.*;
56 import static java.lang.invoke.MethodHandleNatives.Constants.NESTMATE_CLASS;
57 import static java.lang.invoke.MethodHandleNatives.Constants.STRONG_LOADER_LINK;
58 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.NESTMATE;
59 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.STRONG;
60 import static java.lang.invoke.MethodType.methodType;
61 import jdk.internal.constant.ConstantUtils;
62 import jdk.internal.constant.MethodTypeDescImpl;
63 import jdk.internal.constant.ReferenceClassDescImpl;
64 import sun.invoke.util.Wrapper;
65
66 /**
67 * Lambda metafactory implementation which dynamically creates an
68 * inner-class-like class per lambda callsite.
69 *
70 * @see LambdaMetafactory
71 */
72 /* package */ final class InnerClassLambdaMetafactory extends AbstractValidatingLambdaMetafactory {
73 private static final String LAMBDA_INSTANCE_FIELD = "LAMBDA_INSTANCE$";
74 private static final String[] EMPTY_STRING_ARRAY = new String[0];
75 private static final ClassDesc[] EMPTY_CLASSDESC_ARRAY = ConstantUtils.EMPTY_CLASSDESC;
76
77 // For dumping generated classes to disk, for debugging purposes
78 private static final ClassFileDumper lambdaProxyClassFileDumper;
79
80 private static final boolean disableEagerInitialization;
81
82 static {
83 // To dump the lambda proxy classes, set this system property:
84 // -Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles
85 // or -Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles=true
86 final String dumpProxyClassesKey = "jdk.invoke.LambdaMetafactory.dumpProxyClassFiles";
87 lambdaProxyClassFileDumper = ClassFileDumper.getInstance(dumpProxyClassesKey, "DUMP_LAMBDA_PROXY_CLASS_FILES");
88
89 final String disableEagerInitializationKey = "jdk.internal.lambda.disableEagerInitialization";
90 disableEagerInitialization = GetBooleanAction.privilegedGetProperty(disableEagerInitializationKey);
91 }
92
93 // See context values in AbstractValidatingLambdaMetafactory
94 private final ClassDesc implMethodClassDesc; // Name of type containing implementation "CC"
95 private final String implMethodName; // Name of implementation method "impl"
96 private final MethodTypeDesc implMethodDesc; // Type descriptor for implementation methods "(I)Ljava/lang/String;"
97 private final MethodType constructorType; // Generated class constructor type "(CC)void"
98 private final MethodTypeDesc constructorTypeDesc;// Type descriptor for the generated class constructor type "(CC)void"
99 private final String[] argNames; // Generated names for the constructor arguments
100 private final ClassDesc[] argDescs; // Type descriptors for the constructor arguments
101 private final String lambdaClassName; // Generated name for the generated class "X$$Lambda$1"
134 * either the target type or one of the additional SAM
135 * types must extend {@code Serializable}.
136 * @param altInterfaces Additional interfaces which the lambda object
137 * should implement.
138 * @param altMethods Method types for additional signatures to be
139 * implemented by invoking the implementation method
140 * @throws LambdaConversionException If any of the meta-factory protocol
141 * invariants are violated
142 * @throws SecurityException If a security manager is present, and it
143 * <a href="MethodHandles.Lookup.html#secmgr">denies access</a>
144 * from {@code caller} to the package of {@code implementation}.
145 */
146 public InnerClassLambdaMetafactory(MethodHandles.Lookup caller,
147 MethodType factoryType,
148 String interfaceMethodName,
149 MethodType interfaceMethodType,
150 MethodHandle implementation,
151 MethodType dynamicMethodType,
152 boolean isSerializable,
153 Class<?>[] altInterfaces,
154 MethodType[] altMethods)
155 throws LambdaConversionException {
156 super(caller, factoryType, interfaceMethodName, interfaceMethodType,
157 implementation, dynamicMethodType,
158 isSerializable, altInterfaces, altMethods);
159 implMethodClassDesc = implClassDesc(implClass);
160 implMethodName = implInfo.getName();
161 implMethodDesc = methodDesc(implInfo.getMethodType());
162 constructorType = factoryType.changeReturnType(Void.TYPE);
163 lambdaClassName = lambdaClassName(targetClass);
164 lambdaClassEntry = pool.classEntry(ReferenceClassDescImpl.ofValidated(ConstantUtils.concat("L", lambdaClassName, ";")));
165 // If the target class invokes a protected method inherited from a
166 // superclass in a different package, or does 'invokespecial', the
167 // lambda class has no access to the resolved method, or does
168 // 'invokestatic' on a hidden class which cannot be resolved by name.
169 // Instead, we need to pass the live implementation method handle to
170 // the proxy class to invoke directly. (javac prefers to avoid this
171 // situation by generating bridges in the target class)
172 useImplMethodHandle = (Modifier.isProtected(implInfo.getModifiers()) &&
173 !VerifyAccess.isSamePackage(targetClass, implInfo.getDeclaringClass())) ||
174 implKind == MethodHandleInfo.REF_invokeSpecial ||
175 implKind == MethodHandleInfo.REF_invokeStatic && implClass.isHidden();
176 int parameterCount = factoryType.parameterCount();
177 if (parameterCount > 0) {
178 argNames = new String[parameterCount];
299 // Assure no duplicate interfaces (ClassFormatError)
300 Set<ClassDesc> itfs = LinkedHashSet.newLinkedHashSet(altInterfaces.length + 1);
301 itfs.add(interfaceDesc);
302 for (Class<?> i : altInterfaces) {
303 itfs.add(classDesc(i));
304 accidentallySerializable |= !isSerializable && Serializable.class.isAssignableFrom(i);
305 }
306 interfaces = List.copyOf(itfs);
307 }
308 final boolean finalAccidentallySerializable = accidentallySerializable;
309 final byte[] classBytes = ClassFile.of().build(lambdaClassEntry, pool, new Consumer<ClassBuilder>() {
310 @Override
311 public void accept(ClassBuilder clb) {
312 clb.withFlags(ACC_SUPER | ACC_FINAL | ACC_SYNTHETIC)
313 .withInterfaceSymbols(interfaces);
314 // Generate final fields to be filled in by constructor
315 for (int i = 0; i < argDescs.length; i++) {
316 clb.withField(argNames[i], argDescs[i], ACC_PRIVATE | ACC_FINAL);
317 }
318
319 generateConstructor(clb);
320
321 if (factoryType.parameterCount() == 0 && disableEagerInitialization) {
322 generateClassInitializer(clb);
323 }
324
325 // Forward the SAM method
326 clb.withMethodBody(interfaceMethodName,
327 methodDesc(interfaceMethodType),
328 ACC_PUBLIC,
329 forwardingMethod(interfaceMethodType));
330
331 // Forward the bridges
332 if (altMethods != null) {
333 for (MethodType mt : altMethods) {
334 clb.withMethodBody(interfaceMethodName,
335 methodDesc(mt),
336 ACC_PUBLIC | ACC_BRIDGE,
337 forwardingMethod(mt));
338 }
339 }
340
341 if (isSerializable)
342 generateSerializationFriendlyMethods(clb);
343 else if (finalAccidentallySerializable)
344 generateSerializationHostileMethods(clb);
345 }
346 });
347
348 // Define the generated class in this VM.
349
350 try {
351 // this class is linked at the indy callsite; so define a hidden nestmate
352 var classdata = useImplMethodHandle? implementation : null;
353 return caller.makeHiddenClassDefiner(lambdaClassName, classBytes, lambdaProxyClassFileDumper, NESTMATE_CLASS | STRONG_LOADER_LINK)
354 .defineClass(!disableEagerInitialization, classdata);
355
356 } catch (Throwable t) {
357 throw new InternalError(t);
358 }
359 }
360
361 /**
362 * Generate a static field and a static initializer that sets this field to an instance of the lambda
363 */
364 private void generateClassInitializer(ClassBuilder clb) {
365 ClassDesc lambdaTypeDescriptor = classDesc(factoryType.returnType());
366
367 // Generate the static final field that holds the lambda singleton
368 clb.withField(LAMBDA_INSTANCE_FIELD, lambdaTypeDescriptor, ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
369
370 // Instantiate the lambda and store it to the static final field
371 clb.withMethodBody(CLASS_INIT_NAME, MTD_void, ACC_STATIC, new Consumer<>() {
372 @Override
382 }
383
384 /**
385 * Generate the constructor for the class
386 */
387 private void generateConstructor(ClassBuilder clb) {
388 // Generate constructor
389 clb.withMethodBody(INIT_NAME, constructorTypeDesc, ACC_PRIVATE,
390 new Consumer<>() {
391 @Override
392 public void accept(CodeBuilder cob) {
393 cob.aload(0)
394 .invokespecial(CD_Object, INIT_NAME, MTD_void);
395 int parameterCount = factoryType.parameterCount();
396 for (int i = 0; i < parameterCount; i++) {
397 cob.aload(0);
398 Class<?> argType = factoryType.parameterType(i);
399 cob.loadLocal(TypeKind.from(argType), cob.parameterSlot(i));
400 cob.putfield(pool.fieldRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(argNames[i], argDescs[i])));
401 }
402 cob.return_();
403 }
404 });
405 }
406
407 private static class SerializationSupport {
408 // Serialization support
409 private static final ClassDesc CD_SerializedLambda = ReferenceClassDescImpl.ofValidated("Ljava/lang/invoke/SerializedLambda;");
410 private static final ClassDesc CD_ObjectOutputStream = ReferenceClassDescImpl.ofValidated("Ljava/io/ObjectOutputStream;");
411 private static final ClassDesc CD_ObjectInputStream = ReferenceClassDescImpl.ofValidated("Ljava/io/ObjectInputStream;");
412 private static final MethodTypeDesc MTD_Object = MethodTypeDescImpl.ofValidated(CD_Object);
413 private static final MethodTypeDesc MTD_void_ObjectOutputStream = MethodTypeDescImpl.ofValidated(CD_void, CD_ObjectOutputStream);
414 private static final MethodTypeDesc MTD_void_ObjectInputStream = MethodTypeDescImpl.ofValidated(CD_void, CD_ObjectInputStream);
415
416 private static final String NAME_METHOD_WRITE_REPLACE = "writeReplace";
417 private static final String NAME_METHOD_READ_OBJECT = "readObject";
418 private static final String NAME_METHOD_WRITE_OBJECT = "writeObject";
419
420 static final ClassDesc CD_NotSerializableException = ReferenceClassDescImpl.ofValidated("Ljava/io/NotSerializableException;");
421 static final MethodTypeDesc MTD_CTOR_NOT_SERIALIZABLE_EXCEPTION = MethodTypeDescImpl.ofValidated(CD_void, CD_String);
422 static final MethodTypeDesc MTD_CTOR_SERIALIZED_LAMBDA = MethodTypeDescImpl.ofValidated(CD_void,
423 CD_Class, CD_String, CD_String, CD_String, CD_int, CD_String, CD_String, CD_String, CD_String, ReferenceClassDescImpl.ofValidated("[Ljava/lang/Object;"));
424
425 }
426
443 .ldc(implInfo.getName())
444 .ldc(implInfo.getMethodType().toMethodDescriptorString())
445 .ldc(dynamicMethodType.toMethodDescriptorString())
446 .loadConstant(argDescs.length)
447 .anewarray(CD_Object);
448 for (int i = 0; i < argDescs.length; i++) {
449 cob.dup()
450 .loadConstant(i)
451 .aload(0)
452 .getfield(pool.fieldRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(argNames[i], argDescs[i])));
453 TypeConvertingMethodAdapter.boxIfTypePrimitive(cob, TypeKind.from(argDescs[i]));
454 cob.aastore();
455 }
456 cob.invokespecial(SerializationSupport.CD_SerializedLambda, INIT_NAME,
457 SerializationSupport.MTD_CTOR_SERIALIZED_LAMBDA)
458 .areturn();
459 }
460 });
461 }
462
463 /**
464 * Generate a readObject/writeObject method that is hostile to serialization
465 */
466 private void generateSerializationHostileMethods(ClassBuilder clb) {
467 var hostileMethod = new Consumer<MethodBuilder>() {
468 @Override
469 public void accept(MethodBuilder mb) {
470 ConstantPoolBuilder cp = mb.constantPool();
471 ClassEntry nseCE = cp.classEntry(SerializationSupport.CD_NotSerializableException);
472 mb.with(ExceptionsAttribute.of(nseCE))
473 .withCode(new Consumer<CodeBuilder>() {
474 @Override
475 public void accept(CodeBuilder cob) {
476 cob.new_(nseCE)
477 .dup()
478 .ldc("Non-serializable lambda")
479 .invokespecial(cp.methodRefEntry(nseCE, cp.nameAndTypeEntry(INIT_NAME,
480 SerializationSupport.MTD_CTOR_NOT_SERIALIZABLE_EXCEPTION)))
481 .athrow();
482 }
486 clb.withMethod(SerializationSupport.NAME_METHOD_WRITE_OBJECT, SerializationSupport.MTD_void_ObjectOutputStream,
487 ACC_PRIVATE + ACC_FINAL, hostileMethod);
488 clb.withMethod(SerializationSupport.NAME_METHOD_READ_OBJECT, SerializationSupport.MTD_void_ObjectInputStream,
489 ACC_PRIVATE + ACC_FINAL, hostileMethod);
490 }
491
492 /**
493 * This method generates a method body which calls the lambda implementation
494 * method, converting arguments, as needed.
495 */
496 Consumer<CodeBuilder> forwardingMethod(MethodType methodType) {
497 return new Consumer<>() {
498 @Override
499 public void accept(CodeBuilder cob) {
500 if (implKind == MethodHandleInfo.REF_newInvokeSpecial) {
501 cob.new_(implMethodClassDesc)
502 .dup();
503 }
504 if (useImplMethodHandle) {
505 ConstantPoolBuilder cp = cob.constantPool();
506 cob.ldc(cp.constantDynamicEntry(cp.bsmEntry(cp.methodHandleEntry(BSM_CLASS_DATA), List.of()),
507 cp.nameAndTypeEntry(DEFAULT_NAME, CD_MethodHandle)));
508 }
509 for (int i = 0; i < argNames.length; i++) {
510 cob.aload(0)
511 .getfield(pool.fieldRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(argNames[i], argDescs[i])));
512 }
513
514 convertArgumentTypes(cob, methodType);
515
516 if (useImplMethodHandle) {
517 MethodType mtype = implInfo.getMethodType();
518 if (implKind != MethodHandleInfo.REF_invokeStatic) {
519 mtype = mtype.insertParameterTypes(0, implClass);
520 }
521 cob.invokevirtual(CD_MethodHandle, "invokeExact", methodDesc(mtype));
522 } else {
523 // Invoke the method we want to forward to
524 cob.invoke(invocationOpcode(), implMethodClassDesc, implMethodName, implMethodDesc, implClass.isInterface());
525 }
526 // Convert the return value (if any) and return it
527 // Note: if adapting from non-void to void, the 'return'
528 // instruction will pop the unneeded result
529 Class<?> implReturnClass = implMethodType.returnType();
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang.invoke;
27
28 import jdk.internal.misc.CDS;
29 import jdk.internal.util.ClassFileDumper;
30 import sun.invoke.util.VerifyAccess;
31 import sun.security.action.GetBooleanAction;
32
33 import java.io.Serializable;
34 import java.lang.classfile.ClassBuilder;
35 import java.lang.classfile.ClassFile;
36 import java.lang.classfile.CodeBuilder;
37 import java.lang.classfile.FieldBuilder;
38 import java.lang.classfile.MethodBuilder;
39 import java.lang.classfile.Opcode;
40 import java.lang.classfile.TypeKind;
41 import java.lang.classfile.constantpool.MethodHandleEntry;
42 import java.lang.classfile.constantpool.NameAndTypeEntry;
43 import java.lang.constant.ClassDesc;
44 import java.lang.constant.DynamicConstantDesc;
45 import java.lang.constant.MethodTypeDesc;
46 import java.lang.invoke.MethodHandles.Lookup;
47 import java.lang.module.Configuration;
48 import java.lang.module.ModuleFinder;
49 import java.lang.reflect.Modifier;
50 import java.util.LinkedHashSet;
51 import java.util.List;
52 import java.util.Optional;
53 import java.util.ServiceLoader;
54 import java.util.Set;
55 import java.util.function.Consumer;
56
57 import static java.lang.classfile.ClassFile.*;
58 import java.lang.classfile.attribute.ExceptionsAttribute;
59 import java.lang.classfile.constantpool.ClassEntry;
60 import java.lang.classfile.constantpool.ConstantPoolBuilder;
61 import java.lang.classfile.constantpool.MethodRefEntry;
62 import static java.lang.constant.ConstantDescs.*;
63 import static java.lang.invoke.MethodHandleNatives.Constants.NESTMATE_CLASS;
64 import static java.lang.invoke.MethodHandleNatives.Constants.STRONG_LOADER_LINK;
65 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.NESTMATE;
66 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.STRONG;
67 import static java.lang.invoke.MethodType.methodType;
68 import jdk.internal.constant.ConstantUtils;
69 import jdk.internal.constant.MethodTypeDescImpl;
70 import jdk.internal.constant.ReferenceClassDescImpl;
71 import sun.invoke.util.Wrapper;
72
73 /**
74 * Lambda metafactory implementation which dynamically creates an
75 * inner-class-like class per lambda callsite.
76 *
77 * @see LambdaMetafactory
78 */
79 /* package */ final class InnerClassLambdaMetafactory extends AbstractValidatingLambdaMetafactory {
80 private static final String LAMBDA_INSTANCE_FIELD = "LAMBDA_INSTANCE$";
81 private static final String[] EMPTY_STRING_ARRAY = new String[0];
82 private static final ClassDesc[] EMPTY_CLASSDESC_ARRAY = ConstantUtils.EMPTY_CLASSDESC;
83
84 // Static builders to avoid lambdas
85 record MethodBody(Consumer<CodeBuilder> code) implements Consumer<MethodBuilder> {
86 @Override
87 public void accept(MethodBuilder mb) {
88 mb.withCode(code);
89 }
90 };
91
92 // For dumping generated classes to disk, for debugging purposes
93 private static final ClassFileDumper lambdaProxyClassFileDumper;
94
95 private static final boolean disableEagerInitialization;
96
97 private static final String NAME_METHOD_QUOTED = "quoted";
98 private static final String quotedInstanceFieldName = "quoted";
99
100 static {
101 // To dump the lambda proxy classes, set this system property:
102 // -Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles
103 // or -Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles=true
104 final String dumpProxyClassesKey = "jdk.invoke.LambdaMetafactory.dumpProxyClassFiles";
105 lambdaProxyClassFileDumper = ClassFileDumper.getInstance(dumpProxyClassesKey, "DUMP_LAMBDA_PROXY_CLASS_FILES");
106
107 final String disableEagerInitializationKey = "jdk.internal.lambda.disableEagerInitialization";
108 disableEagerInitialization = GetBooleanAction.privilegedGetProperty(disableEagerInitializationKey);
109 }
110
111 // See context values in AbstractValidatingLambdaMetafactory
112 private final ClassDesc implMethodClassDesc; // Name of type containing implementation "CC"
113 private final String implMethodName; // Name of implementation method "impl"
114 private final MethodTypeDesc implMethodDesc; // Type descriptor for implementation methods "(I)Ljava/lang/String;"
115 private final MethodType constructorType; // Generated class constructor type "(CC)void"
116 private final MethodTypeDesc constructorTypeDesc;// Type descriptor for the generated class constructor type "(CC)void"
117 private final String[] argNames; // Generated names for the constructor arguments
118 private final ClassDesc[] argDescs; // Type descriptors for the constructor arguments
119 private final String lambdaClassName; // Generated name for the generated class "X$$Lambda$1"
152 * either the target type or one of the additional SAM
153 * types must extend {@code Serializable}.
154 * @param altInterfaces Additional interfaces which the lambda object
155 * should implement.
156 * @param altMethods Method types for additional signatures to be
157 * implemented by invoking the implementation method
158 * @throws LambdaConversionException If any of the meta-factory protocol
159 * invariants are violated
160 * @throws SecurityException If a security manager is present, and it
161 * <a href="MethodHandles.Lookup.html#secmgr">denies access</a>
162 * from {@code caller} to the package of {@code implementation}.
163 */
164 public InnerClassLambdaMetafactory(MethodHandles.Lookup caller,
165 MethodType factoryType,
166 String interfaceMethodName,
167 MethodType interfaceMethodType,
168 MethodHandle implementation,
169 MethodType dynamicMethodType,
170 boolean isSerializable,
171 Class<?>[] altInterfaces,
172 MethodType[] altMethods,
173 MethodHandle reflectiveField)
174 throws LambdaConversionException {
175 super(caller, factoryType, interfaceMethodName, interfaceMethodType,
176 implementation, dynamicMethodType,
177 isSerializable, altInterfaces, altMethods, reflectiveField);
178 implMethodClassDesc = implClassDesc(implClass);
179 implMethodName = implInfo.getName();
180 implMethodDesc = methodDesc(implInfo.getMethodType());
181 constructorType = factoryType.changeReturnType(Void.TYPE);
182 lambdaClassName = lambdaClassName(targetClass);
183 lambdaClassEntry = pool.classEntry(ReferenceClassDescImpl.ofValidated(ConstantUtils.concat("L", lambdaClassName, ";")));
184 // If the target class invokes a protected method inherited from a
185 // superclass in a different package, or does 'invokespecial', the
186 // lambda class has no access to the resolved method, or does
187 // 'invokestatic' on a hidden class which cannot be resolved by name.
188 // Instead, we need to pass the live implementation method handle to
189 // the proxy class to invoke directly. (javac prefers to avoid this
190 // situation by generating bridges in the target class)
191 useImplMethodHandle = (Modifier.isProtected(implInfo.getModifiers()) &&
192 !VerifyAccess.isSamePackage(targetClass, implInfo.getDeclaringClass())) ||
193 implKind == MethodHandleInfo.REF_invokeSpecial ||
194 implKind == MethodHandleInfo.REF_invokeStatic && implClass.isHidden();
195 int parameterCount = factoryType.parameterCount();
196 if (parameterCount > 0) {
197 argNames = new String[parameterCount];
318 // Assure no duplicate interfaces (ClassFormatError)
319 Set<ClassDesc> itfs = LinkedHashSet.newLinkedHashSet(altInterfaces.length + 1);
320 itfs.add(interfaceDesc);
321 for (Class<?> i : altInterfaces) {
322 itfs.add(classDesc(i));
323 accidentallySerializable |= !isSerializable && Serializable.class.isAssignableFrom(i);
324 }
325 interfaces = List.copyOf(itfs);
326 }
327 final boolean finalAccidentallySerializable = accidentallySerializable;
328 final byte[] classBytes = ClassFile.of().build(lambdaClassEntry, pool, new Consumer<ClassBuilder>() {
329 @Override
330 public void accept(ClassBuilder clb) {
331 clb.withFlags(ACC_SUPER | ACC_FINAL | ACC_SYNTHETIC)
332 .withInterfaceSymbols(interfaces);
333 // Generate final fields to be filled in by constructor
334 for (int i = 0; i < argDescs.length; i++) {
335 clb.withField(argNames[i], argDescs[i], ACC_PRIVATE | ACC_FINAL);
336 }
337
338 // if quotable, generate the field that will hold the value of quoted
339 if (quotableOpField != null) {
340 clb.withField(quotedInstanceFieldName, CodeReflectionSupport.CD_Quoted, ACC_PRIVATE + ACC_FINAL);
341 }
342
343 generateConstructor(clb);
344
345 if (factoryType.parameterCount() == 0 && disableEagerInitialization) {
346 generateClassInitializer(clb);
347 }
348
349 // Forward the SAM method
350 clb.withMethodBody(interfaceMethodName,
351 methodDesc(interfaceMethodType),
352 ACC_PUBLIC,
353 forwardingMethod(interfaceMethodType));
354
355 // Forward the bridges
356 if (altMethods != null) {
357 for (MethodType mt : altMethods) {
358 clb.withMethodBody(interfaceMethodName,
359 methodDesc(mt),
360 ACC_PUBLIC | ACC_BRIDGE,
361 forwardingMethod(mt));
362 }
363 }
364
365 if (isSerializable)
366 generateSerializationFriendlyMethods(clb);
367 else if (finalAccidentallySerializable)
368 generateSerializationHostileMethods(clb);
369
370 if (quotableOpField != null) {
371 generateQuotableMethod(clb);
372 }
373 }
374 });
375
376 // Define the generated class in this VM.
377
378 try {
379 // this class is linked at the indy callsite; so define a hidden nestmate
380 List<?> classdata;
381 if (useImplMethodHandle || quotableOpField != null) {
382 classdata = quotableOpField == null ?
383 List.of(implementation) :
384 List.of(implementation, quotableOpField, CodeReflectionSupport.HANDLE_MAKE_QUOTED);
385 } else {
386 classdata = null;
387 }
388 return caller.makeHiddenClassDefiner(lambdaClassName, classBytes, lambdaProxyClassFileDumper, NESTMATE_CLASS | STRONG_LOADER_LINK)
389 .defineClass(!disableEagerInitialization, classdata);
390
391 } catch (Throwable t) {
392 throw new InternalError(t);
393 }
394 }
395
396 /**
397 * Generate a static field and a static initializer that sets this field to an instance of the lambda
398 */
399 private void generateClassInitializer(ClassBuilder clb) {
400 ClassDesc lambdaTypeDescriptor = classDesc(factoryType.returnType());
401
402 // Generate the static final field that holds the lambda singleton
403 clb.withField(LAMBDA_INSTANCE_FIELD, lambdaTypeDescriptor, ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
404
405 // Instantiate the lambda and store it to the static final field
406 clb.withMethodBody(CLASS_INIT_NAME, MTD_void, ACC_STATIC, new Consumer<>() {
407 @Override
417 }
418
419 /**
420 * Generate the constructor for the class
421 */
422 private void generateConstructor(ClassBuilder clb) {
423 // Generate constructor
424 clb.withMethodBody(INIT_NAME, constructorTypeDesc, ACC_PRIVATE,
425 new Consumer<>() {
426 @Override
427 public void accept(CodeBuilder cob) {
428 cob.aload(0)
429 .invokespecial(CD_Object, INIT_NAME, MTD_void);
430 int parameterCount = factoryType.parameterCount();
431 for (int i = 0; i < parameterCount; i++) {
432 cob.aload(0);
433 Class<?> argType = factoryType.parameterType(i);
434 cob.loadLocal(TypeKind.from(argType), cob.parameterSlot(i));
435 cob.putfield(pool.fieldRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(argNames[i], argDescs[i])));
436 }
437 if (quotableOpField != null) {
438 generateQuotedFieldInitializer(cob);
439 }
440 cob.return_();
441 }
442 });
443 }
444
445 private void generateQuotedFieldInitializer(CodeBuilder cob) {
446 ConstantPoolBuilder cp = cob.constantPool();
447 MethodHandleEntry bsmDataAt = cp.methodHandleEntry(BSM_CLASS_DATA_AT);
448 NameAndTypeEntry natMH = cp.nameAndTypeEntry(DEFAULT_NAME, CD_MethodHandle);
449 // push the receiver on the stack for operand of put field instruction
450 cob.aload(0)
451 .ldc(cp.constantDynamicEntry(cp.bsmEntry(bsmDataAt, List.of(cp.intEntry(2))), natMH))
452 // load op string from field
453 .ldc(cp.constantDynamicEntry(cp.bsmEntry(bsmDataAt, List.of(cp.intEntry(1))), natMH));
454 MethodType mtype = quotableOpFieldInfo.getMethodType();
455 if (quotableOpFieldInfo.getReferenceKind() != MethodHandleInfo.REF_getStatic) {
456 mtype = mtype.insertParameterTypes(0, implClass);
457 }
458 cob.invokevirtual(CD_MethodHandle, "invokeExact", mtype.describeConstable().get());
459
460 // load captured args in array
461
462 int capturedArity = factoryType.parameterCount();
463 cob.loadConstant(capturedArity)
464 .anewarray(CD_Object);
465 // initialize quoted captures
466 for (int i = 0; i < capturedArity; i++) {
467 cob.dup()
468 .loadConstant(i)
469 .aload(0)
470 .getfield(lambdaClassEntry.asSymbol(), argNames[i], argDescs[i]);
471 TypeConvertingMethodAdapter.boxIfTypePrimitive(cob, TypeKind.from(argDescs[i]));
472 cob.aastore();
473 }
474
475 // now create a Quoted from String and captured args Object[]
476
477 cob.invokevirtual(CD_MethodHandle, "invokeExact", methodDesc(CodeReflectionSupport.HANDLE_MAKE_QUOTED.type()))
478 .putfield(lambdaClassEntry.asSymbol(), quotedInstanceFieldName, CodeReflectionSupport.CD_Quoted);
479 }
480
481 static class CodeReflectionSupport {
482 static final Class<?> QUOTED_CLASS;
483 static final Class<?> QUOTABLE_CLASS;
484 static final MethodHandle HANDLE_MAKE_QUOTED;
485
486 static {
487 try {
488 // we know jdk.incubator code is there, no need to check
489 ModuleLayer parent = ModuleLayer.boot();
490 Configuration cf = parent.configuration()
491 .resolve(ModuleFinder.of(), ModuleFinder.ofSystem(), Set.of("jdk.incubator.code"));
492 ClassLoader scl = ClassLoader.getSystemClassLoader();
493 ModuleLayer layer = parent.defineModulesWithOneLoader(cf, scl);
494 QUOTED_CLASS = layer.findLoader("jdk.incubator.code")
495 .loadClass("jdk.incubator.code.Quoted");
496 QUOTABLE_CLASS = layer.findLoader("jdk.incubator.code")
497 .loadClass("jdk.incubator.code.Quotable");
498 Class<?> quotedHelper = layer.findLoader("jdk.incubator.code")
499 .loadClass("jdk.incubator.code.internal.QuotedHelper");
500 MethodHandle makeQuoted = Lookup.IMPL_LOOKUP.findStatic(quotedHelper, "makeQuoted",
501 MethodType.methodType(QUOTED_CLASS, MethodHandles.Lookup.class, String.class, Object[].class));
502 HANDLE_MAKE_QUOTED = makeQuoted.bindTo(Lookup.IMPL_LOOKUP);
503 } catch (Throwable ex) {
504 throw new ExceptionInInitializerError(ex);
505 }
506 }
507
508 static final ClassDesc CD_Quoted = QUOTED_CLASS.describeConstable().get();
509 static final MethodTypeDesc MTD_Quoted = MethodTypeDescImpl.ofValidated(CD_Quoted);
510 }
511
512 private static class SerializationSupport {
513 // Serialization support
514 private static final ClassDesc CD_SerializedLambda = ReferenceClassDescImpl.ofValidated("Ljava/lang/invoke/SerializedLambda;");
515 private static final ClassDesc CD_ObjectOutputStream = ReferenceClassDescImpl.ofValidated("Ljava/io/ObjectOutputStream;");
516 private static final ClassDesc CD_ObjectInputStream = ReferenceClassDescImpl.ofValidated("Ljava/io/ObjectInputStream;");
517 private static final MethodTypeDesc MTD_Object = MethodTypeDescImpl.ofValidated(CD_Object);
518 private static final MethodTypeDesc MTD_void_ObjectOutputStream = MethodTypeDescImpl.ofValidated(CD_void, CD_ObjectOutputStream);
519 private static final MethodTypeDesc MTD_void_ObjectInputStream = MethodTypeDescImpl.ofValidated(CD_void, CD_ObjectInputStream);
520
521 private static final String NAME_METHOD_WRITE_REPLACE = "writeReplace";
522 private static final String NAME_METHOD_READ_OBJECT = "readObject";
523 private static final String NAME_METHOD_WRITE_OBJECT = "writeObject";
524
525 static final ClassDesc CD_NotSerializableException = ReferenceClassDescImpl.ofValidated("Ljava/io/NotSerializableException;");
526 static final MethodTypeDesc MTD_CTOR_NOT_SERIALIZABLE_EXCEPTION = MethodTypeDescImpl.ofValidated(CD_void, CD_String);
527 static final MethodTypeDesc MTD_CTOR_SERIALIZED_LAMBDA = MethodTypeDescImpl.ofValidated(CD_void,
528 CD_Class, CD_String, CD_String, CD_String, CD_int, CD_String, CD_String, CD_String, CD_String, ReferenceClassDescImpl.ofValidated("[Ljava/lang/Object;"));
529
530 }
531
548 .ldc(implInfo.getName())
549 .ldc(implInfo.getMethodType().toMethodDescriptorString())
550 .ldc(dynamicMethodType.toMethodDescriptorString())
551 .loadConstant(argDescs.length)
552 .anewarray(CD_Object);
553 for (int i = 0; i < argDescs.length; i++) {
554 cob.dup()
555 .loadConstant(i)
556 .aload(0)
557 .getfield(pool.fieldRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(argNames[i], argDescs[i])));
558 TypeConvertingMethodAdapter.boxIfTypePrimitive(cob, TypeKind.from(argDescs[i]));
559 cob.aastore();
560 }
561 cob.invokespecial(SerializationSupport.CD_SerializedLambda, INIT_NAME,
562 SerializationSupport.MTD_CTOR_SERIALIZED_LAMBDA)
563 .areturn();
564 }
565 });
566 }
567
568 /**
569 * Generate a writeReplace method that supports serialization
570 */
571 private void generateQuotableMethod(ClassBuilder clb) {
572 clb.withMethod(NAME_METHOD_QUOTED, CodeReflectionSupport.MTD_Quoted, ACC_PUBLIC + ACC_FINAL, new MethodBody(new Consumer<CodeBuilder>() {
573 @Override
574 public void accept(CodeBuilder cob) {
575 cob.aload(0)
576 .getfield(lambdaClassEntry.asSymbol(), quotedInstanceFieldName, CodeReflectionSupport.CD_Quoted)
577 .areturn();
578 }
579 }));
580 }
581
582 /**
583 * Generate a readObject/writeObject method that is hostile to serialization
584 */
585 private void generateSerializationHostileMethods(ClassBuilder clb) {
586 var hostileMethod = new Consumer<MethodBuilder>() {
587 @Override
588 public void accept(MethodBuilder mb) {
589 ConstantPoolBuilder cp = mb.constantPool();
590 ClassEntry nseCE = cp.classEntry(SerializationSupport.CD_NotSerializableException);
591 mb.with(ExceptionsAttribute.of(nseCE))
592 .withCode(new Consumer<CodeBuilder>() {
593 @Override
594 public void accept(CodeBuilder cob) {
595 cob.new_(nseCE)
596 .dup()
597 .ldc("Non-serializable lambda")
598 .invokespecial(cp.methodRefEntry(nseCE, cp.nameAndTypeEntry(INIT_NAME,
599 SerializationSupport.MTD_CTOR_NOT_SERIALIZABLE_EXCEPTION)))
600 .athrow();
601 }
605 clb.withMethod(SerializationSupport.NAME_METHOD_WRITE_OBJECT, SerializationSupport.MTD_void_ObjectOutputStream,
606 ACC_PRIVATE + ACC_FINAL, hostileMethod);
607 clb.withMethod(SerializationSupport.NAME_METHOD_READ_OBJECT, SerializationSupport.MTD_void_ObjectInputStream,
608 ACC_PRIVATE + ACC_FINAL, hostileMethod);
609 }
610
611 /**
612 * This method generates a method body which calls the lambda implementation
613 * method, converting arguments, as needed.
614 */
615 Consumer<CodeBuilder> forwardingMethod(MethodType methodType) {
616 return new Consumer<>() {
617 @Override
618 public void accept(CodeBuilder cob) {
619 if (implKind == MethodHandleInfo.REF_newInvokeSpecial) {
620 cob.new_(implMethodClassDesc)
621 .dup();
622 }
623 if (useImplMethodHandle) {
624 ConstantPoolBuilder cp = cob.constantPool();
625 cob.ldc(cp.constantDynamicEntry(cp.bsmEntry(cp.methodHandleEntry(BSM_CLASS_DATA_AT), List.of(cp.intEntry(0))),
626 cp.nameAndTypeEntry(DEFAULT_NAME, CD_MethodHandle)));
627 }
628 for (int i = 0; i < argNames.length ; i++) {
629 cob.aload(0)
630 .getfield(pool.fieldRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(argNames[i], argDescs[i])));
631 }
632
633 convertArgumentTypes(cob, methodType);
634
635 if (useImplMethodHandle) {
636 MethodType mtype = implInfo.getMethodType();
637 if (implKind != MethodHandleInfo.REF_invokeStatic) {
638 mtype = mtype.insertParameterTypes(0, implClass);
639 }
640 cob.invokevirtual(CD_MethodHandle, "invokeExact", methodDesc(mtype));
641 } else {
642 // Invoke the method we want to forward to
643 cob.invoke(invocationOpcode(), implMethodClassDesc, implMethodName, implMethodDesc, implClass.isInterface());
644 }
645 // Convert the return value (if any) and return it
646 // Note: if adapting from non-void to void, the 'return'
647 // instruction will pop the unneeded result
648 Class<?> implReturnClass = implMethodType.returnType();
|