52 * JJ<Integer> iii = (new CC())::impl;
53 * System.out.printf(">>> %s\n", iii.foo(44));
54 * }}
55 */
56 final MethodHandles.Lookup caller; // The caller's lookup context
57 final Class<?> targetClass; // The class calling the meta-factory via invokedynamic "class X"
58 final MethodType factoryType; // The type of the invoked method "(CC)II"
59 final Class<?> interfaceClass; // The type of the returned instance "interface JJ"
60 final String interfaceMethodName; // Name of the method to implement "foo"
61 final MethodType interfaceMethodType; // Type of the method to implement "(Object)Object"
62 final MethodHandle implementation; // Raw method handle for the implementation method
63 final MethodType implMethodType; // Type of the implementation MethodHandle "(CC,int)String"
64 final MethodHandleInfo implInfo; // Info about the implementation method handle "MethodHandleInfo[5 CC.impl(int)String]"
65 final int implKind; // Invocation kind for implementation "5"=invokevirtual
66 final boolean implIsInstanceMethod; // Is the implementation an instance method "true"
67 final Class<?> implClass; // Class for referencing the implementation method "class CC"
68 final MethodType dynamicMethodType; // Dynamically checked method type "(Integer)Object"
69 final boolean isSerializable; // Should the returned instance be serializable
70 final Class<?>[] altInterfaces; // Additional interfaces to be implemented
71 final MethodType[] altMethods; // Signatures of additional methods to bridge
72
73
74 /**
75 * Meta-factory constructor.
76 *
77 * @param caller Stacked automatically by VM; represents a lookup context
78 * with the accessibility privileges of the caller.
79 * @param factoryType Stacked automatically by VM; the signature of the
80 * invoked method, which includes the expected static
81 * type of the returned lambda object, and the static
82 * types of the captured arguments for the lambda. In
83 * the event that the implementation method is an
84 * instance method, the first argument in the invocation
85 * signature will correspond to the receiver.
86 * @param interfaceMethodName Name of the method in the functional interface to
87 * which the lambda or method reference is being
88 * converted, represented as a String.
89 * @param interfaceMethodType Type of the method in the functional interface to
90 * which the lambda or method reference is being
91 * converted, represented as a MethodType.
92 * @param implementation The implementation method which should be called
93 * (with suitable adaptation of argument types, return
94 * types, and adjustment for captured arguments) when
95 * methods of the resulting functional interface instance
96 * are invoked.
97 * @param dynamicMethodType The signature of the primary functional
98 * interface method after type variables are
99 * substituted with their instantiation from
100 * the capture site
101 * @param isSerializable Should the lambda be made serializable? If set,
102 * either the target type or one of the additional SAM
103 * types must extend {@code Serializable}.
104 * @param altInterfaces Additional interfaces which the lambda object
105 * should implement.
106 * @param altMethods Method types for additional signatures to be
107 * implemented by invoking the implementation method
108 * @throws LambdaConversionException If any of the meta-factory protocol
109 * invariants are violated
110 */
111 AbstractValidatingLambdaMetafactory(MethodHandles.Lookup caller,
112 MethodType factoryType,
113 String interfaceMethodName,
114 MethodType interfaceMethodType,
115 MethodHandle implementation,
116 MethodType dynamicMethodType,
117 boolean isSerializable,
118 Class<?>[] altInterfaces,
119 MethodType[] altMethods)
120 throws LambdaConversionException {
121 if (!caller.hasFullPrivilegeAccess()) {
122 throw new LambdaConversionException(String.format(
123 "Invalid caller: %s",
124 caller.lookupClass().getName()));
125 }
126 this.caller = caller;
127 this.targetClass = caller.lookupClass();
128 this.factoryType = factoryType;
129
130 this.interfaceClass = factoryType.returnType();
131
132 this.interfaceMethodName = interfaceMethodName;
133 this.interfaceMethodType = interfaceMethodType;
134
135 this.implementation = implementation;
136 this.implMethodType = implementation.type();
137 try {
138 this.implInfo = caller.revealDirect(implementation);
139 } catch (IllegalArgumentException e) {
160 this.implKind = implClass.isInterface() ? REF_invokeInterface : REF_invokeVirtual;
161 } else {
162 this.implKind = REF_invokeSpecial;
163 }
164 break;
165 case REF_invokeStatic:
166 case REF_newInvokeSpecial:
167 // JDK-8172817: should use referenced class here for invokestatic, but we don't know what it was
168 this.implClass = implInfo.getDeclaringClass();
169 this.implKind = implInfo.getReferenceKind();
170 this.implIsInstanceMethod = false;
171 break;
172 default:
173 throw new LambdaConversionException(String.format("Unsupported MethodHandle kind: %s", implInfo));
174 }
175
176 this.dynamicMethodType = dynamicMethodType;
177 this.isSerializable = isSerializable;
178 this.altInterfaces = altInterfaces;
179 this.altMethods = altMethods;
180
181 if (interfaceMethodName.isEmpty() ||
182 interfaceMethodName.indexOf('.') >= 0 ||
183 interfaceMethodName.indexOf(';') >= 0 ||
184 interfaceMethodName.indexOf('[') >= 0 ||
185 interfaceMethodName.indexOf('/') >= 0 ||
186 interfaceMethodName.indexOf('<') >= 0 ||
187 interfaceMethodName.indexOf('>') >= 0) {
188 throw new LambdaConversionException(String.format(
189 "Method name '%s' is not legal",
190 interfaceMethodName));
191 }
192
193 if (!interfaceClass.isInterface()) {
194 throw new LambdaConversionException(String.format(
195 "%s is not an interface",
196 interfaceClass.getName()));
197 }
198
199 for (Class<?> c : altInterfaces) {
200 if (!c.isInterface()) {
201 throw new LambdaConversionException(String.format(
202 "%s is not an interface",
203 c.getName()));
204 }
205 }
206 }
207
208 /**
209 * Build the CallSite.
210 *
211 * @return a CallSite, which, when invoked, will return an instance of the
212 * functional interface
213 * @throws LambdaConversionException
214 */
215 abstract CallSite buildCallSite()
216 throws LambdaConversionException;
217
218 /**
219 * Check the meta-factory arguments for errors
220 * @throws LambdaConversionException if there are improper conversions
221 */
222 void validateMetafactoryArgs() throws LambdaConversionException {
223 // Check arity: captured + SAM == impl
224 final int implArity = implMethodType.parameterCount();
225 final int capturedArity = factoryType.parameterCount();
|
52 * JJ<Integer> iii = (new CC())::impl;
53 * System.out.printf(">>> %s\n", iii.foo(44));
54 * }}
55 */
56 final MethodHandles.Lookup caller; // The caller's lookup context
57 final Class<?> targetClass; // The class calling the meta-factory via invokedynamic "class X"
58 final MethodType factoryType; // The type of the invoked method "(CC)II"
59 final Class<?> interfaceClass; // The type of the returned instance "interface JJ"
60 final String interfaceMethodName; // Name of the method to implement "foo"
61 final MethodType interfaceMethodType; // Type of the method to implement "(Object)Object"
62 final MethodHandle implementation; // Raw method handle for the implementation method
63 final MethodType implMethodType; // Type of the implementation MethodHandle "(CC,int)String"
64 final MethodHandleInfo implInfo; // Info about the implementation method handle "MethodHandleInfo[5 CC.impl(int)String]"
65 final int implKind; // Invocation kind for implementation "5"=invokevirtual
66 final boolean implIsInstanceMethod; // Is the implementation an instance method "true"
67 final Class<?> implClass; // Class for referencing the implementation method "class CC"
68 final MethodType dynamicMethodType; // Dynamically checked method type "(Integer)Object"
69 final boolean isSerializable; // Should the returned instance be serializable
70 final Class<?>[] altInterfaces; // Additional interfaces to be implemented
71 final MethodType[] altMethods; // Signatures of additional methods to bridge
72 final MethodHandle quotableOpGetter; // A getter method handle that is used to retrieve the
73 // the quotable lambda's associated intermediate representation (can be null).
74 final MethodHandleInfo quotableOpGetterInfo; // Info about the quotable getter method handle (can be null).
75
76 /**
77 * Meta-factory constructor.
78 *
79 * @param caller Stacked automatically by VM; represents a lookup context
80 * with the accessibility privileges of the caller.
81 * @param factoryType Stacked automatically by VM; the signature of the
82 * invoked method, which includes the expected static
83 * type of the returned lambda object, and the static
84 * types of the captured arguments for the lambda. In
85 * the event that the implementation method is an
86 * instance method, the first argument in the invocation
87 * signature will correspond to the receiver.
88 * @param interfaceMethodName Name of the method in the functional interface to
89 * which the lambda or method reference is being
90 * converted, represented as a String.
91 * @param interfaceMethodType Type of the method in the functional interface to
92 * which the lambda or method reference is being
93 * converted, represented as a MethodType.
94 * @param implementation The implementation method which should be called
95 * (with suitable adaptation of argument types, return
96 * types, and adjustment for captured arguments) when
97 * methods of the resulting functional interface instance
98 * are invoked.
99 * @param dynamicMethodType The signature of the primary functional
100 * interface method after type variables are
101 * substituted with their instantiation from
102 * the capture site
103 * @param isSerializable Should the lambda be made serializable? If set,
104 * either the target type or one of the additional SAM
105 * types must extend {@code Serializable}.
106 * @param altInterfaces Additional interfaces which the lambda object
107 * should implement.
108 * @param altMethods Method types for additional signatures to be
109 * implemented by invoking the implementation method
110 * @param reflectiveField a {@linkplain MethodHandles.Lookup#findGetter(Class, String, Class) getter}
111 * method handle that is used to retrieve the string representation of the
112 * quotable lambda's associated intermediate representation.
113 * @throws LambdaConversionException If any of the meta-factory protocol
114 * invariants are violated
115 */
116 AbstractValidatingLambdaMetafactory(MethodHandles.Lookup caller,
117 MethodType factoryType,
118 String interfaceMethodName,
119 MethodType interfaceMethodType,
120 MethodHandle implementation,
121 MethodType dynamicMethodType,
122 boolean isSerializable,
123 Class<?>[] altInterfaces,
124 MethodType[] altMethods,
125 MethodHandle reflectiveField)
126 throws LambdaConversionException {
127 if (!caller.hasFullPrivilegeAccess()) {
128 throw new LambdaConversionException(String.format(
129 "Invalid caller: %s",
130 caller.lookupClass().getName()));
131 }
132 this.caller = caller;
133 this.targetClass = caller.lookupClass();
134 this.factoryType = factoryType;
135
136 this.interfaceClass = factoryType.returnType();
137
138 this.interfaceMethodName = interfaceMethodName;
139 this.interfaceMethodType = interfaceMethodType;
140
141 this.implementation = implementation;
142 this.implMethodType = implementation.type();
143 try {
144 this.implInfo = caller.revealDirect(implementation);
145 } catch (IllegalArgumentException e) {
166 this.implKind = implClass.isInterface() ? REF_invokeInterface : REF_invokeVirtual;
167 } else {
168 this.implKind = REF_invokeSpecial;
169 }
170 break;
171 case REF_invokeStatic:
172 case REF_newInvokeSpecial:
173 // JDK-8172817: should use referenced class here for invokestatic, but we don't know what it was
174 this.implClass = implInfo.getDeclaringClass();
175 this.implKind = implInfo.getReferenceKind();
176 this.implIsInstanceMethod = false;
177 break;
178 default:
179 throw new LambdaConversionException(String.format("Unsupported MethodHandle kind: %s", implInfo));
180 }
181
182 this.dynamicMethodType = dynamicMethodType;
183 this.isSerializable = isSerializable;
184 this.altInterfaces = altInterfaces;
185 this.altMethods = altMethods;
186 this.quotableOpGetter = reflectiveField;
187
188 if (interfaceMethodName.isEmpty() ||
189 interfaceMethodName.indexOf('.') >= 0 ||
190 interfaceMethodName.indexOf(';') >= 0 ||
191 interfaceMethodName.indexOf('[') >= 0 ||
192 interfaceMethodName.indexOf('/') >= 0 ||
193 interfaceMethodName.indexOf('<') >= 0 ||
194 interfaceMethodName.indexOf('>') >= 0) {
195 throw new LambdaConversionException(String.format(
196 "Method name '%s' is not legal",
197 interfaceMethodName));
198 }
199
200 if (!interfaceClass.isInterface()) {
201 throw new LambdaConversionException(String.format(
202 "%s is not an interface",
203 interfaceClass.getName()));
204 }
205
206 for (Class<?> c : altInterfaces) {
207 if (!c.isInterface()) {
208 throw new LambdaConversionException(String.format(
209 "%s is not an interface",
210 c.getName()));
211 }
212 }
213
214 if (reflectiveField != null) {
215 try {
216 quotableOpGetterInfo = caller.revealDirect(reflectiveField); // may throw SecurityException
217 } catch (IllegalArgumentException e) {
218 throw new LambdaConversionException(implementation + " is not direct or cannot be cracked");
219 }
220 if (quotableOpGetterInfo.getReferenceKind() != REF_invokeStatic) {
221 throw new LambdaConversionException(String.format("Unsupported MethodHandle kind: %s", quotableOpGetterInfo));
222 }
223 } else {
224 quotableOpGetterInfo = null;
225 }
226 }
227
228 /**
229 * Build the CallSite.
230 *
231 * @return a CallSite, which, when invoked, will return an instance of the
232 * functional interface
233 * @throws LambdaConversionException
234 */
235 abstract CallSite buildCallSite()
236 throws LambdaConversionException;
237
238 /**
239 * Check the meta-factory arguments for errors
240 * @throws LambdaConversionException if there are improper conversions
241 */
242 void validateMetafactoryArgs() throws LambdaConversionException {
243 // Check arity: captured + SAM == impl
244 final int implArity = implMethodType.parameterCount();
245 final int capturedArity = factoryType.parameterCount();
|