< prev index next >

src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java

Print this page

 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.constant.ClassOrInterfaceDescImpl;
 29 import jdk.internal.misc.CDS;
 30 import jdk.internal.util.ClassFileDumper;
 31 import sun.invoke.util.VerifyAccess;
 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.constant.ClassDesc;
 41 import java.lang.constant.MethodTypeDesc;



 42 import java.lang.reflect.Modifier;
 43 import java.util.LinkedHashSet;
 44 import java.util.List;
 45 import java.util.Set;
 46 import java.util.function.Consumer;
 47 
 48 import static java.lang.classfile.ClassFile.*;
 49 import java.lang.classfile.attribute.ExceptionsAttribute;
 50 import java.lang.classfile.constantpool.ClassEntry;
 51 import java.lang.classfile.constantpool.ConstantPoolBuilder;
 52 
 53 import static java.lang.constant.ConstantDescs.*;
 54 import static java.lang.invoke.MethodHandleNatives.Constants.NESTMATE_CLASS;
 55 import static java.lang.invoke.MethodHandleNatives.Constants.STRONG_LOADER_LINK;
 56 import jdk.internal.constant.ConstantUtils;
 57 import jdk.internal.constant.MethodTypeDescImpl;
 58 import jdk.internal.vm.annotation.Stable;
 59 import sun.invoke.util.Wrapper;
 60 
 61 /**
 62  * Lambda metafactory implementation which dynamically creates an
 63  * inner-class-like class per lambda callsite.
 64  *
 65  * @see LambdaMetafactory
 66  */
 67 /* package */ final class InnerClassLambdaMetafactory extends AbstractValidatingLambdaMetafactory {
 68     private static final String LAMBDA_INSTANCE_FIELD = "LAMBDA_INSTANCE$";
 69     private static final @Stable String[] ARG_NAME_CACHE = {"arg$1", "arg$2", "arg$3", "arg$4", "arg$5", "arg$6", "arg$7", "arg$8"};
 70     private static final ClassDesc[] EMPTY_CLASSDESC_ARRAY = ConstantUtils.EMPTY_CLASSDESC;
 71 








 72     // For dumping generated classes to disk, for debugging purposes
 73     private static final ClassFileDumper lambdaProxyClassFileDumper;
 74 
 75     private static final boolean disableEagerInitialization;
 76 




 77     static {
 78         // To dump the lambda proxy classes, set this system property:
 79         //    -Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles
 80         // or -Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles=true
 81         final String dumpProxyClassesKey = "jdk.invoke.LambdaMetafactory.dumpProxyClassFiles";
 82         lambdaProxyClassFileDumper = ClassFileDumper.getInstance(dumpProxyClassesKey, "DUMP_LAMBDA_PROXY_CLASS_FILES");
 83 
 84         final String disableEagerInitializationKey = "jdk.internal.lambda.disableEagerInitialization";
 85         disableEagerInitialization = Boolean.getBoolean(disableEagerInitializationKey);
 86     }
 87 
 88     // See context values in AbstractValidatingLambdaMetafactory
 89     private final ClassDesc implMethodClassDesc;     // Name of type containing implementation "CC"
 90     private final String implMethodName;             // Name of implementation method "impl"
 91     private final MethodTypeDesc implMethodDesc;     // Type descriptor for implementation methods "(I)Ljava/lang/String;"
 92     private final MethodType constructorType;        // Generated class constructor type "(CC)void"
 93     private final MethodTypeDesc constructorTypeDesc;// Type descriptor for the generated class constructor type "(CC)void"
 94     private final ClassDesc[] argDescs;              // Type descriptors for the constructor arguments
 95     private final String lambdaClassName;            // Generated name for the generated class "X$$Lambda$1"
 96     private final ConstantPoolBuilder pool = ConstantPoolBuilder.of();

125      *                          substituted with their instantiation from
126      *                          the capture site
127      * @param isSerializable Should the lambda be made serializable?  If set,
128      *                       either the target type or one of the additional SAM
129      *                       types must extend {@code Serializable}.
130      * @param altInterfaces Additional interfaces which the lambda object
131      *                      should implement.
132      * @param altMethods Method types for additional signatures to be
133      *                   implemented by invoking the implementation method
134      * @throws LambdaConversionException If any of the meta-factory protocol
135      *         invariants are violated
136      */
137     public InnerClassLambdaMetafactory(MethodHandles.Lookup caller,
138                                        MethodType factoryType,
139                                        String interfaceMethodName,
140                                        MethodType interfaceMethodType,
141                                        MethodHandle implementation,
142                                        MethodType dynamicMethodType,
143                                        boolean isSerializable,
144                                        Class<?>[] altInterfaces,
145                                        MethodType[] altMethods)

146             throws LambdaConversionException {
147         super(caller, factoryType, interfaceMethodName, interfaceMethodType,
148               implementation, dynamicMethodType,
149               isSerializable, altInterfaces, altMethods);
150         implMethodClassDesc = implClassDesc(implClass);
151         implMethodName = implInfo.getName();
152         implMethodDesc = methodDesc(implInfo.getMethodType());
153         constructorType = factoryType.changeReturnType(Void.TYPE);
154         lambdaClassName = lambdaClassName(targetClass);
155         lambdaClassEntry = pool.classEntry(ConstantUtils.internalNameToDesc(lambdaClassName));
156         // If the target class invokes a protected method inherited from a
157         // superclass in a different package, or does 'invokespecial', the
158         // lambda class has no access to the resolved method, or does
159         // 'invokestatic' on a hidden class which cannot be resolved by name.
160         // Instead, we need to pass the live implementation method handle to
161         // the proxy class to invoke directly. (javac prefers to avoid this
162         // situation by generating bridges in the target class)
163         useImplMethodHandle = (Modifier.isProtected(implInfo.getModifiers()) &&
164                                !VerifyAccess.isSamePackage(targetClass, implInfo.getDeclaringClass())) ||
165                                implKind == MethodHandleInfo.REF_invokeSpecial ||
166                                implKind == MethodHandleInfo.REF_invokeStatic && implClass.isHidden();
167         int parameterCount = factoryType.parameterCount();
168         ClassDesc[] argDescs;
169         MethodTypeDesc constructorTypeDesc;

300             // Assure no duplicate interfaces (ClassFormatError)
301             Set<ClassDesc> itfs = LinkedHashSet.newLinkedHashSet(altInterfaces.length + 1);
302             itfs.add(interfaceDesc);
303             for (Class<?> i : altInterfaces) {
304                 itfs.add(classDesc(i));
305                 accidentallySerializable |= !isSerializable && Serializable.class.isAssignableFrom(i);
306             }
307             interfaces = List.copyOf(itfs);
308         }
309         final boolean finalAccidentallySerializable = accidentallySerializable;
310         final byte[] classBytes = ClassFile.of().build(lambdaClassEntry, pool, new Consumer<ClassBuilder>() {
311             @Override
312             public void accept(ClassBuilder clb) {
313                 clb.withFlags(ACC_SUPER | ACC_FINAL | ACC_SYNTHETIC)
314                    .withInterfaceSymbols(interfaces);
315                 // Generate final fields to be filled in by constructor
316                 for (int i = 0; i < argDescs.length; i++) {
317                     clb.withField(argName(i), argDescs[i], ACC_PRIVATE | ACC_FINAL);
318                 }
319 





320                 generateConstructor(clb);
321 
322                 if (factoryType.parameterCount() == 0 && disableEagerInitialization) {
323                     generateClassInitializer(clb);
324                 }
325 
326                 // Forward the SAM method
327                 clb.withMethodBody(interfaceMethodName,
328                         methodDesc(interfaceMethodType),
329                         ACC_PUBLIC,
330                         forwardingMethod(interfaceMethodType));
331 
332                 // Forward the bridges
333                 if (altMethods != null) {
334                     for (MethodType mt : altMethods) {
335                         clb.withMethodBody(interfaceMethodName,
336                                 methodDesc(mt),
337                                 ACC_PUBLIC | ACC_BRIDGE,
338                                 forwardingMethod(mt));
339                     }
340                 }
341 
342                 if (isSerializable)
343                     generateSerializationFriendlyMethods(clb);
344                 else if (finalAccidentallySerializable)
345                     generateSerializationHostileMethods(clb);




346             }
347         });
348 
349         // Define the generated class in this VM.
350 
351         try {
352             // this class is linked at the indy callsite; so define a hidden nestmate
353             var classdata = useImplMethodHandle? implementation : null;







354             return caller.makeHiddenClassDefiner(lambdaClassName, classBytes, lambdaProxyClassFileDumper, NESTMATE_CLASS | STRONG_LOADER_LINK)
355                          .defineClass(!disableEagerInitialization, classdata);
356 
357         } catch (Throwable t) {
358             throw new InternalError(t);
359         }
360     }
361 
362     /**
363      * Generate a static field and a static initializer that sets this field to an instance of the lambda
364      */
365     private void generateClassInitializer(ClassBuilder clb) {
366         ClassDesc lambdaTypeDescriptor = classDesc(factoryType.returnType());
367 
368         // Generate the static final field that holds the lambda singleton
369         clb.withField(LAMBDA_INSTANCE_FIELD, lambdaTypeDescriptor, ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
370 
371         // Instantiate the lambda and store it to the static final field
372         clb.withMethodBody(CLASS_INIT_NAME, MTD_void, ACC_STATIC, new Consumer<>() {
373             @Override
374             public void accept(CodeBuilder cob) {
375                 assert factoryType.parameterCount() == 0;
376                 cob.new_(lambdaClassEntry)
377                    .dup()
378                    .invokespecial(pool.methodRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(INIT_NAME, constructorTypeDesc)))
379                    .putstatic(pool.fieldRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(LAMBDA_INSTANCE_FIELD, lambdaTypeDescriptor)))
380                    .return_();





























381             }
382         });
383     }
384 
385     /**
386      * Generate the constructor for the class
387      */
388     private void generateConstructor(ClassBuilder clb) {
389         // Generate constructor
390         clb.withMethodBody(INIT_NAME, constructorTypeDesc, ACC_PRIVATE,
391                 new Consumer<>() {
392                     @Override
393                     public void accept(CodeBuilder cob) {
394                         cob.aload(0)
395                            .invokespecial(CD_Object, INIT_NAME, MTD_void);
396                         int parameterCount = factoryType.parameterCount();
397                         for (int i = 0; i < parameterCount; i++) {
398                             cob.aload(0)
399                                .loadLocal(TypeKind.from(factoryType.parameterType(i)), cob.parameterSlot(i))
400                                .putfield(pool.fieldRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(argName(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 = ClassOrInterfaceDescImpl.ofValidated("Ljava/lang/invoke/SerializedLambda;");
410         private static final ClassDesc CD_ObjectOutputStream = ClassOrInterfaceDescImpl.ofValidated("Ljava/io/ObjectOutputStream;");
411         private static final ClassDesc CD_ObjectInputStream = ClassOrInterfaceDescImpl.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 = ClassOrInterfaceDescImpl.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, ConstantUtils.CD_Object_array);
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(argName(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 < argDescs.length; i++) {
510                     cob.aload(0)
511                        .getfield(pool.fieldRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(argName(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

 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.constant.ClassOrInterfaceDescImpl;
 29 import jdk.internal.misc.CDS;
 30 import jdk.internal.util.ClassFileDumper;
 31 import sun.invoke.util.VerifyAccess;
 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 jdk.internal.constant.ConstantUtils;
 62 import jdk.internal.constant.MethodTypeDescImpl;
 63 import jdk.internal.vm.annotation.Stable;
 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 @Stable String[] ARG_NAME_CACHE = {"arg$1", "arg$2", "arg$3", "arg$4", "arg$5", "arg$6", "arg$7", "arg$8"};
 75     private static final ClassDesc[] EMPTY_CLASSDESC_ARRAY = ConstantUtils.EMPTY_CLASSDESC;
 76 
 77     // Static builders to avoid lambdas
 78     record MethodBody(Consumer<CodeBuilder> code) implements Consumer<MethodBuilder> {
 79         @Override
 80         public void accept(MethodBuilder mb) {
 81             mb.withCode(code);
 82         }
 83     };
 84 
 85     // For dumping generated classes to disk, for debugging purposes
 86     private static final ClassFileDumper lambdaProxyClassFileDumper;
 87 
 88     private static final boolean disableEagerInitialization;
 89 
 90     private static final String NAME_METHOD_QUOTED = "__internal_quoted";
 91     private static final String quotedInstanceFieldName = "quoted";
 92     private static final String COMPILER_GENERATED_MODEL_FIELD_NAME = "COMPILER_GENERATED_MODEL";
 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 = Boolean.getBoolean(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 ClassDesc[] argDescs;              // Type descriptors for the constructor arguments
112     private final String lambdaClassName;            // Generated name for the generated class "X$$Lambda$1"
113     private final ConstantPoolBuilder pool = ConstantPoolBuilder.of();

142      *                          substituted with their instantiation from
143      *                          the capture site
144      * @param isSerializable Should the lambda be made serializable?  If set,
145      *                       either the target type or one of the additional SAM
146      *                       types must extend {@code Serializable}.
147      * @param altInterfaces Additional interfaces which the lambda object
148      *                      should implement.
149      * @param altMethods Method types for additional signatures to be
150      *                   implemented by invoking the implementation method
151      * @throws LambdaConversionException If any of the meta-factory protocol
152      *         invariants are violated
153      */
154     public InnerClassLambdaMetafactory(MethodHandles.Lookup caller,
155                                        MethodType factoryType,
156                                        String interfaceMethodName,
157                                        MethodType interfaceMethodType,
158                                        MethodHandle implementation,
159                                        MethodType dynamicMethodType,
160                                        boolean isSerializable,
161                                        Class<?>[] altInterfaces,
162                                        MethodType[] altMethods,
163                                        MethodHandle reflectiveField)
164             throws LambdaConversionException {
165         super(caller, factoryType, interfaceMethodName, interfaceMethodType,
166               implementation, dynamicMethodType,
167               isSerializable, altInterfaces, altMethods, reflectiveField);
168         implMethodClassDesc = implClassDesc(implClass);
169         implMethodName = implInfo.getName();
170         implMethodDesc = methodDesc(implInfo.getMethodType());
171         constructorType = factoryType.changeReturnType(Void.TYPE);
172         lambdaClassName = lambdaClassName(targetClass);
173         lambdaClassEntry = pool.classEntry(ConstantUtils.internalNameToDesc(lambdaClassName));
174         // If the target class invokes a protected method inherited from a
175         // superclass in a different package, or does 'invokespecial', the
176         // lambda class has no access to the resolved method, or does
177         // 'invokestatic' on a hidden class which cannot be resolved by name.
178         // Instead, we need to pass the live implementation method handle to
179         // the proxy class to invoke directly. (javac prefers to avoid this
180         // situation by generating bridges in the target class)
181         useImplMethodHandle = (Modifier.isProtected(implInfo.getModifiers()) &&
182                                !VerifyAccess.isSamePackage(targetClass, implInfo.getDeclaringClass())) ||
183                                implKind == MethodHandleInfo.REF_invokeSpecial ||
184                                implKind == MethodHandleInfo.REF_invokeStatic && implClass.isHidden();
185         int parameterCount = factoryType.parameterCount();
186         ClassDesc[] argDescs;
187         MethodTypeDesc constructorTypeDesc;

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(argName(i), argDescs[i], ACC_PRIVATE | ACC_FINAL);
336                 }
337 
338                 // if quotable, generate the field that will hold the value of quoted
339                 if (quotableOpGetter != null) {
340                     clb.withField(quotedInstanceFieldName, CodeReflectionSupport.CD_Quoted, ACC_PRIVATE + ACC_FINAL);
341                 }
342 
343                 generateConstructor(clb);
344 
345                 generateClassInitializationMethod(clb);
346 

347 
348                 // Forward the SAM method
349                 clb.withMethodBody(interfaceMethodName,
350                         methodDesc(interfaceMethodType),
351                         ACC_PUBLIC,
352                         forwardingMethod(interfaceMethodType));
353 
354                 // Forward the bridges
355                 if (altMethods != null) {
356                     for (MethodType mt : altMethods) {
357                         clb.withMethodBody(interfaceMethodName,
358                                 methodDesc(mt),
359                                 ACC_PUBLIC | ACC_BRIDGE,
360                                 forwardingMethod(mt));
361                     }
362                 }
363 
364                 if (isSerializable)
365                     generateSerializationFriendlyMethods(clb);
366                 else if (finalAccidentallySerializable)
367                     generateSerializationHostileMethods(clb);
368 
369                 if (quotableOpGetter != null) {
370                     generateQuotedMethod(clb);
371                 }
372             }
373         });
374 
375         // Define the generated class in this VM.
376 
377         try {
378             // this class is linked at the indy callsite; so define a hidden nestmate
379             List<?> classdata;
380             if (useImplMethodHandle || quotableOpGetter != null) {
381                 classdata = quotableOpGetter == null ?
382                         List.of(implementation) :
383                         List.of(implementation, quotableOpGetter, CodeReflectionSupport.QUOTED_OP_MH);
384             } else {
385                 classdata = null;
386             }
387             return caller.makeHiddenClassDefiner(lambdaClassName, classBytes, lambdaProxyClassFileDumper, NESTMATE_CLASS | STRONG_LOADER_LINK)
388                          .defineClass(!disableEagerInitialization, classdata);
389 
390         } catch (Throwable t) {
391             throw new InternalError(t);
392         }
393     }
394 
395     private void generateClassInitializationMethod(ClassBuilder clb) {
396         if (!(factoryType.parameterCount() == 0 && disableEagerInitialization) && quotableOpGetter == null) {
397             return;
398         }
399         clb.withMethodBody(CLASS_INIT_NAME, MTD_void, ACC_STATIC, new Consumer<CodeBuilder>() {






400             @Override
401             public void accept(CodeBuilder cob) {
402                 if (factoryType.parameterCount() == 0 && disableEagerInitialization) {
403                     ClassDesc lambdaTypeDescriptor = classDesc(factoryType.returnType());
404                     // Generate the static final field that holds the lambda singleton
405                     clb.withField(LAMBDA_INSTANCE_FIELD, lambdaTypeDescriptor, ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
406                     cob.new_(lambdaClassEntry)
407                             .dup()
408                             .invokespecial(pool.methodRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(INIT_NAME, constructorTypeDesc)))
409                             .putstatic(pool.fieldRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(LAMBDA_INSTANCE_FIELD, lambdaTypeDescriptor)));
410                 }
411 
412                 if (quotableOpGetter != null) {
413                     // if we visit a callsite twice, we will use the same class
414                     // if the lambda doesn't capture values we only have one instance, model shared anyway
415                     // if it captures values, each visit result in a creation of new instance of the class
416                     // those instances have the same code model generated by the compiler
417                     // they may differ in captured values
418                     // as first step let's share the compiler generated code model
419                     ClassDesc funcOpClassDesc = CodeReflectionSupport.FUNC_OP_CLASS.describeConstable().get();
420                     clb.withField(COMPILER_GENERATED_MODEL_FIELD_NAME, funcOpClassDesc,
421                             ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
422 
423                     ConstantPoolBuilder cp = pool;
424                     MethodHandleEntry bsmDataAt = cp.methodHandleEntry(BSM_CLASS_DATA_AT);
425                     NameAndTypeEntry natMH = cp.nameAndTypeEntry(DEFAULT_NAME, CD_MethodHandle);
426                     // load quotableOpGetter
427                     cob.ldc(cp.constantDynamicEntry(cp.bsmEntry(bsmDataAt, List.of(cp.intEntry(1))), natMH));
428                     MethodType mtype = quotableOpGetterInfo.getMethodType();
429                     if (quotableOpGetterInfo.getReferenceKind() != MethodHandleInfo.REF_invokeStatic) {
430                         mtype = mtype.insertParameterTypes(0, implClass);
431                     }
432                     cob.invokevirtual(CD_MethodHandle, "invokeExact", mtype.describeConstable().get());
433                     cob.checkcast(funcOpClassDesc);
434                     cob.putstatic(lambdaClassEntry.asSymbol(), COMPILER_GENERATED_MODEL_FIELD_NAME, funcOpClassDesc);
435                 }
436                 cob.return_();
437             }
438         });
439     }
440 
441     /**
442      * Generate the constructor for the class
443      */
444     private void generateConstructor(ClassBuilder clb) {
445         // Generate constructor
446         clb.withMethodBody(INIT_NAME, constructorTypeDesc, ACC_PRIVATE,
447                 new Consumer<>() {
448                     @Override
449                     public void accept(CodeBuilder cob) {
450                         cob.aload(0)
451                            .invokespecial(CD_Object, INIT_NAME, MTD_void);
452                         int parameterCount = factoryType.parameterCount();
453                         for (int i = 0; i < parameterCount; i++) {
454                             cob.aload(0)
455                                .loadLocal(TypeKind.from(factoryType.parameterType(i)), cob.parameterSlot(i))
456                                .putfield(pool.fieldRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(argName(i), argDescs[i])));
457                         }
458                         if (quotableOpGetter != null) {
459                             generateQuotedFieldInitializer(cob);
460                         }
461                         cob.return_();
462                     }
463                 });
464     }
465 
466     private void generateQuotedFieldInitializer(CodeBuilder cob) {
467         ConstantPoolBuilder cp = cob.constantPool();
468         MethodHandleEntry bsmDataAt = cp.methodHandleEntry(BSM_CLASS_DATA_AT);
469         NameAndTypeEntry natMH = cp.nameAndTypeEntry(DEFAULT_NAME, CD_MethodHandle);
470         // push the receiver on the stack for operand of put field instruction
471         cob.aload(0)
472                 // load class data: CodeReflectionSupport.HANDLE_MAKE_QUOTED
473                 .ldc(cp.constantDynamicEntry(cp.bsmEntry(bsmDataAt, List.of(cp.intEntry(2))), natMH))
474                 .getstatic(lambdaClassEntry.asSymbol(), COMPILER_GENERATED_MODEL_FIELD_NAME,
475                         CodeReflectionSupport.FUNC_OP_CLASS.describeConstable().get());
476 
477 
478         // load captured args in array
479 
480         int capturedArity = factoryType.parameterCount();
481         cob.loadConstant(capturedArity)
482            .anewarray(CD_Object);
483         // initialize quoted captures
484         for (int i = 0; i < capturedArity; i++) {
485             cob.dup()
486                .loadConstant(i)
487                .aload(0)
488                .getfield(lambdaClassEntry.asSymbol(), argName(i), argDescs[i]);
489             TypeConvertingMethodAdapter.boxIfTypePrimitive(cob, TypeKind.from(argDescs[i]));
490             cob.aastore();
491         }
492 
493         // Create a Quoted from FuncOp and captured args Object[]
494 
495         cob.invokevirtual(CD_MethodHandle, "invokeExact", methodDesc(CodeReflectionSupport.QUOTED_OP_MH.type()))
496            .putfield(lambdaClassEntry.asSymbol(), quotedInstanceFieldName, CodeReflectionSupport.CD_Quoted);
497     }
498 
499     static class CodeReflectionSupport {
500         static final Class<?> QUOTED_CLASS;
501         static final Class<?> QUOTABLE_CLASS;
502         static final MethodHandle QUOTED_OP_MH;
503         static final Class<?> FUNC_OP_CLASS;
504 
505         static {
506             try {
507                 ModuleLayer layer = codeLayer();
508                 ClassLoader cl = layer.findLoader("jdk.incubator.code");
509                 QUOTED_CLASS = cl.loadClass("jdk.incubator.code.Quoted");
510                 QUOTABLE_CLASS = cl.loadClass("jdk.incubator.code.Quotable");
511                 FUNC_OP_CLASS = cl.loadClass("jdk.incubator.code.dialect.core.CoreOp$FuncOp");
512                 QUOTED_OP_MH = Lookup.IMPL_LOOKUP.findStatic(QUOTED_CLASS, "quotedOp",
513                         MethodType.methodType(QUOTED_CLASS, FUNC_OP_CLASS, Object[].class));
514             } catch (Throwable ex) {
515                 throw new ExceptionInInitializerError(ex);
516             }
517         }
518 
519         static ModuleLayer codeLayer() {
520             final ModuleLayer codeLayer;
521             if (ModuleLayer.boot().findModule("jdk.incubator.code").isPresent()) {
522                 // we are in an exploded build, so just use the boot layer
523                 return ModuleLayer.boot();
524             } else if (java.lang.module.ModuleFinder.ofSystem().find("jdk.incubator.code").isPresent()) {
525                 // the code module is installed, but not in the boot layer, create a new layer which contains it
526                 ModuleLayer parent = ModuleLayer.boot();
527                 Configuration cf = parent.configuration()
528                         .resolve(ModuleFinder.of(), ModuleFinder.ofSystem(), Set.of("jdk.incubator.code"));
529                 ClassLoader scl = ClassLoader.getSystemClassLoader();
530                 return parent.defineModulesWithOneLoader(cf, scl);
531             } else {
532                 throw new IllegalStateException("jdk.incubator.code module not found");
533             }
534         }
535 
536         static final ClassDesc CD_Quoted = QUOTED_CLASS.describeConstable().get();
537         static final MethodTypeDesc MTD_Quoted = MethodTypeDescImpl.ofValidated(CD_Quoted);
538     }
539 
540     private static class SerializationSupport {
541         // Serialization support
542         private static final ClassDesc CD_SerializedLambda = ClassOrInterfaceDescImpl.ofValidated("Ljava/lang/invoke/SerializedLambda;");
543         private static final ClassDesc CD_ObjectOutputStream = ClassOrInterfaceDescImpl.ofValidated("Ljava/io/ObjectOutputStream;");
544         private static final ClassDesc CD_ObjectInputStream = ClassOrInterfaceDescImpl.ofValidated("Ljava/io/ObjectInputStream;");
545         private static final MethodTypeDesc MTD_Object = MethodTypeDescImpl.ofValidated(CD_Object);
546         private static final MethodTypeDesc MTD_void_ObjectOutputStream = MethodTypeDescImpl.ofValidated(CD_void, CD_ObjectOutputStream);
547         private static final MethodTypeDesc MTD_void_ObjectInputStream = MethodTypeDescImpl.ofValidated(CD_void, CD_ObjectInputStream);
548 
549         private static final String NAME_METHOD_WRITE_REPLACE = "writeReplace";
550         private static final String NAME_METHOD_READ_OBJECT = "readObject";
551         private static final String NAME_METHOD_WRITE_OBJECT = "writeObject";
552 
553         static final ClassDesc CD_NotSerializableException = ClassOrInterfaceDescImpl.ofValidated("Ljava/io/NotSerializableException;");
554         static final MethodTypeDesc MTD_CTOR_NOT_SERIALIZABLE_EXCEPTION = MethodTypeDescImpl.ofValidated(CD_void, CD_String);
555         static final MethodTypeDesc MTD_CTOR_SERIALIZED_LAMBDA = MethodTypeDescImpl.ofValidated(CD_void,
556                 CD_Class, CD_String, CD_String, CD_String, CD_int, CD_String, CD_String, CD_String, CD_String, ConstantUtils.CD_Object_array);
557 
558     }
559 

576                            .ldc(implInfo.getName())
577                            .ldc(implInfo.getMethodType().toMethodDescriptorString())
578                            .ldc(dynamicMethodType.toMethodDescriptorString())
579                            .loadConstant(argDescs.length)
580                            .anewarray(CD_Object);
581                         for (int i = 0; i < argDescs.length; i++) {
582                             cob.dup()
583                                .loadConstant(i)
584                                .aload(0)
585                                .getfield(pool.fieldRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(argName(i), argDescs[i])));
586                             TypeConvertingMethodAdapter.boxIfTypePrimitive(cob, TypeKind.from(argDescs[i]));
587                             cob.aastore();
588                         }
589                         cob.invokespecial(SerializationSupport.CD_SerializedLambda, INIT_NAME,
590                                           SerializationSupport.MTD_CTOR_SERIALIZED_LAMBDA)
591                            .areturn();
592                     }
593                 });
594     }
595 
596     /**
597     * Generate method #quoted()
598      */
599     private void generateQuotedMethod(ClassBuilder clb) {
600         clb.withMethod(NAME_METHOD_QUOTED, CodeReflectionSupport.MTD_Quoted, ACC_PRIVATE + ACC_FINAL, new MethodBody(new Consumer<CodeBuilder>() {
601             @Override
602             public void accept(CodeBuilder cob) {
603                 cob.aload(0)
604                    .getfield(lambdaClassEntry.asSymbol(), quotedInstanceFieldName, CodeReflectionSupport.CD_Quoted)
605                    .areturn();
606             }
607         }));
608     }
609 
610     /**
611      * Generate a readObject/writeObject method that is hostile to serialization
612      */
613     private void generateSerializationHostileMethods(ClassBuilder clb) {
614         var hostileMethod = new Consumer<MethodBuilder>() {
615             @Override
616             public void accept(MethodBuilder mb) {
617                 ConstantPoolBuilder cp = mb.constantPool();
618                 ClassEntry nseCE = cp.classEntry(SerializationSupport.CD_NotSerializableException);
619                 mb.with(ExceptionsAttribute.of(nseCE))
620                         .withCode(new Consumer<CodeBuilder>() {
621                             @Override
622                             public void accept(CodeBuilder cob) {
623                                 cob.new_(nseCE)
624                                         .dup()
625                                         .ldc("Non-serializable lambda")
626                                         .invokespecial(cp.methodRefEntry(nseCE, cp.nameAndTypeEntry(INIT_NAME,
627                                                 SerializationSupport.MTD_CTOR_NOT_SERIALIZABLE_EXCEPTION)))
628                                         .athrow();
629                             }

633         clb.withMethod(SerializationSupport.NAME_METHOD_WRITE_OBJECT, SerializationSupport.MTD_void_ObjectOutputStream,
634                 ACC_PRIVATE + ACC_FINAL, hostileMethod);
635         clb.withMethod(SerializationSupport.NAME_METHOD_READ_OBJECT, SerializationSupport.MTD_void_ObjectInputStream,
636                 ACC_PRIVATE + ACC_FINAL, hostileMethod);
637     }
638 
639     /**
640      * This method generates a method body which calls the lambda implementation
641      * method, converting arguments, as needed.
642      */
643     Consumer<CodeBuilder> forwardingMethod(MethodType methodType) {
644         return new Consumer<>() {
645             @Override
646             public void accept(CodeBuilder cob) {
647                 if (implKind == MethodHandleInfo.REF_newInvokeSpecial) {
648                     cob.new_(implMethodClassDesc)
649                        .dup();
650                 }
651                 if (useImplMethodHandle) {
652                     ConstantPoolBuilder cp = cob.constantPool();
653                     cob.ldc(cp.constantDynamicEntry(cp.bsmEntry(cp.methodHandleEntry(BSM_CLASS_DATA_AT), List.of(cp.intEntry(0))),
654                                                     cp.nameAndTypeEntry(DEFAULT_NAME, CD_MethodHandle)));
655                 }
656                 for (int i = 0; i < argDescs.length; i++) {
657                     cob.aload(0)
658                        .getfield(pool.fieldRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(argName(i), argDescs[i])));
659                 }
660 
661                 convertArgumentTypes(cob, methodType);
662 
663                 if (useImplMethodHandle) {
664                     MethodType mtype = implInfo.getMethodType();
665                     if (implKind != MethodHandleInfo.REF_invokeStatic) {
666                         mtype = mtype.insertParameterTypes(0, implClass);
667                     }
668                     cob.invokevirtual(CD_MethodHandle, "invokeExact", methodDesc(mtype));
669                 } else {
670                     // Invoke the method we want to forward to
671                     cob.invoke(invocationOpcode(), implMethodClassDesc, implMethodName, implMethodDesc, implClass.isInterface());
672                 }
673                 // Convert the return value (if any) and return it
< prev index next >