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;
114 * which the lambda or method reference is being
115 * converted, represented as a String.
116 * @param interfaceMethodType Type of the method in the functional interface to
117 * which the lambda or method reference is being
118 * converted, represented as a MethodType.
119 * @param implementation The implementation method which should be called (with
120 * suitable adaptation of argument types, return types,
121 * and adjustment for captured arguments) when methods of
122 * the resulting functional interface instance are invoked.
123 * @param dynamicMethodType The signature of the primary functional
124 * interface method after type variables are
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;
290 * @throws LambdaConversionException If properly formed functional interface
291 * is not found
292 */
293 private Class<?> generateInnerClass() throws LambdaConversionException {
294 List<ClassDesc> interfaces;
295 ClassDesc interfaceDesc = classDesc(interfaceClass);
296 boolean accidentallySerializable = !isSerializable && Serializable.class.isAssignableFrom(interfaceClass);
297 if (altInterfaces.length == 0) {
298 interfaces = List.of(interfaceDesc);
299 } else {
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 // All Classes in the BSM argument method types are loaded; no need for LoadableDescriptors
316
317 // Generate final fields to be filled in by constructor
318 for (int i = 0; i < argDescs.length; i++) {
319 clb.withField(argName(i), argDescs[i], ACC_PRIVATE | ACC_FINAL);
320 }
321
322 generateConstructor(clb);
323
324 if (factoryType.parameterCount() == 0 && disableEagerInitialization) {
325 generateClassInitializer(clb);
326 }
327
328 // Forward the SAM method
329 clb.withMethodBody(interfaceMethodName,
330 methodDesc(interfaceMethodType),
331 ACC_PUBLIC,
332 forwardingMethod(interfaceMethodType));
333
334 // Forward the bridges
335 if (altMethods != null) {
336 for (MethodType mt : altMethods) {
337 clb.withMethodBody(interfaceMethodName,
338 methodDesc(mt),
339 ACC_PUBLIC | ACC_BRIDGE,
340 forwardingMethod(mt));
341 }
342 }
343
344 if (isSerializable)
345 generateSerializationFriendlyMethods(clb);
346 else if (finalAccidentallySerializable)
347 generateSerializationHostileMethods(clb);
348 }
349 });
350
351 // Define the generated class in this VM.
352
353 try {
354 // this class is linked at the indy callsite; so define a hidden nestmate
355 var classdata = useImplMethodHandle? implementation : null;
356 return caller.makeHiddenClassDefiner(lambdaClassName, classBytes, lambdaProxyClassFileDumper, NESTMATE_CLASS | STRONG_LOADER_LINK)
357 .defineClass(!disableEagerInitialization, classdata);
358
359 } catch (Throwable t) {
360 throw new InternalError(t);
361 }
362 }
363
364 /**
365 * Generate a static field and a static initializer that sets this field to an instance of the lambda
366 */
367 private void generateClassInitializer(ClassBuilder clb) {
368 ClassDesc lambdaTypeDescriptor = classDesc(factoryType.returnType());
369
370 // Generate the static final field that holds the lambda singleton
371 clb.withField(LAMBDA_INSTANCE_FIELD, lambdaTypeDescriptor, ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
372
373 // Instantiate the lambda and store it to the static final field
374 clb.withMethodBody(CLASS_INIT_NAME, MTD_void, ACC_STATIC, new Consumer<>() {
375 @Override
488 clb.withMethod(SerializationSupport.NAME_METHOD_WRITE_OBJECT, SerializationSupport.MTD_void_ObjectOutputStream,
489 ACC_PRIVATE + ACC_FINAL, hostileMethod);
490 clb.withMethod(SerializationSupport.NAME_METHOD_READ_OBJECT, SerializationSupport.MTD_void_ObjectInputStream,
491 ACC_PRIVATE + ACC_FINAL, hostileMethod);
492 }
493
494 /**
495 * This method generates a method body which calls the lambda implementation
496 * method, converting arguments, as needed.
497 */
498 Consumer<CodeBuilder> forwardingMethod(MethodType methodType) {
499 return new Consumer<>() {
500 @Override
501 public void accept(CodeBuilder cob) {
502 if (implKind == MethodHandleInfo.REF_newInvokeSpecial) {
503 cob.new_(implMethodClassDesc)
504 .dup();
505 }
506 if (useImplMethodHandle) {
507 ConstantPoolBuilder cp = cob.constantPool();
508 cob.ldc(cp.constantDynamicEntry(cp.bsmEntry(cp.methodHandleEntry(BSM_CLASS_DATA), List.of()),
509 cp.nameAndTypeEntry(DEFAULT_NAME, CD_MethodHandle)));
510 }
511 for (int i = 0; i < argDescs.length; i++) {
512 cob.aload(0)
513 .getfield(pool.fieldRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(argName(i), argDescs[i])));
514 }
515
516 convertArgumentTypes(cob, methodType);
517
518 if (useImplMethodHandle) {
519 MethodType mtype = implInfo.getMethodType();
520 if (implKind != MethodHandleInfo.REF_invokeStatic) {
521 mtype = mtype.insertParameterTypes(0, implClass);
522 }
523 cob.invokevirtual(CD_MethodHandle, "invokeExact", methodDesc(mtype));
524 } else {
525 // Invoke the method we want to forward to
526 cob.invoke(invocationOpcode(), implMethodClassDesc, implMethodName, implMethodDesc, implClass.isInterface());
527 }
528 // Convert the return value (if any) and return it
|
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 import java.util.function.Function;
48
49 import static java.lang.classfile.ClassFile.*;
50 import java.lang.classfile.attribute.ExceptionsAttribute;
51 import java.lang.classfile.constantpool.ClassEntry;
52 import java.lang.classfile.constantpool.ConstantPoolBuilder;
53
54 import static java.lang.constant.ConstantDescs.*;
55 import static java.lang.invoke.MethodHandleNatives.Constants.NESTMATE_CLASS;
56 import static java.lang.invoke.MethodHandleNatives.Constants.STRONG_LOADER_LINK;
57 import java.util.ArrayList;
58 import jdk.internal.constant.ConstantUtils;
59 import jdk.internal.constant.MethodTypeDescImpl;
60 import jdk.internal.vm.annotation.Stable;
61 import sun.invoke.util.Wrapper;
62
63 /**
64 * Lambda metafactory implementation which dynamically creates an
65 * inner-class-like class per lambda callsite.
66 *
67 * @see LambdaMetafactory
68 */
69 /* package */ final class InnerClassLambdaMetafactory extends AbstractValidatingLambdaMetafactory {
70 private static final String LAMBDA_INSTANCE_FIELD = "LAMBDA_INSTANCE$";
71 private static final @Stable String[] ARG_NAME_CACHE = {"arg$1", "arg$2", "arg$3", "arg$4", "arg$5", "arg$6", "arg$7", "arg$8"};
72 private static final ClassDesc[] EMPTY_CLASSDESC_ARRAY = ConstantUtils.EMPTY_CLASSDESC;
73
74 // For dumping generated classes to disk, for debugging purposes
75 private static final ClassFileDumper lambdaProxyClassFileDumper;
76
77 private static final boolean disableEagerInitialization;
116 * which the lambda or method reference is being
117 * converted, represented as a String.
118 * @param interfaceMethodType Type of the method in the functional interface to
119 * which the lambda or method reference is being
120 * converted, represented as a MethodType.
121 * @param implementation The implementation method which should be called (with
122 * suitable adaptation of argument types, return types,
123 * and adjustment for captured arguments) when methods of
124 * the resulting functional interface instance are invoked.
125 * @param dynamicMethodType The signature of the primary functional
126 * interface method after type variables are
127 * substituted with their instantiation from
128 * the capture site
129 * @param isSerializable Should the lambda be made serializable? If set,
130 * either the target type or one of the additional SAM
131 * types must extend {@code Serializable}.
132 * @param altInterfaces Additional interfaces which the lambda object
133 * should implement.
134 * @param altMethods Method types for additional signatures to be
135 * implemented by invoking the implementation method
136 * @param finisher Function called at the end of the lambda class build process
137 * that returns an additional object to append to the class data,
138 * may be (@code null}, may return {@code null}.
139 * @throws LambdaConversionException If any of the meta-factory protocol
140 * invariants are violated
141 */
142 public InnerClassLambdaMetafactory(MethodHandles.Lookup caller,
143 MethodType factoryType,
144 String interfaceMethodName,
145 MethodType interfaceMethodType,
146 MethodHandle implementation,
147 MethodType dynamicMethodType,
148 boolean isSerializable,
149 Class<?>[] altInterfaces,
150 MethodType[] altMethods,
151 Function<ClassBuilder, Object> finisher)
152 throws LambdaConversionException {
153 super(caller, factoryType, interfaceMethodName, interfaceMethodType,
154 implementation, dynamicMethodType,
155 isSerializable, altInterfaces, altMethods, finisher);
156 implMethodClassDesc = implClassDesc(implClass);
157 implMethodName = implInfo.getName();
158 implMethodDesc = methodDesc(implInfo.getMethodType());
159 constructorType = factoryType.changeReturnType(Void.TYPE);
160 lambdaClassName = lambdaClassName(targetClass);
161 lambdaClassEntry = pool.classEntry(ConstantUtils.internalNameToDesc(lambdaClassName));
162 // If the target class invokes a protected method inherited from a
163 // superclass in a different package, or does 'invokespecial', the
164 // lambda class has no access to the resolved method, or does
165 // 'invokestatic' on a hidden class which cannot be resolved by name.
166 // Instead, we need to pass the live implementation method handle to
167 // the proxy class to invoke directly. (javac prefers to avoid this
168 // situation by generating bridges in the target class)
169 useImplMethodHandle = (Modifier.isProtected(implInfo.getModifiers()) &&
170 !VerifyAccess.isSamePackage(targetClass, implInfo.getDeclaringClass())) ||
171 implKind == MethodHandleInfo.REF_invokeSpecial ||
172 implKind == MethodHandleInfo.REF_invokeStatic && implClass.isHidden();
173 int parameterCount = factoryType.parameterCount();
174 ClassDesc[] argDescs;
175 MethodTypeDesc constructorTypeDesc;
296 * @throws LambdaConversionException If properly formed functional interface
297 * is not found
298 */
299 private Class<?> generateInnerClass() throws LambdaConversionException {
300 List<ClassDesc> interfaces;
301 ClassDesc interfaceDesc = classDesc(interfaceClass);
302 boolean accidentallySerializable = !isSerializable && Serializable.class.isAssignableFrom(interfaceClass);
303 if (altInterfaces.length == 0) {
304 interfaces = List.of(interfaceDesc);
305 } else {
306 // Assure no duplicate interfaces (ClassFormatError)
307 Set<ClassDesc> itfs = LinkedHashSet.newLinkedHashSet(altInterfaces.length + 1);
308 itfs.add(interfaceDesc);
309 for (Class<?> i : altInterfaces) {
310 itfs.add(classDesc(i));
311 accidentallySerializable |= !isSerializable && Serializable.class.isAssignableFrom(i);
312 }
313 interfaces = List.copyOf(itfs);
314 }
315 final boolean finalAccidentallySerializable = accidentallySerializable;
316 final Object[] additionalClassDataHolder = new Object[1];
317 final byte[] classBytes = ClassFile.of().build(lambdaClassEntry, pool, new Consumer<ClassBuilder>() {
318 @Override
319 public void accept(ClassBuilder clb) {
320 clb.withFlags(ACC_SUPER | ACC_FINAL | ACC_SYNTHETIC)
321 .withInterfaceSymbols(interfaces);
322 // All Classes in the BSM argument method types are loaded; no need for LoadableDescriptors
323
324 // Generate final fields to be filled in by constructor
325 for (int i = 0; i < argDescs.length; i++) {
326 clb.withField(argName(i), argDescs[i], ACC_PRIVATE | ACC_FINAL);
327 }
328
329 generateConstructor(clb);
330
331 if (factoryType.parameterCount() == 0 && disableEagerInitialization) {
332 generateClassInitializer(clb);
333 }
334
335 // Forward the SAM method
336 clb.withMethodBody(interfaceMethodName,
337 methodDesc(interfaceMethodType),
338 ACC_PUBLIC,
339 forwardingMethod(interfaceMethodType));
340
341 // Forward the bridges
342 if (altMethods != null) {
343 for (MethodType mt : altMethods) {
344 clb.withMethodBody(interfaceMethodName,
345 methodDesc(mt),
346 ACC_PUBLIC | ACC_BRIDGE,
347 forwardingMethod(mt));
348 }
349 }
350
351 if (isSerializable)
352 generateSerializationFriendlyMethods(clb);
353 else if (finalAccidentallySerializable)
354 generateSerializationHostileMethods(clb);
355
356 if (finisher != null) {
357 additionalClassDataHolder[0] = finisher.apply(clb);
358 }
359 }
360 });
361
362 // Define the generated class in this VM.
363
364 try {
365 // this class is linked at the indy callsite; so define a hidden nestmate
366 var additionalClassData = additionalClassDataHolder[0];
367 var classdata = additionalClassData == null
368 ? useImplMethodHandle ? List.of(implementation) : null
369 : useImplMethodHandle ? List.of(implementation, additionalClassData) : List.of(additionalClassData);
370 return caller.makeHiddenClassDefiner(lambdaClassName, classBytes, lambdaProxyClassFileDumper, NESTMATE_CLASS | STRONG_LOADER_LINK)
371 .defineClass(!disableEagerInitialization, classdata);
372
373 } catch (Throwable t) {
374 throw new InternalError(t);
375 }
376 }
377
378 /**
379 * Generate a static field and a static initializer that sets this field to an instance of the lambda
380 */
381 private void generateClassInitializer(ClassBuilder clb) {
382 ClassDesc lambdaTypeDescriptor = classDesc(factoryType.returnType());
383
384 // Generate the static final field that holds the lambda singleton
385 clb.withField(LAMBDA_INSTANCE_FIELD, lambdaTypeDescriptor, ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
386
387 // Instantiate the lambda and store it to the static final field
388 clb.withMethodBody(CLASS_INIT_NAME, MTD_void, ACC_STATIC, new Consumer<>() {
389 @Override
502 clb.withMethod(SerializationSupport.NAME_METHOD_WRITE_OBJECT, SerializationSupport.MTD_void_ObjectOutputStream,
503 ACC_PRIVATE + ACC_FINAL, hostileMethod);
504 clb.withMethod(SerializationSupport.NAME_METHOD_READ_OBJECT, SerializationSupport.MTD_void_ObjectInputStream,
505 ACC_PRIVATE + ACC_FINAL, hostileMethod);
506 }
507
508 /**
509 * This method generates a method body which calls the lambda implementation
510 * method, converting arguments, as needed.
511 */
512 Consumer<CodeBuilder> forwardingMethod(MethodType methodType) {
513 return new Consumer<>() {
514 @Override
515 public void accept(CodeBuilder cob) {
516 if (implKind == MethodHandleInfo.REF_newInvokeSpecial) {
517 cob.new_(implMethodClassDesc)
518 .dup();
519 }
520 if (useImplMethodHandle) {
521 ConstantPoolBuilder cp = cob.constantPool();
522 cob.ldc(cp.constantDynamicEntry(cp.bsmEntry(cp.methodHandleEntry(BSM_CLASS_DATA_AT), List.of(cp.intEntry(0))),
523 cp.nameAndTypeEntry(DEFAULT_NAME, CD_MethodHandle)));
524 }
525 for (int i = 0; i < argDescs.length; i++) {
526 cob.aload(0)
527 .getfield(pool.fieldRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(argName(i), argDescs[i])));
528 }
529
530 convertArgumentTypes(cob, methodType);
531
532 if (useImplMethodHandle) {
533 MethodType mtype = implInfo.getMethodType();
534 if (implKind != MethodHandleInfo.REF_invokeStatic) {
535 mtype = mtype.insertParameterTypes(0, implClass);
536 }
537 cob.invokevirtual(CD_MethodHandle, "invokeExact", methodDesc(mtype));
538 } else {
539 // Invoke the method we want to forward to
540 cob.invoke(invocationOpcode(), implMethodClassDesc, implMethodName, implMethodDesc, implClass.isInterface());
541 }
542 // Convert the return value (if any) and return it
|