Annotation Interface BeanCtor


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

The bean-modeling sibling to @MarshalledProp/@BeanProp: where those describe individual bean properties, this annotation describes how a constructor's parameters map to the bean's logical property set so the framework can instantiate read-only beans during parsing and deserialize property values through the constructor instead of via setters.

Lives in juneau-commons so the bean-modeling layer can describe constructor argument mappings without depending on the marshalling layer.

Can be used in the following locations:

  • Bean constructors.

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.

Java Records:

For Java records, the canonical constructor and its property mappings are automatically detected, so this annotation is not required. It can still be used to specify a non-canonical constructor if needed, for example to provide default values for certain components.

See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Optional description for the exposed API.
    The names of the properties of the constructor arguments.
  • Element Details

    • description

      Optional description for the exposed API.
      Returns:
      The annotation value.
      Since:
      9.2.0
      Default:
      {}
    • properties

      The names of the properties of the constructor arguments.

      The @BeanCtor annotation is used to map constructor arguments to property names on beans 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; @BeanCtor(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; } }

      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:
      ""