aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremcmanus <emcmanus@google.com>2020-04-30 16:21:12 -0700
committerChris Povirk <beigetangerine@gmail.com>2020-05-01 11:09:10 -0400
commit2bbe5068fa94956471e2ff89841d3e6f93d6d441 (patch)
tree09f5d4957050fe82b3fb82c7304a4c63669222ca
parentd6e56d3a75425584dd84b9cc7b15ae7701df7979 (diff)
downloadauto-2bbe5068fa94956471e2ff89841d3e6f93d6d441.tar.gz
Allow boxed @AutoValue properties to be set from the corresponding primitive type.
We also allow the converse, though that's less likely to be useful. RELNOTES=Allow boxed @AutoValue properties to be set from the corresponding primitive type. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=309320843
-rw-r--r--value/src/it/functional/src/test/java/com/google/auto/value/AutoValueTest.java45
-rw-r--r--value/src/main/java/com/google/auto/value/processor/BuilderMethodClassifier.java6
2 files changed, 50 insertions, 1 deletions
diff --git a/value/src/it/functional/src/test/java/com/google/auto/value/AutoValueTest.java b/value/src/it/functional/src/test/java/com/google/auto/value/AutoValueTest.java
index 88477284..3a024ab1 100644
--- a/value/src/it/functional/src/test/java/com/google/auto/value/AutoValueTest.java
+++ b/value/src/it/functional/src/test/java/com/google/auto/value/AutoValueTest.java
@@ -1571,6 +1571,51 @@ public class AutoValueTest {
}
@AutoValue
+ public abstract static class PrimitiveAndBoxed {
+ public abstract int anInt();
+
+ @Nullable
+ public abstract Integer aNullableInteger();
+
+ public abstract Integer aNonNullableInteger();
+
+ public abstract Builder toBuilder();
+
+ public static Builder builder() {
+ return new AutoValue_AutoValueTest_PrimitiveAndBoxed.Builder();
+ }
+
+ @AutoValue.Builder
+ public interface Builder {
+ Builder setAnInt(Integer x);
+
+ Builder setANullableInteger(int x);
+
+ Builder setANonNullableInteger(int x);
+
+ PrimitiveAndBoxed build();
+ }
+ }
+
+ @Test
+ public void testPrimitiveAndBoxed() {
+ PrimitiveAndBoxed instance1 =
+ PrimitiveAndBoxed.builder().setAnInt(17).setANonNullableInteger(23).build();
+ assertThat(instance1.anInt()).isEqualTo(17);
+ assertThat(instance1.aNullableInteger()).isNull();
+ assertThat(instance1.aNonNullableInteger()).isEqualTo(23);
+
+ PrimitiveAndBoxed instance2 = instance1.toBuilder().setANullableInteger(5).build();
+ assertThat(instance2.aNullableInteger()).isEqualTo(5);
+
+ try {
+ instance1.toBuilder().setAnInt(null);
+ fail();
+ } catch (NullPointerException expected) {
+ }
+ }
+
+ @AutoValue
public abstract static class OptionalPropertiesWithBuilder {
public abstract com.google.common.base.Optional<String> optionalString();
diff --git a/value/src/main/java/com/google/auto/value/processor/BuilderMethodClassifier.java b/value/src/main/java/com/google/auto/value/processor/BuilderMethodClassifier.java
index 41957969..97a50e8b 100644
--- a/value/src/main/java/com/google/auto/value/processor/BuilderMethodClassifier.java
+++ b/value/src/main/java/com/google/auto/value/processor/BuilderMethodClassifier.java
@@ -432,7 +432,11 @@ class BuilderMethodClassifier {
MoreTypes.asExecutable(
typeUtils.asMemberOf(MoreTypes.asDeclared(builderType.asType()), setter));
TypeMirror parameterType = finalSetter.getParameterTypes().get(0);
- if (typeUtils.isSameType(parameterType, targetType)) {
+ // Two types are assignable to each other if they are the same type, or if one is primitive and
+ // the other is the corresponding boxed type. There might be other cases where this is true, but
+ // we're likely to want to accept those too.
+ if (typeUtils.isAssignable(parameterType, targetType)
+ && typeUtils.isAssignable(targetType, parameterType)) {
if (nullableParameter) {
boolean nullableProperty =
nullableAnnotationFor(valueGetter, valueGetter.getReturnType()).isPresent();