- Type Parameters:
T
- the proxy type
public interface ProxyBuilder<T>
Builds a dynamic proxy for a single interface.
Note that if the
Object.equals(Object)
method is not proxied the resulting proxy is equal only to itself.
List<String> list = new ArrayList<>();
List<String> listProxy = ProxyBuilder.builder(List.class)
.delegate(list)
.method("add", Object.class, parameters -> {
Object item = parameters.arguments().get(0);
System.out.println("Adding: " + item);
return parameters.delegate().add(item);
})
.method("remove", Object.class, parameters -> {
Object item = parameters.arguments().get(0);
System.out.println("Removing: " + item);
return parameters.delegate().remove(item);
})
.method("equals", Object.class, parameters -> {
Object object = parameters.arguments().get(0);
System.out.println("Equals: " + object);
return parameters.delegate().equals(object);
})
.method("size", parameters -> {
System.out.println("Size");
return parameters.delegate().size();
})
.build();
- See Also:
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
A proxy method. -
Method Summary
Modifier and TypeMethodDescriptionbuild()
Builds the Proxy instance.static <T> ProxyBuilder<T>
Returns a newProxyBuilder
instance.Sets the delegate instance to forward non-proxied method calls to.method
(String methodName, ProxyBuilder.ProxyMethod<T> proxyMethod) Proxy the given no-argument method with the given proxy method.method
(String methodName, Class<?> parameterType, ProxyBuilder.ProxyMethod<T> proxyMethod) Proxy the given single-argument method with the given proxy method.Proxy the given method with the given proxy method.
-
Method Details
-
delegate
Sets the delegate instance to forward non-proxied method calls to. If not specified, all non-proxied methods throwUnsupportedOperationException
.- Parameters:
delegate
- the delegate instance to receive all non-proxied method calls- Returns:
- this proxy builder
-
method
Proxy the given no-argument method with the given proxy method.- Parameters:
methodName
- the method nameproxyMethod
- the proxy replacement method- Returns:
- this proxy builder
-
method
ProxyBuilder<T> method(String methodName, Class<?> parameterType, ProxyBuilder.ProxyMethod<T> proxyMethod) Proxy the given single-argument method with the given proxy method.- Parameters:
methodName
- the method nameparameterType
- the method parameter typeproxyMethod
- the proxy method- Returns:
- this proxy builder
-
method
ProxyBuilder<T> method(String methodName, List<Class<?>> parameterTypes, ProxyBuilder.ProxyMethod<T> proxyMethod) Proxy the given method with the given proxy method.- Parameters:
methodName
- the method nameparameterTypes
- the method parameter typesproxyMethod
- the proxy method- Returns:
- this proxy builder
-
build
T build()Builds the Proxy instance.- Returns:
- a new proxy instance
-
builder
Returns a newProxyBuilder
instance.- Type Parameters:
T
- the proxy type- Parameters:
interfaceToProxy
- the interface to proxy- Returns:
- a new
ProxyBuilder
instance. - Throws:
IllegalArgumentException
- in caseinterfaceToProxy
is not an interface
-