Interface Filters

Occasionally, you may want to limit bean properties to only those defined on a parent class or interface. This is accomplished through interface filters.

Interface filters are defined through the following:

- {@link oaj.BeanContext#BEAN_beanFilters}
- {@link oaj.annotation.Bean#interfaceClass() Bean(interfaceClass)}
- {@link oaj.transform.BeanFilterBuilder#interfaceClass(Class)}

For example, let's define the following interface and implementation:

// Interface public class MyInterface { public String getFoo(); } // Implementation public class MyInterfaceImpl implements MyInterface { public String getFoo() {...} public String getBar() {...} }

Suppose we only want to render the properties defined on our interface, not the implementation. To do so, we can define the following bean filter:

// Define transform that limits properties to only those defined on MyClass public class MyInterfaceFilter extends BeanFilter<MyInterface> { public MyInterfaceFilter() { interfaceClass(MyInterface.class); } }

When serialized, the serialized bean will only include properties defined on the interface.

WriterSerializer serializer = JsonSerializer .create() .simple() .beanFilters(MyInterfaceFilter.class) .build(); MyInterface object = new MyInterfaceImpl(); // Prints "{foo:'foo'}" // bar is ignored because it's not on the interface String json = serializer.serialize(object);

The {@link oaj.BeanContextBuilder#beanFilters(Object...)} method will automatically interpret any non-BeanFilter classes passed in as meaning interface classes. So in the previous example, the BeanFilter class could have been avoided altogether by just passing in MyInterface.class to the serializer, like so:

WriterSerializer serializer = JsonSerializer .create() .beanFilters(MyInterface.class) Shortcut! .build();

The annotation equivalent is {@link oaj.annotation.Bean#interfaceClass() Bean#interfaceClass()}.

@Bean(interfaceClass=MyInterface.class) public class MyInterfaceImpl implements MyInterface { public String getFoo() {...} public String getBar() {...} }

The annotation can be used in a couple of ways.

Using the annotation on an interface will be inherited by all children.

@Bean(interfaceClass=MyInterface.class) public class MyInterface { public String getFoo(); }

The annotation can be used on parent classes as well. Child beans will only serialize properties defined on the parent class.

@Bean(interfaceClass=MyAbstractClass.class) public abstract class MyAbstractClass { public String getFoo() {...}; }