Skip to content

Commit 81134b5

Browse files
eamonnmcmanusnetdpb
authored andcommitted
Fix a template bug concerning @AutoOneOf arrays. Like AutoValue, AutoOneOf allows values to be primitive arrays, but the code template referenced an undefined variable in that case. The implementation of foo.getMyArray() copies the array, so the implementation of equals(Object) checks whether the Object is of the generated subclass, and if so accesses the private myArray field directly. For @autovalue Foo, this subclass is AutoValue_Foo, and the $subclass template variable is set to that. But for @AutoOneOf Foo, there is a subclass per property, and we actually want AutoOneOf_Foo.Impl_myArray. So we need to make sure that $subclass is set to that in the #equalsThatExpression macro.
This requires updating to EscapeVelocity 0.9.1, which allows variable references within strings, like "Impl_$p". RELNOTES=Primitive arrays now work in @AutoOneOf classes. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=256190191
1 parent 91118b0 commit 81134b5

File tree

5 files changed

+38
-4
lines changed

5 files changed

+38
-4
lines changed

value/processor/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<dependency>
6262
<groupId>com.google.escapevelocity</groupId>
6363
<artifactId>escapevelocity</artifactId>
64-
<version>0.9</version>
64+
<version>0.9.1</version>
6565
</dependency>
6666
<dependency>
6767
<groupId>net.ltgt.gradle.incap</groupId>

value/src/it/functional/src/test/java/com/google/auto/value/AutoOneOfTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,40 @@ public void reservedWordProperty() {
470470
assertThat(pkg.toString()).isEqualTo("LetterOrPackage{package=pacquet}");
471471
}
472472

473+
@AutoOneOf(ArrayValue.Kind.class)
474+
public abstract static class ArrayValue {
475+
public enum Kind {
476+
STRING,
477+
INTS
478+
}
479+
480+
public abstract Kind getKind();
481+
482+
public abstract String string();
483+
484+
@SuppressWarnings("mutable")
485+
public abstract int[] ints();
486+
487+
public static ArrayValue ofString(String string) {
488+
return AutoOneOf_AutoOneOfTest_ArrayValue.string(string);
489+
}
490+
491+
public static ArrayValue ofInts(int[] ints) {
492+
return AutoOneOf_AutoOneOfTest_ArrayValue.ints(ints);
493+
}
494+
}
495+
496+
@Test
497+
public void arrayValues() {
498+
ArrayValue string = ArrayValue.ofString("foo");
499+
ArrayValue ints1 = ArrayValue.ofInts(new int[] {17, 23});
500+
ArrayValue ints2 = ArrayValue.ofInts(new int[] {17, 23});
501+
new EqualsTester()
502+
.addEqualityGroup(string)
503+
.addEqualityGroup(ints1, ints2)
504+
.testEquals();
505+
}
506+
473507
@Retention(RetentionPolicy.RUNTIME)
474508
public @interface CopyTest {
475509
int value();

value/src/main/java/com/google/auto/value/processor/autooneof.vm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ final class $generatedClass {
189189
if (x instanceof $origClass) {
190190
$origClass$wildcardTypes that = ($origClass$wildcardTypes) x;
191191
return this.${kindGetter}() == that.${kindGetter}()
192-
&& #equalsThatExpression($p);
192+
&& #equalsThatExpression($p "Impl_$p");
193193
} else {
194194
return false;
195195
}

value/src/main/java/com/google/auto/value/processor/autovalue.vm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ ${modifiers}class $subclass$formalTypes extends $origClass$actualTypes {
145145
$origClass$wildcardTypes that = ($origClass$wildcardTypes) o;
146146
return ##
147147
#foreach ($p in $props)
148-
#equalsThatExpression ($p)##
148+
#equalsThatExpression ($p $subclass)##
149149
#if ($foreach.hasNext)
150150

151151
&& ##

value/src/main/java/com/google/auto/value/processor/equalshashcode.vm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
## The expression should be surrounded by parentheses if otherwise there might be precedence
4141
## problems when it is followed by &&.
4242
## A reminder that trailing ## here serves to delete the newline, which we don't want in the output.
43-
#macro (equalsThatExpression $p)
43+
#macro (equalsThatExpression $p $subclass)
4444
#if ($p.kind == "FLOAT")
4545
Float.floatToIntBits(this.$p) == Float.floatToIntBits(that.${p.getter}()) ##
4646
#elseif ($p.kind == "DOUBLE")

0 commit comments

Comments
 (0)