diff options
author | Éamonn McManus <emcmanus@google.com> | 2021-05-19 08:40:38 -0700 |
---|---|---|
committer | Google Java Core Libraries <java-libraries-firehose+copybara@google.com> | 2021-05-19 08:41:07 -0700 |
commit | f00c32a51676058758e9b0487e5c8a6f5c69ed14 (patch) | |
tree | 5430eecb28ee79917be9bc628f1a8a360339942b /value/src/test/java/com/google | |
parent | cd55f631ba54aabe5290de7701b1c449815413e2 (diff) | |
download | auto-f00c32a51676058758e9b0487e5c8a6f5c69ed14.tar.gz |
Generate missing-property checks in a way that is easier for null-analysis to understand.
The new code is more verbose than the old, since it has two null checks for every property (when there is more than one required property). The second check will only be executed when at least one property is missing and we are about to throw an exception. The extra checks also don't happen when compiling without identifiers, as is typically the case in code-size-sensitive environments like Android.
RELNOTES=Generated builder code is now slightly friendly to null-analysis.
PiperOrigin-RevId: 374652915
Diffstat (limited to 'value/src/test/java/com/google')
-rw-r--r-- | value/src/test/java/com/google/auto/value/processor/AutoBuilderCompilationTest.java | 17 | ||||
-rw-r--r-- | value/src/test/java/com/google/auto/value/processor/AutoValueCompilationTest.java | 24 |
2 files changed, 22 insertions, 19 deletions
diff --git a/value/src/test/java/com/google/auto/value/processor/AutoBuilderCompilationTest.java b/value/src/test/java/com/google/auto/value/processor/AutoBuilderCompilationTest.java index ddf12ff3..2a5f55a0 100644 --- a/value/src/test/java/com/google/auto/value/processor/AutoBuilderCompilationTest.java +++ b/value/src/test/java/com/google/auto/value/processor/AutoBuilderCompilationTest.java @@ -58,14 +58,15 @@ public final class AutoBuilderCompilationTest { "", " @Override", " public Baz build() {", - " String missing = \"\";", - " if (this.anInt == null) {", - " missing += \" anInt\";", - " }", - " if (this.aString == null) {", - " missing += \" aString\";", - " }", - " if (!missing.isEmpty()) {", + " if (this.anInt == null", + " || this.aString == null) {", + " StringBuilder missing = new StringBuilder();", + " if (this.anInt == null) {", + " missing.append(\" anInt\");", + " }", + " if (this.aString == null) {", + " missing.append(\" aString\");", + " }", " throw new IllegalStateException(\"Missing required properties:\" + missing);", " }", " return new Baz(", diff --git a/value/src/test/java/com/google/auto/value/processor/AutoValueCompilationTest.java b/value/src/test/java/com/google/auto/value/processor/AutoValueCompilationTest.java index 4d9026ac..1baf4b4d 100644 --- a/value/src/test/java/com/google/auto/value/processor/AutoValueCompilationTest.java +++ b/value/src/test/java/com/google/auto/value/processor/AutoValueCompilationTest.java @@ -1311,17 +1311,19 @@ public class AutoValueCompilationTest { + "NestedAutoValue.builder();", " this.aNestedAutoValue = aNestedAutoValue$builder.build();", " }", - " String missing = \"\";", - " if (this.anInt == null) {", - " missing += \" anInt\";", - " }", - " if (this.aByteArray == null) {", - " missing += \" aByteArray\";", - " }", - " if (this.aList == null) {", - " missing += \" aList\";", - " }", - " if (!missing.isEmpty()) {", + " if (this.anInt == null", + " || this.aByteArray == null", + " || this.aList == null) {", + " StringBuilder missing = new StringBuilder();", + " if (this.anInt == null) {", + " missing.append(\" anInt\");", + " }", + " if (this.aByteArray == null) {", + " missing.append(\" aByteArray\");", + " }", + " if (this.aList == null) {", + " missing.append(\" aList\");", + " }", " throw new IllegalStateException(\"Missing required properties:\" + missing);", " }", " return new AutoValue_Baz<T>(", |