aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremcmanus <emcmanus@google.com>2020-02-11 16:54:52 -0800
committerChris Povirk <beigetangerine@gmail.com>2020-02-11 17:15:07 -0800
commitecb6032d6398317da0539ce0ac7287989ea7b0e3 (patch)
tree7e99151ceeb8af317580eb990fac49bc154993be
parent4ab1b53b0ae9a6dbaae3cbc54c270e90fc7daa84 (diff)
downloadauto-ecb6032d6398317da0539ce0ac7287989ea7b0e3.tar.gz
If AutoValue detects an error, don't invoke extensions or generate code.
For example if there's a type mismatch between a property getter and the corresponding setter in a Builder, we may otherwise end up invoking an extension with an inconsistent state. Also the generated code is likely to have compile errors of its own, which distract from the real source of the problem. Fixes https://github.com/google/auto/issues/809. RELNOTES=Don't generate code or invoke extensions if AutoValue detects a problem, for example a mismatch between getters and setters. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=294552938
-rw-r--r--value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java5
-rw-r--r--value/src/test/java/com/google/auto/value/processor/ExtensionTest.java38
2 files changed, 43 insertions, 0 deletions
diff --git a/value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java b/value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java
index a60eef69..6342d802 100644
--- a/value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java
+++ b/value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java
@@ -247,6 +247,11 @@ public class AutoValueProcessor extends AutoValueOrOneOfProcessor {
defineSharedVarsForType(type, methods, vars);
defineVarsForType(type, vars, toBuilderMethods, propertyMethodsAndTypes, builder);
+ // If we've encountered problems then we might end up invoking extensions with inconsistent
+ // state. Anyway we probably don't want to generate code which is likely to provoke further
+ // compile errors to add to the ones we've already seen.
+ errorReporter().abortIfAnyError();
+
GwtCompatibility gwtCompatibility = new GwtCompatibility(type);
vars.gwtCompatibleAnnotation = gwtCompatibility.gwtCompatibleAnnotationString();
diff --git a/value/src/test/java/com/google/auto/value/processor/ExtensionTest.java b/value/src/test/java/com/google/auto/value/processor/ExtensionTest.java
index dabe32b9..062343c1 100644
--- a/value/src/test/java/com/google/auto/value/processor/ExtensionTest.java
+++ b/value/src/test/java/com/google/auto/value/processor/ExtensionTest.java
@@ -1089,6 +1089,44 @@ public class ExtensionTest {
.compilesWithoutError();
}
+ // https://github.com/google/auto/issues/809
+ @Test
+ public void propertyErrorShouldNotCrash() {
+ JavaFileObject autoValueClass = JavaFileObjects.forSourceLines(
+ "test.Test",
+ "package test;",
+ "import com.google.auto.value.AutoValue;",
+ "import java.util.List;",
+ "",
+ "@AutoValue",
+ "public abstract class Test {",
+ " abstract Integer property();",
+ " abstract List<String> listProperty();",
+ "",
+ " @AutoValue.Builder",
+ " public interface Builder {",
+ " Builder property(Integer property);",
+ " Builder listProperty(List<String> listProperty);",
+ " Builder listProperty(Integer listPropertyValues);",
+ " Test build();",
+ " }",
+ "}");
+ // We don't actually expect the extension to be invoked. Previously it was, and that led to a
+ // NullPointerException when calling .setters() in the checker.
+ ContextChecker checker =
+ context -> {
+ assertThat(context.builder()).isPresent();
+ assertThat(context.builder().get().setters()).isEmpty();
+ };
+ ContextCheckingExtension extension = new ContextCheckingExtension(checker);
+ assertThat(autoValueClass)
+ .processedWith(new AutoValueProcessor(ImmutableList.of(extension)))
+ .failsToCompile()
+ .withErrorContaining("Parameter type java.lang.Integer of setter method")
+ .in(autoValueClass)
+ .onLine(14);
+ }
+
private interface ContextChecker extends Consumer<AutoValueExtension.Context> {}
private static class ContextCheckingExtension extends AutoValueExtension {