{title:'Dynamically Applied Annotations', created:'8.1.3', updated:'9.0.0'}

In the section Swaps, you were introduced to annotations that can be applied to bean classes, methods, fields, and constructors such as {@link oaj.annotation.Bean @Bean}:

| // Address class with only street/city/state properties (in that order). | // All other properties are ignored. | @Bean(properties="street,city,state") | public class Address { ... }

An alternate way of applying these annotations is to attach them to unrelated classes and methods and then tell your serializer or parser where to find them.

| // Unannotated class. | public class Address { ... } | | @Bean(onClass=Address.class, properties="street,city,state") | public static class DummyClass {} | | WriterSerializer serializer = JsonSerializer | .create() | .applyAnnotations(DummyClass.class) | .build(); | | String json = serializer.toString(addressBean);

The advantage to this approach is it allows you to use Juneau annotations on classes/methods/fields/constructors where you might not have access to the source code, or when you only want to selectively apply the annotation under certain scenarios instead of globally.

For example, the following shows the @Bean annotation being selectively applied on a single REST method (described later in juneau-rest-server):

| @RestGet | @Bean(onClass=Address.class, properties="street,city,state") | public List<Address> getAddresses() {}

Any Juneau annotation that has an on()/onClass() method can be applied dynamically this way. These include:

The valid pattern matches are: