Dynamically Applied Annotations
In the section Swaps, you were introduced to annotations that can be applied to bean classes, methods, fields, and constructors such as @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 getAddresses() {}
Any Juneau annotation that has an on()/onClass()
method can be applied dynamically this way.
These include:
The valid pattern matches are:
- Classes:
- Fully qualified:
com.foo.MyClass
- Fully qualified inner class:
com.foo.MyClass$Inner1$Inner2
- Simple:
MyClass
- Simple inner:
MyClass$Inner1$Inner2
,Inner1$Inner2
,Inner2
- Fully qualified:
- Methods:
- Fully qualified with args:
com.foo.MyClass.myMethod(String,int)
,com.foo.MyClass.myMethod(java.lang.String,int)
,com.foo.MyClass.myMethod()
- Fully qualified:
com.foo.MyClass.myMethod
- Simple with args:
MyClass.myMethod(String,int)
,MyClass.myMethod(java.lang.String,int)
,MyClass.myMethod()
- Simple:
MyClass.myMethod
- Simple inner class:
MyClass$Inner1$Inner2.myMethod
,Inner1$Inner2.myMethod
,Inner2.myMethod
- Fully qualified with args:
- Fields:
- Fully qualified:
com.foo.MyClass.myField
- Simple:
MyClass.myField
- Simple inner class:
MyClass$Inner1$Inner2.myField
,Inner1$Inner2.myField
,Inner2.myField
- Fully qualified:
- Constructors:
- Fully qualified with args:
com.foo.MyClass(String,int)
,com.foo.MyClass(java.lang.String,int)
,com.foo.MyClass()
- Simple with args:
MyClass(String,int)
,MyClass(java.lang.String,int)
,MyClass()
- Simple inner class:
MyClass$Inner1$Inner2()
,Inner1$Inner2()
,Inner2()
- Fully qualified with args:
- A comma-delimited list of anything on this list.