Class BeanProxyInvocationHandler<T>

java.lang.Object
org.apache.juneau.BeanProxyInvocationHandler<T>
Type Parameters:
T - The interface class type
All Implemented Interfaces:
InvocationHandler

public class BeanProxyInvocationHandler<T> extends Object implements InvocationHandler
Provides an InvocationHandler for creating dynamic proxy instances of bean interfaces.

This class enables the creation of bean instances from interfaces without requiring concrete implementations. When the useInterfaceProxies setting is enabled in BeanContext, this handler is used to create proxy instances that implement bean interfaces.

The handler stores bean property values in an internal map and intercepts method calls to:

  • Getter methods - Returns values from the internal property map
  • Setter methods - Stores values in the internal property map
  • equals(Object) - Compares property maps, with special handling for other proxy instances
  • hashCode() - Returns hash code based on the property map
  • toString() - Serializes the property map to JSON

When comparing two proxy instances using equals(), if both are created with BeanProxyInvocationHandler, the comparison is optimized by directly comparing their internal property maps rather than converting to BeanMap.

Example:

// Define an interface public interface Person { String getName(); void setName(String name); int getAge(); void setAge(int age); } // Create a proxy instance var bc = BeanContext.create().useInterfaceProxies().build(); var person = bc.getClassMeta(Person.class).newInstance(); // Use it like a regular bean person.setName("John"); person.setAge(25); var name = person.getName(); // Returns "John"

See Also:
  • Constructor Details

  • Method Details

    • invoke

      public Object invoke(Object proxy, Method method, Object[] args)
      Handles method invocations on the proxy instance.

      This method intercepts all method calls on the proxy and routes them appropriately:

      • If the method is equals(Object), compares property maps
      • If the method is hashCode(), returns the hash code of the property map
      • If the method is toString(), serializes the property map to JSON
      • If the method is a getter (identified via BeanMeta.getGetterProps()), returns the property value
      • If the method is a setter (identified via BeanMeta.getSetterProps()), stores the property value
      • Otherwise, throws UnsupportedOperationException

      The equals() method has special optimization: when comparing two proxy instances created with this handler, it directly compares their internal property maps using Proxy.getInvocationHandler(Object) to access the other instance's handler.

      Specified by:
      invoke in interface InvocationHandler
      Parameters:
      proxy - The proxy instance on which the method was invoked
      method - The method that was invoked
      args - The arguments passed to the method, or null if no arguments
      Returns:
      The return value of the method invocation
      Throws:
      UnsupportedOperationException - If the method is not a supported bean method (getter, setter, equals, hashCode, or toString)