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

io.smallrye.faulttolerance.DefaultMethodFallbackProvider Maven / Gradle / Ivy

There is a newer version: 6.6.2
Show newest version
package io.smallrye.faulttolerance;

import java.lang.invoke.MethodHandles.Lookup;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * Workaround for default fallback methods (used e.g. in MP Rest Client).
 * 
 * @author Martin Kouba
 */
class DefaultMethodFallbackProvider {

    static Object getFallback(Method fallbackMethod, ExecutionContextWithInvocationContext ctx)
            throws Throwable {
        // This should work in Java 8
        Class declaringClazz = fallbackMethod.getDeclaringClass();
        Constructor constructor = Lookup.class.getDeclaredConstructor(Class.class);
        constructor.setAccessible(true);
        return constructor.newInstance(declaringClazz)
                .in(declaringClazz)
                .unreflectSpecial(fallbackMethod, declaringClazz)
                .bindTo(ctx.getTarget())
                .invokeWithArguments(ctx.getParameters());
    }

    private DefaultMethodFallbackProvider() {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy