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