Problem
When an annotation processor generated type (RecordBuilder or otherwise) is referenced by a record configured to generate a builder, the builder code generated by @RecordBuilder does not generate the required import statement for that referenced type when the referenced type is in a different package.
Reproduction
For example, if we have a simple Parent class configured to generate ParentBuilder in a root package:
package io.soabase.recordbuilder.test.generated;
import io.soabase.recordbuilder.core.RecordBuilder;
@RecordBuilder
public record Parent(String foo) {
}
and a Child class configured to generate a ChildBuilder in a child package:
package io.soabase.recordbuilder.test.generated.nested;
import io.soabase.recordbuilder.core.RecordBuilder;
import io.soabase.recordbuilder.test.generated.ParentBuilder;
@RecordBuilder
public record Child(ParentBuilder parentBuilder) {
}
Expected behavior
The generated ChildBuilder java file contains an import io.soabase.recordbuilder.test.generated.ParentBuilder; line in its source and therefore compiles successfully.
Actual results
The generated ChildBuilder code will incorrectly accidentally omit the import statement required for ParentBuilder:
// Auto generated by io.soabase.recordbuilder.core.RecordBuilder: https://github.com/Randgalt/record-builder
package io.soabase.recordbuilder.test.generated.nested;
import java.util.AbstractMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Stream;
import javax.annotation.processing.Generated;
@Generated("io.soabase.recordbuilder.core.RecordBuilder")
public class ChildBuilder {
private ParentBuilder parentBuilder;
@Generated("io.soabase.recordbuilder.core.RecordBuilder")
private ChildBuilder() {
}
<SNIP>
}
Compilation returns an error on only on the generated ChildBuilder code:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.14.0:compile (default-compile) on project record-builder-test: Compilation failure: Compilation failure:
[ERROR] <SNIP>/record-builder/record-builder-test/target/generated-sources/annotations/io/soabase/recordbuilder/test/generated/nested/ChildBuilder.java:[13,13] cannot find symbol
[ERROR] symbol: class ParentBuilder
[ERROR] location: class io.soabase.recordbuilder.test.generated.nested.ChildBuilder
[ERROR] <SNIP>/record-builder/record-builder-test/target/generated-sources/annotations/io/soabase/recordbuilder/test/generated/nested/ChildBuilder.java:[20,26] cannot find symbol
[ERROR] symbol: class ParentBuilder
[ERROR] location: class io.soabase.recordbuilder.test.generated.nested.ChildBuilder
[ERROR] <SNIP>/record-builder/record-builder-test/target/generated-sources/annotations/io/soabase/recordbuilder/test/generated/nested/ChildBuilder.java:[28,31] cannot find symbol
[ERROR] symbol: class ParentBuilder
[ERROR] location: class io.soabase.recordbuilder.test.generated.nested.ChildBuilder
[ERROR] <SNIP>/record-builder/record-builder-test/target/generated-sources/annotations/io/soabase/recordbuilder/test/generated/nested/ChildBuilder.java:[95,39] cannot find symbol
[ERROR] symbol: class ParentBuilder
[ERROR] location: class io.soabase.recordbuilder.test.generated.nested.ChildBuilder
[ERROR] <SNIP>/record-builder/record-builder-test/target/generated-sources/annotations/io/soabase/recordbuilder/test/generated/nested/ChildBuilder.java:[104,12] cannot find symbol
[ERROR] symbol: class ParentBuilder
[ERROR] location: class io.soabase.recordbuilder.test.generated.nested.ChildBuilder
[ERROR] <SNIP>/record-builder/record-builder-test/target/generated-sources/annotations/io/soabase/recordbuilder/test/generated/nested/ChildBuilder.java:[117,9] cannot find symbol
[ERROR] symbol: class ParentBuilder
[ERROR] location: interface io.soabase.recordbuilder.test.generated.nested.ChildBuilder.With
[ERROR] <SNIP>/record-builder/record-builder-test/target/generated-sources/annotations/io/soabase/recordbuilder/test/generated/nested/ChildBuilder.java:[141,41] cannot find symbol
[ERROR] symbol: class ParentBuilder
[ERROR] location: interface io.soabase.recordbuilder.test.generated.nested.ChildBuilder.With
[ERROR] <SNIP>/record-builder/record-builder-test/target/generated-sources/annotations/io/soabase/recordbuilder/test/generated/nested/ChildBuilder.java:[157,16] cannot find symbol
[ERROR] symbol: class ParentBuilder
[ERROR] location: class io.soabase.recordbuilder.test.generated.nested.ChildBuilder._FromWith
[ERROR] -> [Help 1]
Additional details
Records which use an annotation-generated types which are in the same package do work correctly due to the import being unnecessary. That is, the following class with the previous example does not experience any compilation issues:
package io.soabase.recordbuilder.test.generated;
import io.soabase.recordbuilder.core.RecordBuilder;
@RecordBuilder
public record Sibling(ParentBuilder parentBuilder) {
}
Full reproduction diff
diff --git a/record-builder-test/src/main/java/io/soabase/recordbuilder/test/generated/Parent.java b/record-builder-test/src/main/java/io/soabase/recordbuilder/test/generated/Parent.java
new file mode 100644
index 0000000..29bb117
--- /dev/null
+++ b/record-builder-test/src/main/java/io/soabase/recordbuilder/test/generated/Parent.java
@@ -0,0 +1,8 @@
+package io.soabase.recordbuilder.test.generated;
+
+import io.soabase.recordbuilder.core.RecordBuilder;
+
+@RecordBuilder
+public record Parent(String foo) {
+
+}
diff --git a/record-builder-test/src/main/java/io/soabase/recordbuilder/test/generated/Sibling.java b/record-builder-test/src/main/java/io/soabase/recordbuilder/test/generated/Sibling.java
new file mode 100644
index 0000000..4d94214
--- /dev/null
+++ b/record-builder-test/src/main/java/io/soabase/recordbuilder/test/generated/Sibling.java
@@ -0,0 +1,8 @@
+package io.soabase.recordbuilder.test.generated;
+
+import io.soabase.recordbuilder.core.RecordBuilder;
+
+@RecordBuilder
+public record Sibling(ParentBuilder parentBuilder) {
+
+}
diff --git a/record-builder-test/src/main/java/io/soabase/recordbuilder/test/generated/nested/Child.java b/record-builder-test/src/main/java/io/soabase/recordbuilder/test/generated/nested/Child.java
new file mode 100644
index 0000000..6d17a1b
--- /dev/null
+++ b/record-builder-test/src/main/java/io/soabase/recordbuilder/test/generated/nested/Child.java
@@ -0,0 +1,9 @@
+package io.soabase.recordbuilder.test.generated.nested;
+
+import io.soabase.recordbuilder.core.RecordBuilder;
+import io.soabase.recordbuilder.test.generated.ParentBuilder;
+
+@RecordBuilder
+public record Child(ParentBuilder parentBuilder) {
+
+}
Problem
When an annotation processor generated type (RecordBuilder or otherwise) is referenced by a record configured to generate a builder, the builder code generated by
@RecordBuilderdoes not generate the required import statement for that referenced type when the referenced type is in a different package.Reproduction
For example, if we have a simple Parent class configured to generate
ParentBuilderin a root package:and a
Childclass configured to generate aChildBuilderin a child package:Expected behavior
The generated
ChildBuilderjava file contains animport io.soabase.recordbuilder.test.generated.ParentBuilder;line in its source and therefore compiles successfully.Actual results
The generated ChildBuilder code will incorrectly accidentally omit the import statement required for
ParentBuilder:Compilation returns an error on only on the generated
ChildBuildercode:Additional details
Records which use an annotation-generated types which are in the same package do work correctly due to the import being unnecessary. That is, the following class with the previous example does not experience any compilation issues:
Full reproduction diff