aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉamonn McManus <emcmanus@google.com>2021-06-24 13:56:13 -0700
committerGoogle Java Core Libraries <java-libraries-firehose+copybara@google.com>2021-06-24 13:57:00 -0700
commit05ea13561169d5c6c0ea6711f1b7b3eb1492920d (patch)
treeb227d4b2cef17d95e0e792c38ca469e080599284
parente688dd708e2202e0ac8a00be49fca0b9bcf252df (diff)
downloadauto-05ea13561169d5c6c0ea6711f1b7b3eb1492920d.tar.gz
Ensure that AutoBuilder works with property builders.
Previously it worked, but only if the builder interface also had a setter for the property. Otherwise you would get an exception because the template referenced the `$toBuilderMethods` variable, which was undefined. RELNOTES=Fixed a bug with AutoBuilder and property builder methods. PiperOrigin-RevId: 381330964
-rw-r--r--value/src/it/functional/src/test/java/com/google/auto/value/AutoBuilderTest.java24
-rw-r--r--value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java1
-rw-r--r--value/src/main/java/com/google/auto/value/processor/AutoValueOrBuilderTemplateVars.java7
-rw-r--r--value/src/main/java/com/google/auto/value/processor/AutoValueTemplateVars.java4
4 files changed, 32 insertions, 4 deletions
diff --git a/value/src/it/functional/src/test/java/com/google/auto/value/AutoBuilderTest.java b/value/src/it/functional/src/test/java/com/google/auto/value/AutoBuilderTest.java
index a1c4eccd..952edaac 100644
--- a/value/src/it/functional/src/test/java/com/google/auto/value/AutoBuilderTest.java
+++ b/value/src/it/functional/src/test/java/com/google/auto/value/AutoBuilderTest.java
@@ -405,6 +405,30 @@ public final class AutoBuilderTest {
}
}
+ static <T> String concatList(ImmutableList<T> list) {
+ // We're avoiding streams for now so we compile this in Java 7 mode in CompileWithEclipseTest.
+ StringBuilder sb = new StringBuilder();
+ for (T element : list) {
+ sb.append(element);
+ }
+ return sb.toString();
+ }
+
+ @AutoBuilder(callMethod = "concatList")
+ interface ConcatListCaller<T> {
+ ImmutableList.Builder<T> listBuilder();
+
+ String call();
+ }
+
+ @Test
+ public void propertyBuilderWithoutSetter() {
+ ConcatListCaller<Integer> caller = new AutoBuilder_AutoBuilderTest_ConcatListCaller<>();
+ caller.listBuilder().add(1, 1, 2, 3, 5, 8);
+ String s = caller.call();
+ assertThat(s).isEqualTo("112358");
+ }
+
static <K, V extends Number> Map<K, V> singletonMap(K key, V value) {
return Collections.singletonMap(key, value);
}
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 7a700431..b6a578fc 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,6 +143,7 @@ public class AutoBuilderProcessor extends AutoValueishProcessor {
vars.build = build(executable);
vars.types = typeUtils();
vars.toBuilderConstructor = false;
+ vars.toBuilderMethods = ImmutableList.of();
defineSharedVarsForType(autoBuilderType, ImmutableSet.of(), vars);
String text = vars.toText();
text = TypeEncoder.decode(text, processingEnv, vars.pkg, autoBuilderType.asType());
diff --git a/value/src/main/java/com/google/auto/value/processor/AutoValueOrBuilderTemplateVars.java b/value/src/main/java/com/google/auto/value/processor/AutoValueOrBuilderTemplateVars.java
index 83aab3ce..86cf4974 100644
--- a/value/src/main/java/com/google/auto/value/processor/AutoValueOrBuilderTemplateVars.java
+++ b/value/src/main/java/com/google/auto/value/processor/AutoValueOrBuilderTemplateVars.java
@@ -131,6 +131,13 @@ abstract class AutoValueOrBuilderTemplateVars extends AutoValueishTemplateVars {
Boolean toBuilderConstructor;
/**
+ * Any {@code toBuilder()} methods, that is methods that return the builder type. AutoBuilder does
+ * not currently support this, but it's included in these shared variables to simplify the
+ * template.
+ */
+ ImmutableList<SimpleMethod> toBuilderMethods;
+
+ /**
* Whether to include identifiers in strings in the generated code. If false, exception messages
* will not mention properties by name, and {@code toString()} will include neither property names
* nor the name of the {@code @AutoValue} class.
diff --git a/value/src/main/java/com/google/auto/value/processor/AutoValueTemplateVars.java b/value/src/main/java/com/google/auto/value/processor/AutoValueTemplateVars.java
index d68466a4..e53b92e9 100644
--- a/value/src/main/java/com/google/auto/value/processor/AutoValueTemplateVars.java
+++ b/value/src/main/java/com/google/auto/value/processor/AutoValueTemplateVars.java
@@ -15,7 +15,6 @@
*/
package com.google.auto.value.processor;
-import com.google.common.collect.ImmutableList;
import com.google.escapevelocity.Template;
/**
@@ -42,9 +41,6 @@ class AutoValueTemplateVars extends AutoValueOrBuilderTemplateVars {
*/
String modifiers;
- /** Any {@code toBuilder()} methods, that is methods that return the builder type. */
- ImmutableList<SimpleMethod> toBuilderMethods;
-
private static final Template TEMPLATE = parsedTemplateForResource("autovalue.vm");
@Override