Git Product home page Git Product logo

callbuilder's Introduction

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();

callbuilder's People

Contributors

matvore avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

callbuilder's Issues

Support for default values

Default values can be potentially supported by automatically calling a function before building:

@CallBuilder
Person person(String name, int age) {...}

void person_preBuild(PersonBuilder builder) {
  if (builder.getAge() == 0) builder.setAge(30);
}

Where {methodName}_preBuild is an optional method that is called by the build() method automatically if supplied.

Create genrule target to make release jars

There should probably be two release jars:

  • plugin deploy jar, which has a file at META-INF/services/javax.annotation.processing.Processor with the class name of the processor implementation
  • annotations-only jar, which lets you use the @CallBuilder and @BuilderField annotations in your app.

The former can be added to the processor_path while the latter can be added to the compile-time and run-time classpath, OR the former can be added to all three if you are not concerned about the size of your release jar.

It doesn't have to be a genrule necessarily - just some automated, reproducible method for creating the jars.

Allow specifying "sub-package" within which the generated builder class will reside

Since all the builder classes that CallBuilder generates are currently dropped into the same namespace, there's a chance that class names will conflict.
To work around that, one can use a longer class name (perhaps prefixed with the name of the method that will be called). This could result, though, in very long class names.

Can you please enhance CallBuilder so that we can specify a sub-package in which the generated builder class will reside?

e.g.

package foo.bar;
class Baz {
    CallBuilder(className="builder.GenerateQuxBuilder")
    public static Qux generateQux();
}

will create foo.bar.builder.GenerateQuxBuilder.
This can then be imported as GenerateQuxBuilder with no risk of conflicts.

Alternatively, you can add an attribute to the annotation to specify the package name separately.

Annotate generated classes with @Generated

Generated classes should be annotated with javax.annotation.Generated.
Similar to how AutoValue annotates its generated classes:
@ Generated("com.google.auto.value.processor.AutoValueProcessor")

How to build and use the proejct

Hey, I find this project occasionally, it's really a good idea.
But the wiki link seems broken, I've no idea how to use it.
Can anyone help to give a user guide doc?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.