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 // 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
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
|
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 // Generate final fields to be filled in by constructor
323 for (int i = 0; i < argDescs.length; i++) {
324 clb.withField(argName(i), argDescs[i], ACC_PRIVATE | ACC_FINAL);
325 }
326
327 generateConstructor(clb);
328
329 if (factoryType.parameterCount() == 0 && disableEagerInitialization) {
330 generateClassInitializer(clb);
331 }
332
333 // Forward the SAM method
334 clb.withMethodBody(interfaceMethodName,
335 methodDesc(interfaceMethodType),
336 ACC_PUBLIC,
337 forwardingMethod(interfaceMethodType));
338
339 // Forward the bridges
340 if (altMethods != null) {
341 for (MethodType mt : altMethods) {
342 clb.withMethodBody(interfaceMethodName,
343 methodDesc(mt),
344 ACC_PUBLIC | ACC_BRIDGE,
345 forwardingMethod(mt));
346 }
347 }
348
349 if (isSerializable)
350 generateSerializationFriendlyMethods(clb);
351 else if (finalAccidentallySerializable)
352 generateSerializationHostileMethods(clb);
353
354 if (finisher != null) {
355 additionalClassDataHolder[0] = finisher.apply(clb);
356 }
357 }
358 });
359
360 // Define the generated class in this VM.
361
362 try {
363 // this class is linked at the indy callsite; so define a hidden nestmate
364 var additionalClassData = additionalClassDataHolder[0];
365 var classdata = additionalClassData == null
366 ? useImplMethodHandle ? List.of(implementation) : null
367 : useImplMethodHandle ? List.of(implementation, additionalClassData) : List.of(additionalClassData);
368 return caller.makeHiddenClassDefiner(lambdaClassName, classBytes, lambdaProxyClassFileDumper, NESTMATE_CLASS | STRONG_LOADER_LINK)
369 .defineClass(!disableEagerInitialization, classdata);
370
371 } catch (Throwable t) {
372 throw new InternalError(t);
373 }
374 }
375
376 /**
377 * Generate a static field and a static initializer that sets this field to an instance of the lambda
378 */
379 private void generateClassInitializer(ClassBuilder clb) {
380 ClassDesc lambdaTypeDescriptor = classDesc(factoryType.returnType());
381
382 // Generate the static final field that holds the lambda singleton
383 clb.withField(LAMBDA_INSTANCE_FIELD, lambdaTypeDescriptor, ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
384
385 // Instantiate the lambda and store it to the static final field
386 clb.withMethodBody(CLASS_INIT_NAME, MTD_void, ACC_STATIC, new Consumer<>() {
387 @Override
500 clb.withMethod(SerializationSupport.NAME_METHOD_WRITE_OBJECT, SerializationSupport.MTD_void_ObjectOutputStream,
501 ACC_PRIVATE + ACC_FINAL, hostileMethod);
502 clb.withMethod(SerializationSupport.NAME_METHOD_READ_OBJECT, SerializationSupport.MTD_void_ObjectInputStream,
503 ACC_PRIVATE + ACC_FINAL, hostileMethod);
504 }
505
506 /**
507 * This method generates a method body which calls the lambda implementation
508 * method, converting arguments, as needed.
509 */
510 Consumer<CodeBuilder> forwardingMethod(MethodType methodType) {
511 return new Consumer<>() {
512 @Override
513 public void accept(CodeBuilder cob) {
514 if (implKind == MethodHandleInfo.REF_newInvokeSpecial) {
515 cob.new_(implMethodClassDesc)
516 .dup();
517 }
518 if (useImplMethodHandle) {
519 ConstantPoolBuilder cp = cob.constantPool();
520 cob.ldc(cp.constantDynamicEntry(cp.bsmEntry(cp.methodHandleEntry(BSM_CLASS_DATA_AT), List.of(cp.intEntry(0))),
521 cp.nameAndTypeEntry(DEFAULT_NAME, CD_MethodHandle)));
522 }
523 for (int i = 0; i < argDescs.length; i++) {
524 cob.aload(0)
525 .getfield(pool.fieldRefEntry(lambdaClassEntry, pool.nameAndTypeEntry(argName(i), argDescs[i])));
526 }
527
528 convertArgumentTypes(cob, methodType);
529
530 if (useImplMethodHandle) {
531 MethodType mtype = implInfo.getMethodType();
532 if (implKind != MethodHandleInfo.REF_invokeStatic) {
533 mtype = mtype.insertParameterTypes(0, implClass);
534 }
535 cob.invokevirtual(CD_MethodHandle, "invokeExact", methodDesc(mtype));
536 } else {
537 // Invoke the method we want to forward to
538 cob.invoke(invocationOpcode(), implMethodClassDesc, implMethodName, implMethodDesc, implClass.isInterface());
539 }
540 // Convert the return value (if any) and return it
|