7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package java.lang.invoke;
26
27 import sun.invoke.util.Wrapper;
28
29 import java.lang.reflect.Modifier;
30
31 import static java.lang.invoke.MethodHandleInfo.*;
32 import static sun.invoke.util.Wrapper.forPrimitiveType;
33 import static sun.invoke.util.Wrapper.forWrapperType;
34 import static sun.invoke.util.Wrapper.isWrapperType;
35
36 /**
37 * Abstract implementation of a lambda metafactory which provides parameter
38 * unrolling and input validation.
39 *
40 * @see LambdaMetafactory
41 */
42 /* package */ abstract class AbstractValidatingLambdaMetafactory {
43
44 /*
45 * For context, the comments for the following fields are marked in quotes
46 * with their values, given this program:
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();
|
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package java.lang.invoke;
26
27 import jdk.internal.access.JavaLangInvokeAccess.ReflectableLambdaInfo;
28 import sun.invoke.util.Wrapper;
29
30 import java.lang.reflect.Modifier;
31
32 import static java.lang.invoke.MethodHandleInfo.*;
33 import static sun.invoke.util.Wrapper.forPrimitiveType;
34 import static sun.invoke.util.Wrapper.forWrapperType;
35 import static sun.invoke.util.Wrapper.isWrapperType;
36
37 /**
38 * Abstract implementation of a lambda metafactory which provides parameter
39 * unrolling and input validation.
40 *
41 * @see LambdaMetafactory
42 */
43 /* package */ abstract class AbstractValidatingLambdaMetafactory {
44
45 /*
46 * For context, the comments for the following fields are marked in quotes
47 * with their values, given this program:
53 * JJ<Integer> iii = (new CC())::impl;
54 * System.out.printf(">>> %s\n", iii.foo(44));
55 * }}
56 */
57 final MethodHandles.Lookup caller; // The caller's lookup context
58 final Class<?> targetClass; // The class calling the meta-factory via invokedynamic "class X"
59 final MethodType factoryType; // The type of the invoked method "(CC)II"
60 final Class<?> interfaceClass; // The type of the returned instance "interface JJ"
61 final String interfaceMethodName; // Name of the method to implement "foo"
62 final MethodType interfaceMethodType; // Type of the method to implement "(Object)Object"
63 final MethodHandle implementation; // Raw method handle for the implementation method
64 final MethodType implMethodType; // Type of the implementation MethodHandle "(CC,int)String"
65 final MethodHandleInfo implInfo; // Info about the implementation method handle "MethodHandleInfo[5 CC.impl(int)String]"
66 final int implKind; // Invocation kind for implementation "5"=invokevirtual
67 final boolean implIsInstanceMethod; // Is the implementation an instance method "true"
68 final Class<?> implClass; // Class for referencing the implementation method "class CC"
69 final MethodType dynamicMethodType; // Dynamically checked method type "(Integer)Object"
70 final boolean isSerializable; // Should the returned instance be serializable
71 final Class<?>[] altInterfaces; // Additional interfaces to be implemented
72 final MethodType[] altMethods; // Signatures of additional methods to bridge
73 final ReflectableLambdaInfo reflectableLambdaInfo; // A holder for information pertinent to a reflectable lambda
74 // the quotable lambda's associated intermediate representation (can be null).
75 final MethodHandleInfo quotableOpGetterInfo; // Info about the quotable getter method handle (can be null).
76
77 /**
78 * Meta-factory constructor.
79 *
80 * @param caller Stacked automatically by VM; represents a lookup context
81 * with the accessibility privileges of the caller.
82 * @param factoryType Stacked automatically by VM; the signature of the
83 * invoked method, which includes the expected static
84 * type of the returned lambda object, and the static
85 * types of the captured arguments for the lambda. In
86 * the event that the implementation method is an
87 * instance method, the first argument in the invocation
88 * signature will correspond to the receiver.
89 * @param interfaceMethodName Name of the method in the functional interface to
90 * which the lambda or method reference is being
91 * converted, represented as a String.
92 * @param interfaceMethodType Type of the method in the functional interface to
93 * which the lambda or method reference is being
94 * converted, represented as a MethodType.
95 * @param implementation The implementation method which should be called
96 * (with suitable adaptation of argument types, return
97 * types, and adjustment for captured arguments) when
98 * methods of the resulting functional interface instance
99 * are invoked.
100 * @param dynamicMethodType The signature of the primary functional
101 * interface method after type variables are
102 * substituted with their instantiation from
103 * the capture site
104 * @param isSerializable Should the lambda be made serializable? If set,
105 * either the target type or one of the additional SAM
106 * types must extend {@code Serializable}.
107 * @param altInterfaces Additional interfaces which the lambda object
108 * should implement.
109 * @param altMethods Method types for additional signatures to be
110 * implemented by invoking the implementation method
111 * @param reflectableLambdaInfo a holder for information pertinent to a reflectable lambda
112 * @throws LambdaConversionException If any of the meta-factory protocol
113 * invariants are violated
114 */
115 AbstractValidatingLambdaMetafactory(MethodHandles.Lookup caller,
116 MethodType factoryType,
117 String interfaceMethodName,
118 MethodType interfaceMethodType,
119 MethodHandle implementation,
120 MethodType dynamicMethodType,
121 boolean isSerializable,
122 Class<?>[] altInterfaces,
123 MethodType[] altMethods,
124 ReflectableLambdaInfo reflectableLambdaInfo)
125 throws LambdaConversionException {
126 if (!caller.hasFullPrivilegeAccess()) {
127 throw new LambdaConversionException(String.format(
128 "Invalid caller: %s",
129 caller.lookupClass().getName()));
130 }
131 this.caller = caller;
132 this.targetClass = caller.lookupClass();
133 this.factoryType = factoryType;
134
135 this.interfaceClass = factoryType.returnType();
136
137 this.interfaceMethodName = interfaceMethodName;
138 this.interfaceMethodType = interfaceMethodType;
139
140 this.implementation = implementation;
141 this.implMethodType = implementation.type();
142 try {
143 this.implInfo = caller.revealDirect(implementation);
144 } catch (IllegalArgumentException e) {
165 this.implKind = implClass.isInterface() ? REF_invokeInterface : REF_invokeVirtual;
166 } else {
167 this.implKind = REF_invokeSpecial;
168 }
169 break;
170 case REF_invokeStatic:
171 case REF_newInvokeSpecial:
172 // JDK-8172817: should use referenced class here for invokestatic, but we don't know what it was
173 this.implClass = implInfo.getDeclaringClass();
174 this.implKind = implInfo.getReferenceKind();
175 this.implIsInstanceMethod = false;
176 break;
177 default:
178 throw new LambdaConversionException(String.format("Unsupported MethodHandle kind: %s", implInfo));
179 }
180
181 this.dynamicMethodType = dynamicMethodType;
182 this.isSerializable = isSerializable;
183 this.altInterfaces = altInterfaces;
184 this.altMethods = altMethods;
185 this.reflectableLambdaInfo = reflectableLambdaInfo;
186
187 if (interfaceMethodName.isEmpty() ||
188 interfaceMethodName.indexOf('.') >= 0 ||
189 interfaceMethodName.indexOf(';') >= 0 ||
190 interfaceMethodName.indexOf('[') >= 0 ||
191 interfaceMethodName.indexOf('/') >= 0 ||
192 interfaceMethodName.indexOf('<') >= 0 ||
193 interfaceMethodName.indexOf('>') >= 0) {
194 throw new LambdaConversionException(String.format(
195 "Method name '%s' is not legal",
196 interfaceMethodName));
197 }
198
199 if (!interfaceClass.isInterface()) {
200 throw new LambdaConversionException(String.format(
201 "%s is not an interface",
202 interfaceClass.getName()));
203 }
204
205 for (Class<?> c : altInterfaces) {
206 if (!c.isInterface()) {
207 throw new LambdaConversionException(String.format(
208 "%s is not an interface",
209 c.getName()));
210 }
211 }
212
213 if (reflectableLambdaInfo != null) {
214 try {
215 quotableOpGetterInfo = caller.revealDirect(reflectableLambdaInfo.opHandle()); // may throw SecurityException
216 } catch (IllegalArgumentException e) {
217 throw new LambdaConversionException(implementation + " is not direct or cannot be cracked");
218 }
219 if (quotableOpGetterInfo.getReferenceKind() != REF_invokeStatic) {
220 throw new LambdaConversionException(String.format("Unsupported MethodHandle kind: %s", quotableOpGetterInfo));
221 }
222 } else {
223 quotableOpGetterInfo = null;
224 }
225 }
226
227 /**
228 * Build the CallSite.
229 *
230 * @return a CallSite, which, when invoked, will return an instance of the
231 * functional interface
232 * @throws LambdaConversionException
233 */
234 abstract CallSite buildCallSite()
235 throws LambdaConversionException;
236
237 /**
238 * Check the meta-factory arguments for errors
239 * @throws LambdaConversionException if there are improper conversions
240 */
241 void validateMetafactoryArgs() throws LambdaConversionException {
242 // Check arity: captured + SAM == impl
243 final int implArity = implMethodType.parameterCount();
244 final int capturedArity = factoryType.parameterCount();
|