Skip to content

Ctor null checks omitted when builder is present, but ctor still package-visible. #345

@JakeWharton

Description

@JakeWharton

Given a model

@AutoValue
public abstract class Whatever {
  public abstract String whatever();

  @AutoValue.Builder
  interface Builder {
    Builder whatever(String whatever);
    Whatever build();
  }
}

AutoValue 1.3-rc1 will generate:

@Generated("com.google.auto.value.processor.AutoValueProcessor")
 abstract class $AutoValue_Whatever extends Whatever {

  private final String whatever;

  $AutoValue_Whatever(
      String whatever) {
    this.whatever = whatever;
  }

  // omitted

  static final class Builder implements Whatever.Builder {
    private String whatever;
    Builder() {
    }
    Builder(Whatever source) {
      this.whatever = source.whatever();
    }
    @Override
    public Whatever.Builder whatever(String whatever) {
      this.whatever = whatever;
      return this;
    }
    @Override
    public Whatever build() {
      String missing = "";
      if (whatever == null) {
        missing += " whatever";
      }
      if (!missing.isEmpty()) {
        throw new IllegalStateException("Missing required properties:" + missing);
      }
      return new AutoValue_Whatever(
          this.whatever);
    }
  }

}

As you can see the constructor is still package scoped which the application code could use to insert a null value for whatever. This constructor is also used by extensions which are not null-checking their values since they rely on the constructor to validate the nullability constraints.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions