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

nl.vpro.util.LeaveDefaultsProxyHandler Maven / Gradle / Ivy

There is a newer version: 5.3.1
Show newest version
package nl.vpro.util;

import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Support for java 8 'default' methods. Resteasy won't honour them. Wrapping around this proxy reinstalls them.
 *
 * @author Michiel Meeuwissen
 * @since 1.8
 */
public class LeaveDefaultsProxyHandler implements InvocationHandler {

    private final Object delegate;

    public LeaveDefaultsProxyHandler(Object delegate) {
        this.delegate = delegate;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (method.isDefault()) {
            final Class declaringClass = method.getDeclaringClass();
            return MethodHandles.lookup().in(declaringClass)
                .unreflectSpecial(method, declaringClass)
                .bindTo(proxy)
                .invokeWithArguments(args);

        } else {
            return method.invoke(delegate, args);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy