All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opencredo.concursus.mapping.reflection.MethodInvoking Maven / Gradle / Ivy

The newest version!
package com.opencredo.concursus.mapping.reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.function.BiFunction;
import java.util.function.Function;

@FunctionalInterface
public interface MethodInvoking extends Function {

    @SuppressWarnings("unchecked")
    static  BiFunction invokingInstance(Method method) {
        return invokingInstance((Class) method.getReturnType(), method);
    }

    static  BiFunction invokingInstance(Class returnType, Method method) {
        return (target, args) -> invokingInstance(returnType, target, method).apply(args);
    }

    @SuppressWarnings("unchecked")
    static  Function invokingInstance(Method method, Object target) {
        return invokingInstance((Class) method.getReturnType(), target, method);
    }

    static  Function invokingInstance(Class returnType, Object target, Method method) {
        return of(args -> returnType.cast(method.invoke(target, args)));
    }

    @SuppressWarnings("unchecked")
    static  Function invokingStatic(Method method) {
        return invokingStatic((Class) method.getReturnType(), method);
    }

    static  Function invokingStatic(Class returnType, Method method) {
        return of(args -> returnType.cast(method.invoke(null, args)));
    }

    static  Function of(MethodInvoking invocation) {
        return invocation;
    }

    O invoke(I input) throws IllegalAccessException, InvocationTargetException;

    default O apply(I input) {
        try {
            return invoke(input);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e.getCause());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy