Annotation Type GeneratePojoBuilder


  • @Retention(CLASS)
    @Target({TYPE,METHOD,CONSTRUCTOR,ANNOTATION_TYPE})
    public @interface GeneratePojoBuilder
    Use this annotation to trigger the code generation of a fluent pojo builder.
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      java.lang.String[] excludeProperties
      Specifies which of the pojo's properties will be excluded from the generated builder.
      java.lang.String[] includeProperties
      Specifies which of the pojo's properties will be included into the generated builder.
      java.lang.String intoPackage
      Specifies the package of the generated builder.
      java.lang.Class<?> withBaseclass
      Specifies the base class of the generated builder.
      java.lang.Class<?> withBuilderInterface
      Specifies the generic builder interface of the generated builder.
      boolean withBuilderProperties
      Specifies whether the generated builder should define builder-based setter-methods using the builder interface.
      Visibility withConstructor
      Specifies the visibility of the builder's constructor.
      boolean withCopyMethod
      Specifies whether a copy method should be generated.
      java.lang.String withFactoryMethod
      Specifies the name of a static factory method added to the builder.
      boolean withGenerationGap
      Specifies whether the generation gap pattern is used.
      java.lang.String withName
      Specifies the name of the generated builder.
      java.lang.Class<?> withOptionalProperties
      Specifies whether the generated builder should define optional-based setter-methods using the specified 'Optional' type.
      java.lang.String withSetterNamePattern
      Specifies the name pattern of the generated setter-methods.
      java.lang.Class<?> withValidator
      Specifies the validator class that should be used to validate the created pojo.
    • Field Detail

      • DEFAULT_NAME

        static final java.lang.String DEFAULT_NAME
      • DEFAULT_PACKAGE

        static final java.lang.String DEFAULT_PACKAGE
      • DEFAULT_SETTER_NAME

        static final java.lang.String DEFAULT_SETTER_NAME
      • DEFAULT_FACTORY_METHOD

        static final java.lang.String DEFAULT_FACTORY_METHOD
      • DEFAULT_INCLUSION_PATTERN

        static final java.lang.String DEFAULT_INCLUSION_PATTERN
    • Element Detail

      • withBaseclass

        java.lang.Class<?> withBaseclass
        Specifies the base class of the generated builder.
        Returns:
        the base class of the generated builder
        Default:
        java.lang.Object.class
      • withBuilderInterface

        java.lang.Class<?> withBuilderInterface
        Specifies the generic builder interface of the generated builder. This interface must declare exactly one type parameter and a build() (or alternatively a get()) method having this type as return type.

        For example:

         
         public interface Builder<T> {
           T build();
         }
          
        Returns:
        the generic interface of the generated builder or Void, if no interface is specified
        Default:
        java.lang.Void.class
      • withBuilderProperties

        boolean withBuilderProperties
        Specifies whether the generated builder should define builder-based setter-methods using the builder interface.

        When set to true, the withBuilderInterface() must specify a valid interface.

        Returns:
        whether the generated builder should define builder-based setter-methods
        Default:
        false
      • withOptionalProperties

        java.lang.Class<?> withOptionalProperties
        Specifies whether the generated builder should define optional-based setter-methods using the specified 'Optional' type.

        The 'Optional' type can have any name but must be interface-compatible with the following interface:

         
         public interface Optional<T> {
           T get();
           boolean isPresent();
         }
          
        where T is the generic type parameter matching the property's type.

        Examples are Google Guava's com.google.common.base.Optional and java.util.Optional introduced with Java 8.

        Returns:
        the 'Optional' type used for generating the optional-based setter-methods, or Void if no optional-based setter-methods should be generated
        Default:
        java.lang.Void.class
      • withName

        java.lang.String withName
        Specifies the name of the generated builder. An asterisk will be replaced with the pojos simple name. Default is "*Builder".
        Returns:
        the name of the generated builder
        Default:
        "*Builder"
      • withSetterNamePattern

        java.lang.String withSetterNamePattern
        Specifies the name pattern of the generated setter-methods. An asterisk will be replaced with the property's original name. Default is "with*".
        Returns:
        the name pattern of the generated setter-methods.
        Default:
        "with*"
      • intoPackage

        java.lang.String intoPackage
        Specifies the package of the generated builder. An asterisk will be replaced with the pojos package. Default is "*".
        Returns:
        the package of the generated builder
        Default:
        "*"
      • withGenerationGap

        boolean withGenerationGap
        Specifies whether the generation gap pattern is used. If enabled this will generate two classes (instead of one), of which one contains the generated code. The other class is for handwritten code. To prevent it from being overwritten please move it out of the generated-sources folder. Default is "false".
        Returns:
        true if the generation gap should be used
        Default:
        false
      • withCopyMethod

        boolean withCopyMethod
        Specifies whether a copy method should be generated. The copy method will take an instance of the built class and will copy all its fields into the builder. This allows it to easily change one or more fields of immutable objects.
        Returns:
        true if a copy method should be generated
        Default:
        false
      • withFactoryMethod

        java.lang.String withFactoryMethod
        Specifies the name of a static factory method added to the builder. An asterisk will be replaced by the pojos simple name. Default is "" meaning not to generate this method.
         
         public class MyPojoBuilder {
           public static MyPojoBuilder myPojo() {
             return new MyPojoBuilder();
           }
         }
          
        Returns:
        the factory-method name
        Default:
        ""
      • withConstructor

        Visibility withConstructor
        Specifies the visibility of the builder's constructor.
        Returns:
        the constructor's visibility
        Default:
        net.karneim.pojobuilder.Visibility.PUBLIC
      • withValidator

        java.lang.Class<?> withValidator
        Specifies the validator class that should be used to validate the created pojo. The class must define a validate method having one parameter that is compatible with the pojo's type. If the validation fails, the method must throw some runtime exception (or one of its subclasses).

        This is an example of how a validator could look like:

         
         public class MyPojoValidator {
           public void validate(Pojo pojo) {
             if ( - check if pojo is invalid -) {
               throw new RuntimeException("This pojo is invalid!");
             }
           }
         }
          
        Returns:
        the validator's class, or Void, if no validator should be used.
        Default:
        java.lang.Void.class
      • includeProperties

        java.lang.String[] includeProperties
        Specifies which of the pojo's properties will be included into the generated builder. All properties that match any property pattern in the specified array will be included. All other non-mandatory properties will be excluded. Mandatory properties are those which are passed as constructor or factory method arguments. They will never be excluded, neither explicitly nor implicitly.

        The property pattern consists of a name pattern followed by an optional type pattern.

        The syntax is <name pattern>[:<type pattern>]. The pattern supports the asterisk '*' wildcard character that matches any character.

        Default is '*'.

        Returns:
        the inclusion patterns
        Default:
        {"*"}
      • excludeProperties

        java.lang.String[] excludeProperties
        Specifies which of the pojo's properties will be excluded from the generated builder. All property that match any property pattern in the specified array will be excluded, except those that are mandatory. Mandatory properties are those which are passed as constructor or factory method arguments. They will never be excluded, neither explicitly nor implicitly.

        The property pattern consists of a name pattern followed by an optional type pattern.

        The syntax is <name pattern>[:<type pattern>]. The pattern supports the asterisk '*' wildcard character that matches any character.

        Default is the empty array.

        Returns:
        the exclusion patterns
        Default:
        {}