Class BeanInterceptor<T>

java.lang.Object
org.apache.juneau.swap.BeanInterceptor<T>
Type Parameters:
T - The bean type.
Direct Known Subclasses:
BeanInterceptor.Void

public class BeanInterceptor<T> extends Object
Bean interceptor.

Bean interceptors intercept calls to bean getters and setters to allow them to override values in transit.

Example:

// Interceptor that strips out sensitive information on Address beans. public class AddressInterceptor extends BeanInterceptor<Address> { @Override public Object readProperty(Address bean, String name, Object value) { if ("taxInfo".equals(name)) return "redacted"; return value; } @Override public Object writeProperty(Address bean, String name, Object value) { if ("taxInfo".equals(name) && "redacted".equals(value)) return TaxInfoUtils.lookup(bean.getStreet(), bean.getCity(), bean.getState()); return value; } }

Bean interceptors are registered in the following way:

Example:

// Register interceptor on bean class. @Bean(interceptor=AddressInterceptor.class) public class Address { public String getTaxInfo() {...} public void setTaxInfo(String value) {...} }

See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • readProperty

      public Object readProperty(T bean, String name, Object value)
      Property read interceptor.

      Subclasses can override this property to convert property values to some other object just before serialization.

      Example:

      // Address filter that strips out sensitive information. public class AddressInterceptor extends BeanInterceptor<Address> { public Object readProperty(Address bean, String name, Object value) { if ("taxInfo".equals(name)) return "redacted"; return value; } }

      Parameters:
      bean - The bean from which the property was read.
      name - The property name.
      value - The value just extracted from calling the bean getter.
      Returns:
      The value to serialize. Default is just to return the existing value.
    • writeProperty

      public Object writeProperty(T bean, String name, Object value)
      Property write interceptor.

      Subclasses can override this property to convert property values to some other object just before calling the bean setter.

      Example:

      // Address filter that strips out sensitive information. public class AddressInterceptor extends BeanInterceptor<Address> { public Object writeProperty(Address bean, String name, Object value) { if ("taxInfo".equals(name) && "redacted".equals(value)) return TaxInfoUtils.lookup(bean.getStreet(), bean.getCity(), bean.getState()); return value; } }

      Parameters:
      bean - The bean from which the property was read.
      name - The property name.
      value - The value just parsed.
      Returns:
      The value to serialize. Default is just to return the existing value.