Interface ProxyBuilder<T>

Type Parameters:
T - the proxy type

public interface ProxyBuilder<T>
Builds a dynamic proxy for a single interface.

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:
  • Method Details

    • delegate

      ProxyBuilder<T> delegate(T delegate)
      Sets the delegate instance to forward non-proxied method calls to. If not specified, all non-proxied methods throw UnsupportedOperationException.

      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

      ProxyBuilder<T> method(String name, ProxyBuilder.ProxyMethod<T> 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 name
      method - the proxy replacement method
      Returns:
      this proxy builder
    • method

      ProxyBuilder<T> method(String name, Class<?> parameterType, ProxyBuilder.ProxyMethod<T> 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 name
      parameterType - the method parameter type
      method - 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 name
      parameterTypes - the method parameter types
      method - 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

      static <T> ProxyBuilder<T> of(Class<T> interfaceToProxy)
      Returns a new ProxyBuilder 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 case interfaceToProxy is not an interface