Interface ProxyBuilder<T>

Type Parameters:
T - the proxy type

public interface ProxyBuilder<T>
Builds a simple 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:
  • 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.
      Parameters:
      delegate - the delegate instance to receive all non-proxied method calls
      Returns:
      this proxy builder
    • method

      ProxyBuilder<T> method(String methodName, ProxyBuilder.ProxyMethod<T> proxyMethod)
      Proxy the given no-argument method with the given proxy method.
      Parameters:
      methodName - the method name
      proxyMethod - 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 name
      parameterType - the method parameter type
      proxyMethod - 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 name
      parameterTypes - the method parameter types
      proxyMethod - the proxy method
      Returns:
      this proxy builder
    • build

      T build()
      Builds the Proxy instance.
      Returns:
      a new proxy instance
    • builder

      static <T> ProxyBuilder<T> builder(Class<T> interfaceToProxy)
      Returns a new ProxyBuilder instance.
      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