Interface AutoValueExtension.BuilderContext

  • Enclosing class:
    AutoValueExtension

    public static interface AutoValueExtension.BuilderContext
    Represents a Builder associated with an @AutoValue class.
    • Method Detail

      • builderType

        TypeElement builderType()
        Returns the @AutoValue.Builder interface or abstract class that this object represents.
      • toBuilderMethods

        Set<ExecutableElement> toBuilderMethods()
        Returns abstract no-argument methods in the @AutoValue class that return the builder type.

        Consider a class like this:

           @AutoValue abstract class Foo {
             abstract String bar();
        
             abstract Builder toBuilder();
        
             ...
             @AutoValue.Builder
             abstract static class Builder {...}
           }
         

        Here toBuilderMethods() will return a set containing the method Foo.toBuilder().

      • builderMethods

        Set<ExecutableElement> builderMethods()
        Returns static no-argument methods in the @AutoValue class that return the builder type.

        Consider a class like this:

           @AutoValue abstract class Foo {
             abstract String bar();
        
             static Builder builder() {
               return new AutoValue_Foo.Builder()
                   .setBar("default bar");
             }
        
             @AutoValue.Builder
             abstract class Builder {
               abstract Builder setBar(String x);
               abstract Foo build();
             }
           }
         

        Here builderMethods() will return a set containing the method Foo.builder(). Generated code should usually call this method in preference to constructing AutoValue_Foo.Builder() directly, because this method can establish default values for properties, as it does here.

      • buildMethod

        Optional<ExecutableElement> buildMethod()
        Returns the method build() in the builder class, if it exists and returns the @AutoValue type. This is the method that generated code for @AutoValue class Foo should call in order to get an instance of Foo from its builder. The returned method is called build(); if the builder uses some other name then extensions have no good way to guess how they should build.

        A common convention is for build() to be a concrete method in the @AutoValue.Builder class, which calls an abstract method autoBuild() that is implemented in the generated subclass. The build() method can then do validation, defaulting, and so on.

      • autoBuildMethod

        ExecutableElement autoBuildMethod()
        Returns the abstract build method. If the @AutoValue class is Foo, this is an abstract no-argument method in the builder class that returns Foo. This might be called build(), or, following a common convention, it might be called autoBuild() and used in the implementation of a build() method that is defined in the builder class.

        Extensions should call the build() method in preference to this one. But they should override this one if they want to customize build-time behaviour.

      • setters

        Map<String,​Set<ExecutableElement>> setters()
        Returns a map from property names to the corresponding setters. A property may have more than one setter. For example, an ImmutableList<String> might be set by setFoo(ImmutableList<String>) and setFoo(String[]).
      • propertyBuilders

        Map<String,​ExecutableElement> propertyBuilders()
        Returns a map from property names to property builders. For example, if there is a property foo defined by abstract ImmutableList<String> foo(); or abstract ImmutableList<String> getFoo(); in the @AutoValue class, then there can potentially be a builder defined by abstract ImmutableList.Builder<String> fooBuilder(); in the @AutoValue.Builder class. This map would then map "foo" to the ExecutableElement representing fooBuilder().