【Java】LambdaMetaFactoryKit

LambdaMetaFactoryKit

import java.lang.invoke.*;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.function.*;

public class LambdaMetaFactoryKit {

    // region supplier

    public static <R> Supplier<R> toSupplier(Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "get",
                MethodType.methodType(Supplier.class),
                methodHandle.type().generic(),
                methodHandle,
                methodHandle.type()
        );

        @SuppressWarnings("unchecked")
        Supplier<R> supplier = (Supplier<R>) callSite.getTarget().invokeExact();
        return supplier;
    }

    public static <BEAN, R> Function<BEAN, R> toSupplier(Class<BEAN> refc, Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "apply",
                MethodType.methodType(Function.class),
                methodHandle.type().generic(),
                methodHandle,
                MethodType.methodType(method.getReturnType(), refc, method.getParameterTypes())
        );

        @SuppressWarnings("unchecked")
        Function<BEAN, R> function = (Function<BEAN, R>) callSite.getTarget().invokeExact();
        return function;
    }

    // endregion supplier

    // region Consumer

    @FunctionalInterface
    public interface Consumer3<A, B, C> {
        void accept(A a, B b, C c);

        default Consumer3<A, B, C> andThen(
                Consumer3<? super A, ? super B, ? super C> after) {

            Objects.requireNonNull(after);
            return (a, b, c) -> {
                accept(a, b, c);
                after.accept(a, b, c);
            };
        }
    }

    @FunctionalInterface
    public interface Consumer4<A, B, C, D> {
        void accept(A a, B b, C c, D d);

        default Consumer4<A, B, C, D> andThen(
                Consumer4<? super A, ? super B, ? super C, ? super D> after) {

            Objects.requireNonNull(after);
            return (a, b, c, d) -> {
                accept(a, b, c, d);
                after.accept(a, b, c, d);
            };
        }
    }

    @FunctionalInterface
    public interface Consumer5<A, B, C, D, E> {
        void accept(A a, B b, C c, D d, E e);

        default Consumer5<A, B, C, D, E> andThen(
                Consumer5<? super A, ? super B, ? super C, ? super D, ? super E> after) {

            Objects.requireNonNull(after);
            return (a, b, c, d, e) -> {
                accept(a, b, c, d, e);
                after.accept(a, b, c, d, e);
            };
        }
    }

    @FunctionalInterface
    public interface Consumer6<A, B, C, D, E, F> {
        void accept(A a, B b, C c, D d, E e, F f);

        default Consumer6<A, B, C, D, E, F> andThen(
                Consumer6<? super A, ? super B, ? super C, ? super D, ? super E, ? super F> after) {

            Objects.requireNonNull(after);
            return (a, b, c, d, e, f) -> {
                accept(a, b, c, d, e, f);
                after.accept(a, b, c, d, e, f);
            };
        }
    }

    @FunctionalInterface
    public interface Consumer7<A, B, C, D, E, F, G> {
        void accept(A a, B b, C c, D d, E e, F f, G g);

        default Consumer7<A, B, C, D, E, F, G> andThen(
                Consumer7<? super A, ? super B, ? super C, ? super D, ? super E, ? super F, ? super G> after) {

            Objects.requireNonNull(after);
            return (a, b, c, d, e, f, g) -> {
                accept(a, b, c, d, e, f, g);
                after.accept(a, b, c, d, e, f, g);
            };
        }
    }

    @FunctionalInterface
    public interface Consumer8<A, B, C, D, E, F, G, H> {
        void accept(A a, B b, C c, D d, E e, F f, G g, H h);

        default Consumer8<A, B, C, D, E, F, G, H> andThen(
                Consumer8<? super A, ? super B, ? super C, ? super D, ? super E, ? super F, ? super G, ? super H> after) {

            Objects.requireNonNull(after);
            return (a, b, c, d, e, f, g, h) -> {
                accept(a, b, c, d, e, f, g, h);
                after.accept(a, b, c, d, e, f, g, h);
            };
        }
    }

    @FunctionalInterface
    public interface Consumer9<A, B, C, D, E, F, G, H, I> {
        void accept(A a, B b, C c, D d, E e, F f, G g, H h, I i);

        default Consumer9<A, B, C, D, E, F, G, H, I> andThen(
                Consumer9<? super A, ? super B, ? super C, ? super D, ? super E, ? super F, ? super G, ? super H, ? super I> after) {

            Objects.requireNonNull(after);
            return (a, b, c, d, e, f, g, h, i) -> {
                accept(a, b, c, d, e, f, g, h, i);
                after.accept(a, b, c, d, e, f, g, h, i);
            };
        }
    }

    public static <T> Consumer<T> toConsumer(Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "accept",
                MethodType.methodType(Consumer.class),
                MethodType.methodType(void.class, Object.class),
                methodHandle,
                methodHandle.type()
        );

        @SuppressWarnings("unchecked")
        Consumer<T> consumer = (Consumer<T>) callSite.getTarget().invokeExact();
        return consumer;
    }

    public static <BEAN, T> BiConsumer<BEAN, T> toConsumer(Class<BEAN> refc, Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "accept",
                MethodType.methodType(BiConsumer.class),
                MethodType.methodType(void.class, Object.class, Object.class),
                methodHandle,
                MethodType.methodType(method.getReturnType(), refc, method.getParameterTypes())
        );

        @SuppressWarnings("unchecked")
        BiConsumer<BEAN, T> consumer
                = (BiConsumer<BEAN, T>) callSite.getTarget().invokeExact();
        return consumer;
    }

    public static <T, U> BiConsumer<T, U> toConsumer2(Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "accept",
                MethodType.methodType(BiConsumer.class),
                MethodType.methodType(void.class, Object.class, Object.class),
                methodHandle,
                methodHandle.type()
        );

        @SuppressWarnings("unchecked")
        BiConsumer<T, U> consumer = (BiConsumer<T, U>) callSite.getTarget().invokeExact();
        return consumer;
    }

    public static <BEAN, T, U> Consumer3<BEAN, T, U> toConsumer2(Class<BEAN> refc, Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "accept",
                MethodType.methodType(Consumer3.class),
                MethodType.methodType(void.class, Object.class, Object.class, Object.class),
                methodHandle,
                MethodType.methodType(method.getReturnType(), refc, method.getParameterTypes())
        );

        @SuppressWarnings("unchecked")
        Consumer3<BEAN, T, U> consumer
                = (Consumer3<BEAN, T, U>) callSite.getTarget().invokeExact();
        return consumer;
    }

    public static <A, B, C> Consumer3<A, B, C> toConsumer3(Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "accept",
                MethodType.methodType(Consumer3.class),
                MethodType.methodType(void.class, Object.class, Object.class, Object.class),
                methodHandle,
                methodHandle.type()
        );

        @SuppressWarnings("unchecked")
        Consumer3<A, B, C> consumer
                = (Consumer3<A, B, C>) callSite.getTarget().invokeExact();
        return consumer;
    }

    public static <BEAN, A, B, C> Consumer4<BEAN, A, B, C> toConsumer3(Class<BEAN> refc, Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "accept",
                MethodType.methodType(Consumer4.class),
                MethodType.methodType(void.class, Object.class,
                        Object.class, Object.class, Object.class),
                methodHandle,
                MethodType.methodType(method.getReturnType(), refc, method.getParameterTypes())
        );

        @SuppressWarnings("unchecked")
        Consumer4<BEAN, A, B, C> consumer
                = (Consumer4<BEAN, A, B, C>) callSite.getTarget().invokeExact();
        return consumer;
    }

    public static <A, B, C, D> Consumer4<A, B, C, D> toConsumer4(Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "accept",
                MethodType.methodType(Consumer4.class),
                MethodType.methodType(void.class,
                        Object.class, Object.class, Object.class, Object.class),
                methodHandle,
                methodHandle.type()
        );

        @SuppressWarnings("unchecked")
        Consumer4<A, B, C, D> consumer
                = (Consumer4<A, B, C, D>) callSite.getTarget().invokeExact();
        return consumer;
    }

    public static <BEAN, A, B, C, D> Consumer5<BEAN, A, B, C, D> toConsumer4(Class<BEAN> refc, Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "accept",
                MethodType.methodType(Consumer5.class),
                MethodType.methodType(void.class, Object.class,
                        Object.class, Object.class, Object.class, Object.class),
                methodHandle,
                MethodType.methodType(method.getReturnType(), refc, method.getParameterTypes())
        );

        @SuppressWarnings("unchecked")
        Consumer5<BEAN, A, B, C, D> consumer
                = (Consumer5<BEAN, A, B, C, D>) callSite.getTarget().invokeExact();
        return consumer;
    }

    public static <A, B, C, D, E> Consumer5<A, B, C, D, E> toConsumer5(Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "accept",
                MethodType.methodType(Consumer5.class),
                MethodType.methodType(void.class,
                        Object.class, Object.class, Object.class, Object.class, Object.class),
                methodHandle,
                methodHandle.type()
        );

        @SuppressWarnings("unchecked")
        Consumer5<A, B, C, D, E> consumer
                = (Consumer5<A, B, C, D, E>) callSite.getTarget().invokeExact();
        return consumer;
    }

    public static <BEAN, A, B, C, D, E> Consumer6<BEAN, A, B, C, D, E> toConsumer5(Class<BEAN> refc, Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "accept",
                MethodType.methodType(Consumer6.class),
                MethodType.methodType(void.class, Object.class,
                        Object.class, Object.class, Object.class, Object.class, Object.class),
                methodHandle,
                MethodType.methodType(method.getReturnType(), refc, method.getParameterTypes())
        );

        @SuppressWarnings("unchecked")
        Consumer6<BEAN, A, B, C, D, E> consumer
                = (Consumer6<BEAN, A, B, C, D, E>) callSite.getTarget().invokeExact();
        return consumer;
    }

    public static <A, B, C, D, E, F> Consumer6<A, B, C, D, E, F> toConsumer6(Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "accept",
                MethodType.methodType(Consumer6.class),
                MethodType.methodType(void.class,
                        Object.class, Object.class, Object.class,
                        Object.class, Object.class, Object.class),
                methodHandle,
                methodHandle.type()
        );

        @SuppressWarnings("unchecked")
        Consumer6<A, B, C, D, E, F> consumer
                = (Consumer6<A, B, C, D, E, F>) callSite.getTarget().invokeExact();
        return consumer;
    }

    public static <BEAN, A, B, C, D, E, F> Consumer7<BEAN, A, B, C, D, E, F> toConsumer6(Class<BEAN> refc, Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "accept",
                MethodType.methodType(Consumer7.class),
                MethodType.methodType(void.class, Object.class,
                        Object.class, Object.class, Object.class,
                        Object.class, Object.class, Object.class),
                methodHandle,
                MethodType.methodType(method.getReturnType(), refc, method.getParameterTypes())
        );

        @SuppressWarnings("unchecked")
        Consumer7<BEAN, A, B, C, D, E, F> consumer
                = (Consumer7<BEAN, A, B, C, D, E, F>) callSite.getTarget().invokeExact();
        return consumer;
    }

    public static <A, B, C, D, E, F, G> Consumer7<A, B, C, D, E, F, G> toConsumer7(Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "accept",
                MethodType.methodType(Consumer7.class),
                MethodType.methodType(void.class,
                        Object.class, Object.class, Object.class,
                        Object.class, Object.class, Object.class, Object.class),
                methodHandle,
                methodHandle.type()
        );

        @SuppressWarnings("unchecked")
        Consumer7<A, B, C, D, E, F, G> consumer
                = (Consumer7<A, B, C, D, E, F, G>) callSite.getTarget().invokeExact();
        return consumer;
    }

    public static <BEAN, A, B, C, D, E, F, G> Consumer8<BEAN, A, B, C, D, E, F, G> toConsumer7(Class<BEAN> refc, Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "accept",
                MethodType.methodType(Consumer8.class),
                MethodType.methodType(void.class, Object.class,
                        Object.class, Object.class, Object.class,
                        Object.class, Object.class, Object.class, Object.class),
                methodHandle,
                MethodType.methodType(method.getReturnType(), refc, method.getParameterTypes())
        );

        @SuppressWarnings("unchecked")
        Consumer8<BEAN, A, B, C, D, E, F, G> consumer
                = (Consumer8<BEAN, A, B, C, D, E, F, G>) callSite.getTarget().invokeExact();
        return consumer;
    }

    public static <A, B, C, D, E, F, G, H> Consumer8<A, B, C, D, E, F, G, H> toConsumer8(Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "accept",
                MethodType.methodType(Consumer8.class),
                MethodType.methodType(void.class,
                        Object.class, Object.class, Object.class, Object.class,
                        Object.class, Object.class, Object.class, Object.class),
                methodHandle,
                methodHandle.type()
        );

        @SuppressWarnings("unchecked")
        Consumer8<A, B, C, D, E, F, G, H> consumer
                = (Consumer8<A, B, C, D, E, F, G, H>) callSite.getTarget().invokeExact();
        return consumer;
    }

    public static <BEAN, A, B, C, D, E, F, G, H> Consumer9<BEAN, A, B, C, D, E, F, G, H> toConsumer8(Class<BEAN> refc, Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "accept",
                MethodType.methodType(Consumer9.class),
                MethodType.methodType(void.class, Object.class,
                        Object.class, Object.class, Object.class, Object.class,
                        Object.class, Object.class, Object.class, Object.class),
                methodHandle,
                MethodType.methodType(method.getReturnType(), refc, method.getParameterTypes())
        );

        @SuppressWarnings("unchecked")
        Consumer9<BEAN, A, B, C, D, E, F, G, H> consumer
                = (Consumer9<BEAN, A, B, C, D, E, F, G, H>) callSite.getTarget().invokeExact();
        return consumer;
    }

    // endregion Consumer

    // region Function

    @FunctionalInterface
    public interface Function3<A, B, C, R> {
        R apply(A a, B b, C c);

        default <V> Function3<A, B, C, V> andThen(Function<? super R, ? extends V> after) {
            Objects.requireNonNull(after);
            return (A a, B b, C c) -> after.apply(apply(a, b, c));
        }
    }

    @FunctionalInterface
    public interface Function4<A, B, C, D, R> {
        R apply(A a, B b, C c, D d);

        default <V> Function4<A, B, C, D, V> andThen(Function<? super R, ? extends V> after) {
            Objects.requireNonNull(after);
            return (A a, B b, C c, D d) -> after.apply(apply(a, b, c, d));
        }
    }

    @FunctionalInterface
    public interface Function5<A, B, C, D, E, R> {
        R apply(A a, B b, C c, D d, E e);

        default <V> Function5<A, B, C, D, E, V> andThen(Function<? super R, ? extends V> after) {
            Objects.requireNonNull(after);
            return (A a, B b, C c, D d, E e) -> after.apply(apply(a, b, c, d, e));
        }
    }

    @FunctionalInterface
    public interface Function6<A, B, C, D, E, F, R> {
        R apply(A a, B b, C c, D d, E e, F f);

        default <V> Function6<A, B, C, D, E, F, V> andThen(Function<? super R, ? extends V> after) {
            Objects.requireNonNull(after);
            return (A a, B b, C c, D d, E e, F f) -> after.apply(apply(a, b, c, d, e, f));
        }
    }

    @FunctionalInterface
    public interface Function7<A, B, C, D, E, F, G, R> {
        R apply(A a, B b, C c, D d, E e, F f, G g);

        default <V> Function7<A, B, C, D, E, F, G, V> andThen(Function<? super R, ? extends V> after) {
            Objects.requireNonNull(after);
            return (A a, B b, C c, D d, E e, F f, G g) -> after.apply(apply(a, b, c, d, e, f, g));
        }
    }

    @FunctionalInterface
    public interface Function8<A, B, C, D, E, F, G, H, R> {
        R apply(A a, B b, C c, D d, E e, F f, G g, H h);

        default <V> Function8<A, B, C, D, E, F, G, H, V> andThen(Function<? super R, ? extends V> after) {
            Objects.requireNonNull(after);
            return (A a, B b, C c, D d, E e, F f, G g, H h) -> after.apply(apply(a, b, c, d, e, f, g, h));
        }
    }

    @FunctionalInterface
    public interface Function9<A, B, C, D, E, F, G, H, I, R> {
        R apply(A a, B b, C c, D d, E e, F f, G g, H h, I i);

        default <V> Function9<A, B, C, D, E, F, G, H, I, V> andThen(Function<? super R, ? extends V> after) {
            Objects.requireNonNull(after);
            return (A a, B b, C c, D d, E e, F f, G g, H h, I i) -> after.apply(apply(a, b, c, d, e, f, g, h, i));
        }
    }

    public static <T, R> Function<T, R> toFunction(Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "apply",
                MethodType.methodType(Function.class),
                methodHandle.type().generic(),
                methodHandle,
                methodHandle.type()
        );

        @SuppressWarnings("unchecked")
        Function<T, R> function = (Function<T, R>) callSite.getTarget().invokeExact();
        return function;
    }

    public static <BEAN, T, R> BiFunction<BEAN, T, R> toFunction(Class<BEAN> refc, Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "apply",
                MethodType.methodType(BiFunction.class),
                methodHandle.type().generic(),
                methodHandle,
                MethodType.methodType(method.getReturnType(), refc, method.getParameterTypes())
        );

        @SuppressWarnings("unchecked")
        BiFunction<BEAN, T, R> function = (BiFunction<BEAN, T, R>) callSite.getTarget().invokeExact();
        return function;
    }

    public static <T, U, R> BiFunction<T, U, R> toFunction2(Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "apply",
                MethodType.methodType(BiFunction.class),
                methodHandle.type().generic(),
                methodHandle,
                methodHandle.type()
        );

        @SuppressWarnings("unchecked")
        BiFunction<T, U, R> function = (BiFunction<T, U, R>) callSite.getTarget().invokeExact();
        return function;
    }

    public static <BEAN, T, U, R> Function3<BEAN, T, U, R> toFunction2(Class<BEAN> refc, Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "apply",
                MethodType.methodType(Function3.class),
                methodHandle.type().generic(),
                methodHandle,
                MethodType.methodType(method.getReturnType(), refc, method.getParameterTypes())
        );

        @SuppressWarnings("unchecked")
        Function3<BEAN, T, U, R> function
                = (Function3<BEAN, T, U, R>) callSite.getTarget().invokeExact();
        return function;
    }

    public static <A, B, C, R> Function3<A, B, C, R> toFunction3(Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "apply",
                MethodType.methodType(Function3.class),
                methodHandle.type().generic(),
                methodHandle,
                methodHandle.type()
        );

        @SuppressWarnings("unchecked")
        Function3<A, B, C, R> function
                = (Function3<A, B, C, R>) callSite.getTarget().invokeExact();
        return function;
    }

    public static <BEAN, A, B, C, R> Function4<BEAN, A, B, C, R> toFunction3(Class<BEAN> refc, Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "apply",
                MethodType.methodType(Function4.class),
                methodHandle.type().generic(),
                methodHandle,
                MethodType.methodType(method.getReturnType(), refc, method.getParameterTypes())
        );

        @SuppressWarnings("unchecked")
        Function4<BEAN, A, B, C, R> function
                = (Function4<BEAN, A, B, C, R>) callSite.getTarget().invokeExact();
        return function;
    }

    public static <A, B, C, D, R> Function4<A, B, C, D, R> toFunction4(Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "apply",
                MethodType.methodType(Function4.class),
                methodHandle.type().generic(),
                methodHandle,
                methodHandle.type()
        );

        @SuppressWarnings("unchecked")
        Function4<A, B, C, D, R> function
                = (Function4<A, B, C, D, R>) callSite.getTarget().invokeExact();
        return function;
    }

    public static <BEAN, A, B, C, D, R> Function5<BEAN, A, B, C, D, R> toFunction4(Class<BEAN> refc, Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "apply",
                MethodType.methodType(Function5.class),
                methodHandle.type().generic(),
                methodHandle,
                MethodType.methodType(method.getReturnType(), refc, method.getParameterTypes())
        );

        @SuppressWarnings("unchecked")
        Function5<BEAN, A, B, C, D, R> function
                = (Function5<BEAN, A, B, C, D, R>) callSite.getTarget().invokeExact();
        return function;
    }

    public static <A, B, C, D, E, R> Function5<A, B, C, D, E, R> toFunction5(Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "apply",
                MethodType.methodType(Function5.class),
                methodHandle.type().generic(),
                methodHandle,
                methodHandle.type()
        );

        @SuppressWarnings("unchecked")
        Function5<A, B, C, D, E, R> function
                = (Function5<A, B, C, D, E, R>) callSite.getTarget().invokeExact();
        return function;
    }

    public static <BEAN, A, B, C, D, E, R> Function6<BEAN, A, B, C, D, E, R> toFunction5(Class<BEAN> refc, Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "apply",
                MethodType.methodType(Function6.class),
                methodHandle.type().generic(),
                methodHandle,
                MethodType.methodType(method.getReturnType(), refc, method.getParameterTypes())
        );

        @SuppressWarnings("unchecked")
        Function6<BEAN, A, B, C, D, E, R> function
                = (Function6<BEAN, A, B, C, D, E, R>) callSite.getTarget().invokeExact();
        return function;
    }

    public static <A, B, C, D, E, F, R> Function6<A, B, C, D, E, F, R> toFunction6(Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "apply",
                MethodType.methodType(Function6.class),
                methodHandle.type().generic(),
                methodHandle,
                methodHandle.type()
        );

        @SuppressWarnings("unchecked")
        Function6<A, B, C, D, E, F, R> function
                = (Function6<A, B, C, D, E, F, R>) callSite.getTarget().invokeExact();
        return function;
    }

    public static <BEAN, A, B, C, D, E, F, R> Function7<BEAN, A, B, C, D, E, F, R> toFunction6(Class<BEAN> refc, Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "apply",
                MethodType.methodType(Function7.class),
                methodHandle.type().generic(),
                methodHandle,
                MethodType.methodType(method.getReturnType(), refc, method.getParameterTypes())
        );

        @SuppressWarnings("unchecked")
        Function7<BEAN, A, B, C, D, E, F, R> function
                = (Function7<BEAN, A, B, C, D, E, F, R>) callSite.getTarget().invokeExact();
        return function;
    }

    public static <A, B, C, D, E, F, G, R> Function7<A, B, C, D, E, F, G, R> toFunction7(Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "apply",
                MethodType.methodType(Function7.class),
                methodHandle.type().generic(),
                methodHandle,
                methodHandle.type()
        );

        @SuppressWarnings("unchecked")
        Function7<A, B, C, D, E, F, G, R> function
                = (Function7<A, B, C, D, E, F, G, R>) callSite.getTarget().invokeExact();
        return function;
    }

    public static <BEAN, A, B, C, D, E, F, G, R> Function8<BEAN, A, B, C, D, E, F, G, R> toFunction7(Class<BEAN> refc, Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "apply",
                MethodType.methodType(Function8.class),
                methodHandle.type().generic(),
                methodHandle,
                MethodType.methodType(method.getReturnType(), refc, method.getParameterTypes())
        );

        @SuppressWarnings("unchecked")
        Function8<BEAN, A, B, C, D, E, F, G, R> function
                = (Function8<BEAN, A, B, C, D, E, F, G, R>) callSite.getTarget().invokeExact();
        return function;
    }

    public static <A, B, C, D, E, F, G, H, R> Function8<A, B, C, D, E, F, G, H, R> toFunction8(Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "apply",
                MethodType.methodType(Function8.class),
                methodHandle.type().generic(),
                methodHandle,
                methodHandle.type()
        );

        @SuppressWarnings("unchecked")
        Function8<A, B, C, D, E, F, G, H, R> function
                = (Function8<A, B, C, D, E, F, G, H, R>) callSite.getTarget().invokeExact();
        return function;
    }

    public static <BEAN, A, B, C, D, E, F, G, H, R> Function9<BEAN, A, B, C, D, E, F, G, H, R> toFunction8(Class<BEAN> refc, Method method) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle methodHandle = lookup.unreflect(method);
        CallSite callSite = LambdaMetafactory.metafactory(
                lookup,
                "apply",
                MethodType.methodType(Function9.class),
                methodHandle.type().generic(),
                methodHandle,
                MethodType.methodType(method.getReturnType(), refc, method.getParameterTypes())
        );

        @SuppressWarnings("unchecked")
        Function9<BEAN, A, B, C, D, E, F, G, H, R> function
                = (Function9<BEAN, A, B, C, D, E, F, G, H, R>) callSite.getTarget().invokeExact();
        return function;
    }

    // endregion Function
}
调用
public class Test {
    public static String print() { return "test"; }
    public void print1(String a) { System.out.println(a); }
    public String print2(String a, String b) { return a + "-" + b; }
    
    public static void main(String[] args) throws Throwable {
        Test a = new Test();
        
		Method m0 = Test.class.getDeclaredMethod("print");
		Supplier<String> supplier = LambdaMetaFactoryKit.toSupplier(m0);
		System.out.println(supplier.get());
		
		Method m1 = Test.class.getDeclaredMethod("print1", String.class);
		BiConsumer<Test, String> consumer = LambdaMetaFactoryKit.toConsumer(Test.class, m1);
		consumer.accept(a, "1");
		
		Method m2 = Test.class.getDeclaredMethod("print2", String.class, String.class);
		Function3<Test, String, String, String> function = LambdaMetaFactoryKit.toFunction2(Test.class, m2);
		System.out.println(function.apply(a, "1", "2"));
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值