
com.rbkmoney.woody.api.proxy.ProxyFactory Maven / Gradle / Ivy
package com.rbkmoney.woody.api.proxy;
import com.rbkmoney.woody.api.proxy.tracer.MethodCallTracer;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
public class ProxyFactory {
private final Object object = new Object();
private final MethodCallerFactory callerFactory;
private final MethodCallTracer callTracer;
private final boolean allowObjectOverriding;
public ProxyFactory(MethodCallTracer callTracer, boolean allowObjectOverriding) {
this(new ReflectionMethodCallerFactory(), callTracer, allowObjectOverriding);
}
public ProxyFactory(MethodCallerFactory callerFactory, MethodCallTracer callTracer, boolean allowObjectOverriding) {
this.callerFactory = callerFactory;
this.callTracer = callTracer;
this.allowObjectOverriding = allowObjectOverriding;
}
public T getInstance(Class iface, InvocationTargetProvider targetProvider) {
return getInstance(iface, targetProvider, callerFactory, callTracer, allowObjectOverriding);
}
@SuppressWarnings("unchecked")
public T getInstance(Class iface, InvocationTargetProvider targetProvider, MethodCallerFactory callerFactory, MethodCallTracer callTracer, boolean allowObjectOverriding) {
if (!allowObjectOverriding) {
Method[] overriden = MethodShadow.getShadowedMethods(object, iface);
if (overriden.length != 0) {
throw new IllegalArgumentException("Target interface " + iface.getName() + "shadows Object methods:" + Arrays.toString(overriden));
}
}
return makeProxy(iface, targetProvider, callerFactory, callTracer);
}
protected T makeProxy(Class iface, InvocationTargetProvider targetProvider, MethodCallerFactory callerFactory, MethodCallTracer callTracer) {
return (T) Proxy.newProxyInstance(
iface.getClassLoader(),
new Class[]{iface},
new ProxyInvocationHandler(iface, targetProvider, callerFactory, MethodCallInterceptors.trackedCallInterceptor(callTracer)));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy