@Beanc Annotation
The @Beanc annotation is used to map constructor arguments to property names on bean with read-only properties.
Since method parameter names are lost during compilation, this annotation essentially redefines them so that they are available at runtime.
The definition of a read-only bean is a bean with properties with only getters, like shown below:
// Our read-only bean.
public class Person {
private final String name;
private final int age;
@Beanc(properties="name,age"})
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Read only properties.
// Getters but no setters.
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
// Parsing into a read-only bean.
String json = "{name:'John Smith',age:45}";
Person person = Json.to(json, Person.class);
String name = person.getName(); // "John Smith"
int age = person.getAge(); // 45
Beans can also be defined with a combination of read-only and read-write properties.
The @Name annotation can also be used instead of
@Beanc(properties)
:
@Beanc
public Person(@Name("name") String name, @Name("age") int age) {
this.name = name;
this.age = age;
}
If neither @Beanc(properties)
or @Name is used to identify the
bean property names, we will try to use the parameter names if they are available in the bytecode.