- Type Parameters:
T
- the proxy type
This builder is reusable and thread-safe. Each call to build()
creates a new proxy instance
based on the current builder state. Subsequent changes to the builder do not affect previously built proxies.
Methods can be replaced by calling method(String, ProxyMethod)
again with the same method signature,
but once added, methods cannot be removed from the builder.
Note that if the Object.equals(Object)
method is not proxied the resulting proxy is equal only to itself.
List<String> list = new ArrayList<>();
ProxyBuilder<List> builder = ProxyBuilder.of(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("size", parameters -> {
System.out.println("Size");
return parameters.delegate().size();
});
List<String> proxy1 = builder.build();
// Builder can be reused and modified
builder.method("remove", Object.class, parameters -> {
Object item = parameters.arguments().get(0);
System.out.println("Removing: " + item);
return parameters.delegate().remove(item);
});
List<String> proxy2 = builder.build(); // Has all three methods
// proxy1 still has only add() and size() methods proxied
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
A proxy method. -
Method Summary
Modifier and TypeMethodDescriptionbuild()
Builds the Proxy instance.Sets the delegate instance to forward non-proxied method calls to.method
(String name, ProxyBuilder.ProxyMethod<T> method) Proxy the given no-argument method with the given proxy method.method
(String name, Class<?> parameterType, ProxyBuilder.ProxyMethod<T> method) Proxy the given single-argument method with the given proxy method.Proxy the given method with the given proxy method.static <T> ProxyBuilder
<T> Returns a newProxyBuilder
instance.
-
Method Details
-
delegate
Sets the delegate instance to forward non-proxied method calls to. If not specified, all non-proxied methods throwUnsupportedOperationException
.The delegate is captured at build time. Changing the delegate after building a proxy does not affect previously built proxies.
- 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.If a proxy method has already been defined for this method signature, it will be replaced with the new one.
- Parameters:
name
- the method namemethod
- the proxy replacement method- Returns:
- this proxy builder
-
method
Proxy the given single-argument method with the given proxy method.If a proxy method has already been defined for this method signature, it will be replaced with the new one.
- Parameters:
name
- the method nameparameterType
- the method parameter typemethod
- the proxy method- Returns:
- this proxy builder
-
method
ProxyBuilder<T> method(String name, List<Class<?>> parameterTypes, ProxyBuilder.ProxyMethod<T> method) Proxy the given method with the given proxy method.If a proxy method has already been defined for this method signature, it will be replaced with the new one.
- Parameters:
name
- the method nameparameterTypes
- the method parameter typesmethod
- the proxy method- Returns:
- this proxy builder
-
build
T build()Builds the Proxy instance.Each call to this method returns a new proxy instance with the current builder configuration. The returned proxy is independent of the builder and will not be affected by subsequent changes to the builder.
- Returns:
- a new proxy instance
-
of
Returns a newProxyBuilder
instance.Note: Unlike other builders in the framework, ProxyBuilder serves as both the builder interface and the factory, as it builds dynamic proxy instances rather than framework objects with separate builder interfaces.
- 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
-