< prev index next >

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

Print this page




3308      * If the requested type is primitive, widening primitive conversions are attempted,
3309      * else reference conversions are attempted.
3310      * <p>The returned method handle is equivalent to {@code identity(type).bindTo(value)}.
3311      * @param type the return type of the desired method handle
3312      * @param value the value to return
3313      * @return a method handle of the given return type and no arguments, which always returns the given value
3314      * @throws NullPointerException if the {@code type} argument is null
3315      * @throws ClassCastException if the value cannot be converted to the required return type
3316      * @throws IllegalArgumentException if the given type is {@code void.class}
3317      */
3318     public static
3319     MethodHandle constant(Class<?> type, Object value) {
3320         if (type.isPrimitive()) {
3321             if (type == void.class)
3322                 throw newIllegalArgumentException("void type");
3323             Wrapper w = Wrapper.forPrimitiveType(type);
3324             value = w.convert(value, type);
3325             if (w.zero().equals(value))
3326                 return zero(w, type);
3327             return insertArguments(identity(type), 0, value);


3328         } else {
3329             if (value == null)
3330                 return zero(Wrapper.OBJECT, type);
3331             return identity(type).bindTo(value);
3332         }
3333     }
3334 
3335     /**
3336      * Produces a method handle which returns its sole argument when invoked.
3337      * @param type the type of the sole parameter and return value of the desired method handle
3338      * @return a unary method handle which accepts and returns the given type
3339      * @throws NullPointerException if the argument is null
3340      * @throws IllegalArgumentException if the given type is {@code void.class}
3341      */
3342     public static
3343     MethodHandle identity(Class<?> type) {
3344         Wrapper btw = (type.isPrimitive() ? Wrapper.forPrimitiveType(type) : Wrapper.OBJECT);
3345         int pos = btw.ordinal();
3346         MethodHandle ident = IDENTITY_MHS[pos];
3347         if (ident == null) {




3308      * If the requested type is primitive, widening primitive conversions are attempted,
3309      * else reference conversions are attempted.
3310      * <p>The returned method handle is equivalent to {@code identity(type).bindTo(value)}.
3311      * @param type the return type of the desired method handle
3312      * @param value the value to return
3313      * @return a method handle of the given return type and no arguments, which always returns the given value
3314      * @throws NullPointerException if the {@code type} argument is null
3315      * @throws ClassCastException if the value cannot be converted to the required return type
3316      * @throws IllegalArgumentException if the given type is {@code void.class}
3317      */
3318     public static
3319     MethodHandle constant(Class<?> type, Object value) {
3320         if (type.isPrimitive()) {
3321             if (type == void.class)
3322                 throw newIllegalArgumentException("void type");
3323             Wrapper w = Wrapper.forPrimitiveType(type);
3324             value = w.convert(value, type);
3325             if (w.zero().equals(value))
3326                 return zero(w, type);
3327             return insertArguments(identity(type), 0, value);
3328         } else if (MinimalValueTypes_1_0.isValueType(type)) {
3329             return insertArguments(identity(type), 0, value);
3330         } else {
3331             if (value == null)
3332                 return zero(Wrapper.OBJECT, type);
3333             return identity(type).bindTo(value);
3334         }
3335     }
3336 
3337     /**
3338      * Produces a method handle which returns its sole argument when invoked.
3339      * @param type the type of the sole parameter and return value of the desired method handle
3340      * @return a unary method handle which accepts and returns the given type
3341      * @throws NullPointerException if the argument is null
3342      * @throws IllegalArgumentException if the given type is {@code void.class}
3343      */
3344     public static
3345     MethodHandle identity(Class<?> type) {
3346         Wrapper btw = (type.isPrimitive() ? Wrapper.forPrimitiveType(type) : Wrapper.OBJECT);
3347         int pos = btw.ordinal();
3348         MethodHandle ident = IDENTITY_MHS[pos];
3349         if (ident == null) {


< prev index next >