All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.karneim.pojobuilder.GeneratePojoBuilder Maven / Gradle / Ivy

Go to download

The PojoBuilder Generator is a Java 6 compliant annotation processor that generates a fluent builder class for POJOs (Plain Old Java Object)

There is a newer version: 4.3.0
Show newest version
package net.karneim.pojobuilder;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Use this annotation to trigger the code generation of a fluent pojo builder.
 */
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.ANNOTATION_TYPE})
public @interface GeneratePojoBuilder {
  public final String DEFAULT_NAME = "*Builder";
  public final String DEFAULT_PACKAGE = "*";
  public final String DEFAULT_SETTER_NAME = "with*";
  public final String DEFAULT_FACTORY_METHOD = "";
  public final String DEFAULT_INCLUSION_PATTERN = "*";

  /**
   * Specifies the base class of the generated builder.
   * 
   * @return the base class of the generated builder
   */
  Class withBaseclass() default Object.class;

  /**
   * Specifies the generic builder interface of the generated builder. This interface must declare exactly one type
   * parameter and a build() method having this type as return type.
   * 

* For example: * *

   * 
   * public interface Builder{@literal <}T{@literal >} {
   *   T build();
   * }
   * 
   * 
* * @return the generic interface of the generated builder or {@link Void}, if no interface is specified */ Class withBuilderInterface() default Void.class; /** * Specifies whether the generated builder should define builder-based setter-methods using the builder interface. *

* When set to true, the {@link GeneratePojoBuilder#withBuilderInterface()} must specify a valid * interface. * * @return whether the generated builder should define builder-based setter-methods */ boolean withBuilderProperties() default false; /** * 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{@literal <}T{@literal >} {
   *   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. * * @return the 'Optional' type used for generating the optional-based setter-methods, or {@link Void} if no * optional-based setter-methods should be generated */ Class withOptionalProperties() default Void.class; /** * Specifies the name of the generated builder. An asterisk will be replaced with the pojos simple name. Default is * "*Builder". * * @return the name of the generated builder */ String withName() default DEFAULT_NAME; /** * Specifies the name pattern of the generated setter-methods. An asterisk will be replaced with the property's * original name. Default is "with*". * * @return the name pattern of the generated setter-methods. */ String withSetterNamePattern() default DEFAULT_SETTER_NAME; /** * Specifies the package of the generated builder. An asterisk will be replaced with the pojos package. Default is * "*". * * @return the package of the generated builder */ String intoPackage() default DEFAULT_PACKAGE; /** * 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". * * @return true if the generation gap should be used */ boolean withGenerationGap() default false; /** * 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. * * @return true if a copy method should be generated */ boolean withCopyMethod() default false; /** * 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();
   *   }
   * }
   * 
   * 
* * @return the factory-method name */ String withFactoryMethod() default DEFAULT_FACTORY_METHOD; /** * 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!");
   *     }
   *   }
   * }
   * 
   * 
* * @return the validator's class, or {@link Void}, if no validator should be used. */ Class withValidator() default Void.class; /** * 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 '*'. * * @return the inclusion patterns */ String[] includeProperties() default {DEFAULT_INCLUSION_PATTERN}; /** * 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. * * @return the exclusion patterns */ String[] excludeProperties() default {}; }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy