Skip to content

Property Pattern Examples

Michael Karneim edited this page Mar 12, 2015 · 16 revisions

The property pattern is part of the feature described in "Support explicit inclusion/exlusion of properties" #96. It is used to select the pojo's properties that should be included or excluded from the generated builder.

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

Syntax

The syntax of the property pattern is <name pattern>[:<type pattern>]

where <name pattern> and <type pattern> are used to match the property's name and type. The type pattern is optional.

The pattern syntax is some poor-man's regex allowing an asterisk * to match any character sequence (including the empty sequence).

For example: *name would match surname, lastname, and firstname, but not email. And url:java.lang.String would match String url but not URL url.

Here are some more examples that show how to use the property pattern.

Enumerating Property Names

Enumerate the property names to define which properties to include explicitly.

@GeneratePojoBuilder(includeProperties = {"firstname","surname"})
public class Contact {
  public String firstname;
  public String surname;
  public String email;
}

This generates a builder having only firstname and surname as properties, but not email.

Using Wildcards

@GeneratePojoBuilder(includeProperties = "*name")
public class Contact {
  public String firstname;
  public String surname;
  public String email;
}

Again this generates a builder having only firstname and surname as properties.

Explicit Exclusion

The same result from the example above could be achived by excluding the unwanted properties explicitly.

@GeneratePojoBuilder(excludeProperties = "email")
public class Contact {
  public String firstname;
  public String surname;
  public String email;
}

This generates a builder having only firstname and surname as properties, but not email.

Combining Inclusion and Exclusion

Of course you could also combine inclusion and exclusion.

@GeneratePojoBuilder(includeProperties = "*name", 
  excludeProperties = "middlename")
public class Contact {
  public String firstname;
  public String middlename;
  public String surname;
  public String email;
}

This generates a builder having only firstname and surname as properties, but not middlename and email.

Exclusion by Type

Use the optional type pattern to match properties by type:

@GeneratePojoBuilder(excludeProperties = "*:java.lang.String[]")
public class Contact {
  public String firstname;
  public String surname;
  public String email;
  public String[] notes;
  public String[] skills;
}

This generates a builder having only firstname, surname, and email as properties, but not skills and notes.

Inclusion by Type

The type pattern also works with inclusion, of course.

@GeneratePojoBuilder(includeProperties = "*:java.lang.String")
public class Contact {
  public String firstname;
  public String surname;
  public String email;
  public String[] notes;
  public String[] skills;
}

This generates a builder having only firstname, surname, and email as properties, but not skills and notes.

Common Pittfalls

Mandatory Properties

You might wonder why your builder has some properties even if you've excluded them all (using excludeProperties = "*" for example).

This can happen if your pojo has some mandatory properties. These are properties that are used as parameters in the pojo's constructor (or the factory method). In this case PojoBuilder will not exclude them from the generated builder.

public class Contact {
  public String firstname;
  public String surname;
  public String email;

  @GeneratePojoBuilder(excludeProperties = "*")
  public Contact(String surname) {
    this.surname = surname;
  }
}

This generates a builder having only surname, but not firstname and email.

Omitting Packages

When you use the type pattern, please make sure to fully qualify all types, or use an asterisk to shorten the package name. Leaving out the packages will otherwise not lead to the expected result.

For example, this will not exclude anything (unless there is a "String" class in the default package):

@GeneratePojoBuilder(excludeProperties = "*:String")
public class Contact {
  public String firstname;
  public String surname;
  public String email;
}

Instead use this:

@GeneratePojoBuilder(excludeProperties = "*:java.lang.String")
public class Contact {
  public String firstname;
  public String surname;
  public String email;
}