Annotation Type Immutable

  • @Target(TYPE)
    public @interface Immutable
    By annotating a class with Immutable an immutable class is generated. The following modifications/additions are done:
    • The annotated class becomes final
    • A private constructor is generated
    • The equals, hashCode and toString methods are generated
    • Instances have to be built by calling the build or the copy method. Using copy provides the possibility to copy an existing instance.
    The annotation handles a couple of types in a special manner:
    • Optionality: By default all attributes are mandatory. If a mandatory field is not initialized the call of build or copy throws an IllegalStateException. By using Optional an attribute can be defines as optional. It is not necessary to initialize an optional attribute.
    • Collections: Currently List and Set are handled. The annotation makes sure that only immutableList and Set instances are returned. It delegates this task to ImmutableList / ImmutableSet
    As immutable classes are final, inheritance is not supported. Therefore you have to use composition. By annotating an attribute with Immutable.Include all the private fields of the included type are copied into the immutable class.

    There are a couple of additional annotations which influence the annotation processor, please consult the corresponding documentation: Immutable.Include, Immutable.ExcludeFromEqualsHashCode, Immutable.ExcludeFromToString, Immutable.Modifiable.

    Oliver Libutzki <[email protected]>
    See Also:
    Immutable.Include, Immutable.ExcludeFromEqualsHashCode, Immutable.ExcludeFromToString, Immutable.Modifiable
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      java.lang.Class<?>[] interfaces
      Based on the getters of the specified interfaces fields are generated and the interfaces are implemented.
    • Element Detail

      • interfaces

        java.lang.Class<?>[] interfaces
        Based on the getters of the specified interfaces fields are generated and the interfaces are implemented. It is possible to annotate the getters of the interfaces with Immutable.ExcludeFromEqualsHashCode, Immutable.ExcludeFromToString or Immutable.Modifiable which has the same effect as annotating a field of the immutable class.
        interfaces to be implemented