Skip to content

Commit

Permalink
Ensure that type annotations appear in AutoBuilder property types.
Browse files Browse the repository at this point in the history
The issue that this showed up with had to do with records and nested types, but I think the problem was more general than that.

RELNOTES=An issue with `@Nullable` type-use annotations in AutoBuilder has been fixed.
PiperOrigin-RevId: 691852142
  • Loading branch information
eamonnmcmanus authored and Google Java Core Libraries committed Oct 31, 2024
1 parent c34894b commit 289c003
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ private Property newProperty(
return new Property(
name,
identifier,
TypeEncoder.encode(type),
TypeEncoder.encodeWithAnnotations(type),
new AnnotatedTypeMirror(type),
nullableAnnotation,
nullables,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ ${builderClassModifiers}class ${builderName}${builderFormalTypes} ##
private ${builderPropertyBuilders[$p.name].nullableBuilderType} ##
${builderPropertyBuilders[$p.name].name};

#end
#end

private $p.builderFieldType $p $p.builderInitializer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,42 @@ public void simpleRecord() {
.hasSourceEquivalentTo(EXPECTED_SIMPLE_OUTPUT);
}

@Test
public void recordWithNullableNestedComponentType() {
double version = Double.parseDouble(JAVA_SPECIFICATION_VERSION.value());
assume().that(version).isAtLeast(16.0);
JavaFileObject javaFileObject =
JavaFileObjects.forSourceLines(
"foo.bar.Baz",
"package foo.bar;",
"",
"import com.google.auto.value.AutoBuilder;",
"import org.checkerframework.checker.nullness.qual.Nullable;",
"",
"public record Baz(@Nullable Nested nested) {",
" public static Builder builder() {",
" return new AutoBuilder_Baz_Builder();",
" }",
"",
" @AutoBuilder",
" public interface Builder {",
" Builder setNested(Nested nested);",
" Baz build();",
" }",
"",
" public record Nested() {}",
"}");
Compilation compilation =
javac()
.withProcessors(new AutoBuilderProcessor())
.withOptions("-A" + Nullables.NULLABLE_OPTION + "=org.checkerframework.checker.nullness.qual.Nullable")
.compile(javaFileObject);
assertThat(compilation)
.generatedSourceFile("foo.bar.AutoBuilder_Baz_Builder")
.contentsAsUtf8String()
.contains("private Baz.@Nullable Nested nested;");
}

@Test
public void buildOtherPackage() {
JavaFileObject built =
Expand Down

0 comments on commit 289c003

Please sign in to comment.