diff options
author | Éamonn McManus <emcmanus@google.com> | 2021-04-30 08:56:40 -0700 |
---|---|---|
committer | Google Java Core Libraries <java-libraries-firehose+copybara@google.com> | 2021-04-30 08:57:23 -0700 |
commit | aaf39f6c211fc34e4fa4abc5954c1e3a6db22cdc (patch) | |
tree | b15403b1e0472ceac7de02aa3e585e3310618fe4 /value | |
parent | 8cb93ed1b00f21d05286ae0fe9a6b4c9a8c5f0fe (diff) | |
download | auto-aaf39f6c211fc34e4fa4abc5954c1e3a6db22cdc.tar.gz |
Use the correct `package` in the generated `AutoBuilder_Foo` class.
Previously it was erroneously using the package from the `ofClass` class. That's often the same package, but when it isn't the generated code was wrong.
RELNOTES=n/a
PiperOrigin-RevId: 371342620
Diffstat (limited to 'value')
3 files changed, 45 insertions, 2 deletions
diff --git a/value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java b/value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java index 7d0ef2e6..5952f624 100644 --- a/value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java +++ b/value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java @@ -143,7 +143,7 @@ public class AutoBuilderProcessor extends AutoValueishProcessor { vars.build = build(executable); vars.types = typeUtils(); vars.toBuilderConstructor = false; - defineSharedVarsForType(ofClass, ImmutableSet.of(), vars); + defineSharedVarsForType(autoBuilderType, ImmutableSet.of(), vars); String text = vars.toText(); text = TypeEncoder.decode(text, processingEnv, vars.pkg, autoBuilderType.asType()); text = Reformatter.fixup(text); diff --git a/value/src/main/java/com/google/auto/value/processor/autobuilder.vm b/value/src/main/java/com/google/auto/value/processor/autobuilder.vm index fd8ef09d..01f61b9c 100644 --- a/value/src/main/java/com/google/auto/value/processor/autobuilder.vm +++ b/value/src/main/java/com/google/auto/value/processor/autobuilder.vm @@ -14,7 +14,7 @@ ## Template for each generated AutoValue_Foo class. ## This template uses the Apache Velocity Template Language (VTL). -## The variables ($pkg, $props, and so on) are defined by the fields of AutoValueTemplateVars. +## The variables ($pkg, $props, and so on) are defined by the fields of AutoBuilderTemplateVars. ## ## Comments, like this one, begin with ##. The comment text extends up to and including the newline ## character at the end of the line. So comments also serve to join a line to the next one. diff --git a/value/src/test/java/com/google/auto/value/processor/AutoBuilderCompilationTest.java b/value/src/test/java/com/google/auto/value/processor/AutoBuilderCompilationTest.java index aabdbc71..ddf12ff3 100644 --- a/value/src/test/java/com/google/auto/value/processor/AutoBuilderCompilationTest.java +++ b/value/src/test/java/com/google/auto/value/processor/AutoBuilderCompilationTest.java @@ -155,6 +155,49 @@ public final class AutoBuilderCompilationTest { } @Test + public void buildOtherPackage() { + JavaFileObject built = + JavaFileObjects.forSourceLines( + "com.example.Built", + "package com.example;", + "", + "public class Built {", + " private final int anInt;", + " private final String aString;", + "", + " public Built(int anInt, String aString) {", + " this.anInt = anInt;", + " this.aString = aString;", + " }", + "}"); + JavaFileObject builder = + JavaFileObjects.forSourceLines( + "foo.bar.Builder", + "package foo.bar;", + "", + "import com.example.Built;", + "import com.google.auto.value.AutoBuilder;", + "", + "@AutoBuilder(ofClass = Built.class)", + "public interface Builder {", + " public static Builder builder() {", + " return new AutoBuilder_Builder();", + " }", + "", + " Builder setAnInt(int x);", + " Builder setAString(String x);", + " Built build();", + "}"); + Compilation compilation = + javac() + .withProcessors(new AutoBuilderProcessor()) + .withOptions("-Acom.google.auto.value.AutoBuilderIsUnstable") + .compile(built, builder); + assertThat(compilation).succeededWithoutWarnings(); + assertThat(compilation).generatedSourceFile("foo.bar.AutoBuilder_Builder"); + } + + @Test public void autoBuilderOnEnum() { JavaFileObject javaFileObject = JavaFileObjects.forSourceLines( |