Annotation Type Beanc


Maps constructor arguments to property names on beans with read-only properties.

Can be used in the following locations:

  • Bean constructors.
  • @Rest-annotated classes and @RestOp-annotated methods when an on() value is specified.

This annotation can be used in the case of beans with properties whose values can only be set by passing them in through a constructor on the class.
Since method parameter names are lost during compilation, this annotation essentially redefines them so that they are available at runtime.

This annotation can only be applied to constructors and can only be applied to one constructor per class.

When present, bean instantiation is delayed until the call to BeanMap.getBean(). Until then, bean property values are stored in a local cache until getBean() is called. Because of this additional caching step, parsing into read-only beans tends to be slower and use more memory than parsing into beans with writable properties.

Attempting to call BeanMap.put(String,Object) on a read-only property after calling BeanMap.getBean() will result in a BeanRuntimeException being thrown. Multiple calls to BeanMap.getBean() will return the same bean instance.

See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Dynamically apply this annotation to the specified constructors.
    The names of the properties of the constructor arguments.
  • Element Details

    • on

      Dynamically apply this annotation to the specified constructors.

      Used in conjunction with BeanContext.Builder.applyAnnotations(Class...) to dynamically apply an annotation to an existing constructor. It is ignored when the annotation is applied directly to constructors.

      The following example shows this annotation in use:

      // Our read-only bean. public class Person { private final String name; private final int 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; } } @BeanConfig @Beanc(on="Person(String,int)", properties="name,age")) public static class MyConfig {}

      // Parsing into a read-only bean. String json = "{name:'John Smith',age:45}"; Person person = JsonParser.DEFAULT.copy().applyAnnotations(MyConfig.class).build().parse(json); String name = person.getName(); // "John Smith" int age = person.getAge(); // 45

      Valid patterns:
      • 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()"
      • A comma-delimited list of anything on this list.
      See Also:
      Returns:
      The annotation value.
      Default:
      {}
    • properties

      The names of the properties of the constructor arguments.

      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 = JsonParser.DEFAULT.parse(json); String name = person.getName(); // "John Smith" int age = person.getAge(); // 45

      Note that the @Name annotation can also be used to identify bean property names on constructor arguments. If neither this annotation or @Name is used, then we try to get the property names from the parameter names if they are available in the bytecode.

      Returns:
      The annotation value.
      Default:
      ""