Skip to content

Commit

Permalink
Fix a template bug concerning @AutoOneOf arrays. Like AutoValue, Auto…
Browse files Browse the repository at this point in the history
…OneOf 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
  • Loading branch information
eamonnmcmanus authored and netdpb committed Aug 19, 2019
1 parent 91118b0 commit 81134b5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion value/processor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<dependency>
<groupId>com.google.escapevelocity</groupId>
<artifactId>escapevelocity</artifactId>
<version>0.9</version>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>net.ltgt.gradle.incap</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,40 @@ public void reservedWordProperty() {
assertThat(pkg.toString()).isEqualTo("LetterOrPackage{package=pacquet}");
}

@AutoOneOf(ArrayValue.Kind.class)
public abstract static class ArrayValue {
public enum Kind {
STRING,
INTS
}

public abstract Kind getKind();

public abstract String string();

@SuppressWarnings("mutable")
public abstract int[] ints();

public static ArrayValue ofString(String string) {
return AutoOneOf_AutoOneOfTest_ArrayValue.string(string);
}

public static ArrayValue ofInts(int[] ints) {
return AutoOneOf_AutoOneOfTest_ArrayValue.ints(ints);
}
}

@Test
public void arrayValues() {
ArrayValue string = ArrayValue.ofString("foo");
ArrayValue ints1 = ArrayValue.ofInts(new int[] {17, 23});
ArrayValue ints2 = ArrayValue.ofInts(new int[] {17, 23});
new EqualsTester()
.addEqualityGroup(string)
.addEqualityGroup(ints1, ints2)
.testEquals();
}

@Retention(RetentionPolicy.RUNTIME)
public @interface CopyTest {
int value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ final class $generatedClass {
if (x instanceof $origClass) {
$origClass$wildcardTypes that = ($origClass$wildcardTypes) x;
return this.${kindGetter}() == that.${kindGetter}()
&& #equalsThatExpression($p);
&& #equalsThatExpression($p "Impl_$p");
} else {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ ${modifiers}class $subclass$formalTypes extends $origClass$actualTypes {
$origClass$wildcardTypes that = ($origClass$wildcardTypes) o;
return ##
#foreach ($p in $props)
#equalsThatExpression ($p)##
#equalsThatExpression ($p $subclass)##
#if ($foreach.hasNext)

&& ##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
## The expression should be surrounded by parentheses if otherwise there might be precedence
## problems when it is followed by &&.
## A reminder that trailing ## here serves to delete the newline, which we don't want in the output.
#macro (equalsThatExpression $p)
#macro (equalsThatExpression $p $subclass)
#if ($p.kind == "FLOAT")
Float.floatToIntBits(this.$p) == Float.floatToIntBits(that.${p.getter}()) ##
#elseif ($p.kind == "DOUBLE")
Expand Down

0 comments on commit 81134b5

Please sign in to comment.