Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make AutoBuilder produce an error if it encounters a @Nullable primitive parameter #1682

Merged
1 commit merged into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,20 @@ private ImmutableSet<Property> propertySet(
.map(
v -> {
String name = v.getSimpleName().toString();
return newProperty(
v,
identifiers.get(v),
propertyToGetterName.get(name),
Optional.ofNullable(builderInitializers.get(name)),
executable.isOptional(name),
nullables);
Property p =
newProperty(
v,
identifiers.get(v),
propertyToGetterName.get(name),
Optional.ofNullable(builderInitializers.get(name)),
executable.isOptional(name),
nullables);
if (p.isNullable() && v.asType().getKind().isPrimitive()) {
errorReporter()
.reportError(
v, "[AutoBuilderNullPrimitive] Primitive types cannot be @Nullable");
}
return p;
})
.collect(toImmutableSet());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,46 @@ public void nullableSetterForNonNullableParameter() {
.onLineContaining("thing(@Nullable String x)");
}

@Test
public void nullablePrimitiveParameter() {
JavaFileObject javaFileObject =
JavaFileObjects.forSourceLines(
"foo.bar.Baz",
"package foo.bar;",
"",
"import com.google.auto.value.AutoBuilder;",
"import com.example.annotations.Nullable;",
"",
"class Baz {",
" Baz(@Nullable int thing) {}",
"",
" @AutoBuilder",
" interface Builder {",
" abstract Builder thing(int x);",
" abstract Baz build();",
" }",
"}");
JavaFileObject nullableFileObject =
JavaFileObjects.forSourceLines(
"com.example.annotations.Nullable",
"package com.example.annotations;",
"",
"import java.lang.annotation.ElementType;",
"import java.lang.annotation.Target;",
"",
"@Target(ElementType.TYPE_USE)",
"public @interface Nullable {}");
Compilation compilation =
javac()
.withProcessors(new AutoBuilderProcessor())
.compile(javaFileObject, nullableFileObject);
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining("[AutoBuilderNullPrimitive] Primitive types cannot be @Nullable")
.inFile(javaFileObject)
.onLineContaining("Baz(@Nullable int thing)");
}

@Test
public void setterWrongType() {
JavaFileObject javaFileObject =
Expand Down