Class BuilderRequiredProperties


  • public abstract class BuilderRequiredProperties
    extends Object
    Code generation to track which properties have been set in a builder.

    Every property in an @AutoValue or @AutoBuilder builder must be set before the build() method is called, with a few exceptions like @Nullable and Optional properties. That means we must keep track of which ones have in fact been set. We do that in two ways: for reference (non-primitive) types, we use null to indicate that the value has not been set, while for primitive types we use a bitmask where each bit indicates whether a certain primitive property has been set.

    Additionally, for Kotlin constructors with default parameters, we track exactly which properties have been set so we can invoke the constructor thas has a bitmask indicating the properties to be defaulted.

    The public methods in this class are accessed reflectively from the builder.vm template. In that template, $builderRequiredProperties references an instance of this class corresponding to the builder being generated. A reference like $builderRequiredProperties.markAsSet($p) calls the method markAsSet(com.google.auto.value.processor.AutoValueishProcessor.Property) with the given parameter. A reference like $builderRequiredProperties.requiredProperties is shorthand for $builderRequiredProperties.getProperties().

    • Method Detail

      • getRequiredProperties

        public com.google.common.collect.ImmutableSet<com.google.auto.value.processor.AutoValueishProcessor.Property> getRequiredProperties()
      • getFieldDeclarations

        public com.google.common.collect.ImmutableList<String> getFieldDeclarations()
        Returns code to declare any fields needed to track which properties have been set. Each line in the returned list should appear on a line of its own.
      • getInitToAllSet

        public com.google.common.collect.ImmutableList<String> getInitToAllSet()
        Returns code to indicate that all tracked properties have received a value. This is needed in the toBuilder() constructor, since it assigns to the corresponding fields directly without going through their setters.
      • markAsSet

        public String markAsSet​(com.google.auto.value.processor.AutoValueishProcessor.Property p)
        Returns code to indicate that the given property has been set, if assigning to the property field is not enough. For reference (non-primitive) properties, assignment is enough, but for primitive properties we also need to set a bit in the bitmask.
      • missingRequiredProperty

        public String missingRequiredProperty​(com.google.auto.value.processor.AutoValueishProcessor.Property p)
        Returns an expression that is true if the given property is required but has not been set. Returns null if the property is not required.
      • noValueToGet

        public String noValueToGet​(com.google.auto.value.processor.AutoValueishProcessor.Property p)
        Returns an expression that is true if the given property has not been given a value. That's only different from missingRequiredProperty(com.google.auto.value.processor.AutoValueishProcessor.Property) if the property has a Kotlin default. If so, we don't require it to be set at build time (because Kotlin will supply the default), but we do require it to be set if it is accessed with a getter on the builder. We don't have access to Kotlin parameter defaults so we can't arrange for the builder field to have the same default value. Rather than returning a bogus zero value we say the value is unset.
      • getAnyMissing

        public abstract String getAnyMissing()
        Returns an expression that is true if any required properties have not been set. Should not be called if there are no required properties.
      • getDefaultedBitmaskParameters

        public abstract String getDefaultedBitmaskParameters()
        Returns additional constructor parameters to indicate what properties have been defaulted, or an empty string if there are none.