google / google/auto

@NotNull and @Nullable not replicated on Builders

Open
#512 7 comments 2 reactions 0 assignees View on GitHub
P3
Dominant language
Java
Stars
10.6k
Forks
1.2k
Avg merge
6h 32m
Merged PRs (30d)
13

Description

If I create a class

```
@AutoValue
abstract class Test {

@Nonnull
abstract String test();
@Nonnull
abstract Builder toBuilder();

@AutoValue.Builder
abstract static class Builder {
@Nonnull
abstract Builder setTest(@Nonnull String test);
@Nonnull
abstract Test build();
}
}
```

then I get this auto generated code
```
final class AutoValue_Test extends Test {

private final String test;

private AutoValue_Test(
String test) {
this.test = test;
}

@Nonnull
@Override
String test() {
return test;
}

@Override
public String toString() {
return "Test{"
+ "test=" + test
+ "}";
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof Test) {
Test that = (Test) o;
return (this.test.equals(that.test()));
}
return false;
}

@Override
public int hashCode() {
int h = 1;
h *= 1000003;
h ^= this.test.hashCode();
return h;
}

@Override
Test.Builder toBuilder() {
return new Builder(this);
}

static final class Builder extends Test.Builder {
private String test;
Builder() {
}
private Builder(Test source) {
this.test = source.test();
}
@Override
Test.Builder setTest(String test) {
if (test == null) {
throw new NullPointerException("Null test");
}
this.test = test;
return this;
}
@Override
Test build() {
String missing = "";
if (this.test == null) {
missing += " test";
}
if (!missing.isEmpty()) {
throw new IllegalStateException("Missing required properties:" + missing);
}
return new AutoValue_Test(
this.test);
}
}

}
```

Notice that all the class generated code replicates the @Nullable and @NonNull annotations, but the builder does not.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.