< prev index next >

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

Print this page

  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:
 47      * interface II<T> {  Object foo(T x); }
 48      * interface JJ<R extends Number> extends II<R> { }
 49      * class CC {  String impl(int i) { return "impl:"+i; }}

 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) {

  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.classfile.ClassBuilder;
 30 import java.lang.reflect.Modifier;
 31 import java.util.function.Function;
 32 
 33 import static java.lang.invoke.MethodHandleInfo.*;
 34 import static sun.invoke.util.Wrapper.forPrimitiveType;
 35 import static sun.invoke.util.Wrapper.forWrapperType;
 36 import static sun.invoke.util.Wrapper.isWrapperType;
 37 
 38 /**
 39  * Abstract implementation of a lambda metafactory which provides parameter
 40  * unrolling and input validation.
 41  *
 42  * @see LambdaMetafactory
 43  */
 44 /* package */ abstract class AbstractValidatingLambdaMetafactory {
 45 
 46     /*
 47      * For context, the comments for the following fields are marked in quotes
 48      * with their values, given this program:
 49      * interface II<T> {  Object foo(T x); }
 50      * interface JJ<R extends Number> extends II<R> { }
 51      * class CC {  String impl(int i) { return "impl:"+i; }}

 54      *         JJ<Integer> iii = (new CC())::impl;
 55      *         System.out.printf(">>> %s\n", iii.foo(44));
 56      * }}
 57      */
 58     final MethodHandles.Lookup caller;        // The caller's lookup context
 59     final Class<?> targetClass;               // The class calling the meta-factory via invokedynamic "class X"
 60     final MethodType factoryType;             // The type of the invoked method "(CC)II"
 61     final Class<?> interfaceClass;            // The type of the returned instance "interface JJ"
 62     final String interfaceMethodName;         // Name of the method to implement "foo"
 63     final MethodType interfaceMethodType;     // Type of the method to implement "(Object)Object"
 64     final MethodHandle implementation;        // Raw method handle for the implementation method
 65     final MethodType implMethodType;          // Type of the implementation MethodHandle "(CC,int)String"
 66     final MethodHandleInfo implInfo;          // Info about the implementation method handle "MethodHandleInfo[5 CC.impl(int)String]"
 67     final int implKind;                       // Invocation kind for implementation "5"=invokevirtual
 68     final boolean implIsInstanceMethod;       // Is the implementation an instance method "true"
 69     final Class<?> implClass;                 // Class for referencing the implementation method "class CC"
 70     final MethodType dynamicMethodType;       // Dynamically checked method type "(Integer)Object"
 71     final boolean isSerializable;             // Should the returned instance be serializable
 72     final Class<?>[] altInterfaces;           // Additional interfaces to be implemented
 73     final MethodType[] altMethods;            // Signatures of additional methods to bridge
 74     final Function<ClassBuilder, Object> finisher; // Function called to finish lambda class build process, returns additional class data (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 finisher Function called at the end of the lambda class build process
111      *                 that returns an additional object to append to the class data,
112      *                 may be (@code null}, may return {@code null}.
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                                         Function<ClassBuilder, Object> finisher)
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.finisher = finisher;
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) {
< prev index next >