Annotation Type ImmutablePreBuild


  • @Retention(SOURCE)
    @Target(METHOD)
    public @interface ImmutablePreBuild
    Annotation defining a method that is to be called just before a code generated immutable bean is built.

    This is used when an immutable bean wants to perform an action on the builder just before it is built into a bean. The action might include validation (normally done using ImmutableValidator) and defaulting (normally done using ImmutableDefaults).

    The special use case for this annotation is the ability to default the value of one property from the value of another. For example, consider a bean with two dates, where one is derived from the other (such as the second being the first adjusted to a valid business day). Use of this annotation allows the second date to be set to the same as the first date if a value is not set.

    The method must be a private static void instance method that takes a single argument of the type 'Builder'. The method will be called at the start of the build() method of the builder. For example:

       @ImmutablePreBuild
       private static void preBuild(Builder builder) {
         if (builder.date2 == null) {
           builder.date2 = builder.date1;  // default date2 to be same as date1
         }
       }