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

name.remal.proxy.MethodHandler Maven / Gradle / Ivy

package name.remal.proxy;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.lang.reflect.Method;

import static name.remal.SneakyThrow.sneakyThrow;

public interface MethodHandler {

    boolean canHandle(@Nonnull Method method);

    @Nullable
    Object handle(@Nonnull Object proxy, @Nonnull Method method, @Nullable Object[] args) throws Throwable;

    @Nonnull
    static MethodHandler of(@Nonnull CanHandle canHandle, @Nonnull Handler handler) {
        return new MethodHandler() {
            @Override
            public boolean canHandle(@Nonnull Method method) {
                try {
                    return canHandle.canHandle(method);
                } catch (Throwable throwable) {
                    throw sneakyThrow(throwable);
                }
            }

            @Nullable
            @Override
            public Object handle(@Nonnull Object proxy, @Nonnull Method method, @Nullable Object[] args) throws Throwable {
                return handler.handle(proxy, method, args);
            }
        };
    }

    @FunctionalInterface
    interface CanHandle {
        boolean canHandle(@Nonnull Method method) throws Throwable;
    }

    @FunctionalInterface
    interface Handler {

        @Nullable
        Object handle(@Nonnull Object proxy, @Nonnull Method method, @Nullable Object[] args) throws Throwable;

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy