GitXplorerGitXplorer
g

CallBuilder

public
106 stars
24 forks
6 issues

Commits

List of commits on branch master.
Unverified
f4c29f6f47ee131ef1723cbccd537273203d0f97

Add genrule to create deploy jar for processor

mmatvore committed 10 years ago
Unverified
01fe9a67af2c91be69db0d7f6d12939d0143862f

Remove Guava-based sample code in README.md

mmatvore committed 10 years ago
Unverified
75f2a093cd72b5033b7343918670633f9357a4c0

Remove NullableSetting.java sample field style

mmatvore committed 10 years ago
Unverified
fa0b41d7a5f0df2e3cf3aa86a6b61f21b5625c57

Check-in source of all third-party dependencies

mmatvore committed 10 years ago
Unverified
f3c9b1eed1a71ac3c74f46e26811defaf41c3ca3

Drop dependency on Guava in remaining source files

mmatvore committed 10 years ago
Unverified
76269c34e2a09bb90e18deb6c483815f8ca8c8ac

Drop dep on Guava in test cases

mmatvore committed 10 years ago

README

The README file for this repository.

CallBuilder

Make a builder by defining one function.
(beta: be ready for breaking API changes)

CallBuilder is a Java code generator that finally makes it easy to make a builder class. Builders are great when you have a constructor or method with many parameters. They are even helpful when you have two or more arguments of the same type, since the order is easy to mix up.

Builders are usually hard to write. You will probably need around 4 lines for every field, and if it's an inner class, it makes the file long and cumbersome to navigate. This discourages many people from writing builders, and those people give up and learn to live with brittle, and hard-to-read code. If you want to add multiple setters for a field (common for lists that may have add and addAll), your builder will quickly become a chore to write and a burden to maintain.

CallBuilder changes that. To use it, you can simply write the method or constructor as you normally would, but add the @CallBuilder annotation:

public class Person {
  @CallBuilder
  Person(
      String familyName,
      String givenName,
      List<String> addressLines,
      @Nullable Integer age) {
    // ...
  }
}

Now you will have access to a PersonBuilder class in the same package!

Person friend = new PersonBuilder()
    .setAddressLines(Arrays.asList("1123 Easy Street", "Townplace, XZ"))
    .setAge(22)
    .setGivenName("John")
    .setFamilyName("Doe")
    .build();

With the field styles feature, you can define custom behavior for the fields that are of a certain type. This can make the API more natural and look more like a manually-written builder:

public class Person {
  @CallBuilder
  Person(
      String familyName,
      String givenName,
      @BuilderField(style = ArrayListAdding.class) ArrayList<String> addressLines,
      @Nullable Integer age) {
    // ...
  }
}

This can be used like:

Person friend = new PersonBuilder()
    .addToAddressLines("1123 Easy Street")
    .addToAddressLines("Townplace, XZ")
    .setAge(22)
    .setGivenName("John")
    .setFamilyName("Doe")
    .build();