Skip to content

Commit

Permalink
Ensure that AutoBuilder works with property builders.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eamonnmcmanus authored and Google Java Core Libraries committed Jun 24, 2021
1 parent e688dd7 commit 05ea135
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,30 @@ public void propertyBuilder() {
}
}

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ void processType(TypeElement autoBuilderType) {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.google.auto.value.processor;

import com.google.common.collect.ImmutableList;
import com.google.escapevelocity.Template;

/**
Expand All @@ -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
Expand Down

0 comments on commit 05ea135

Please sign in to comment.