Skip to content

Commit ecb6032

Browse files
eamonnmcmanuscpovirk
authored andcommitted
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 #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
1 parent 4ab1b53 commit ecb6032

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ void processType(TypeElement type) {
247247
defineSharedVarsForType(type, methods, vars);
248248
defineVarsForType(type, vars, toBuilderMethods, propertyMethodsAndTypes, builder);
249249

250+
// If we've encountered problems then we might end up invoking extensions with inconsistent
251+
// state. Anyway we probably don't want to generate code which is likely to provoke further
252+
// compile errors to add to the ones we've already seen.
253+
errorReporter().abortIfAnyError();
254+
250255
GwtCompatibility gwtCompatibility = new GwtCompatibility(type);
251256
vars.gwtCompatibleAnnotation = gwtCompatibility.gwtCompatibleAnnotationString();
252257

value/src/test/java/com/google/auto/value/processor/ExtensionTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,44 @@ public void oddBuilderContext() {
10891089
.compilesWithoutError();
10901090
}
10911091

1092+
// https://github.com/google/auto/issues/809
1093+
@Test
1094+
public void propertyErrorShouldNotCrash() {
1095+
JavaFileObject autoValueClass = JavaFileObjects.forSourceLines(
1096+
"test.Test",
1097+
"package test;",
1098+
"import com.google.auto.value.AutoValue;",
1099+
"import java.util.List;",
1100+
"",
1101+
"@AutoValue",
1102+
"public abstract class Test {",
1103+
" abstract Integer property();",
1104+
" abstract List<String> listProperty();",
1105+
"",
1106+
" @AutoValue.Builder",
1107+
" public interface Builder {",
1108+
" Builder property(Integer property);",
1109+
" Builder listProperty(List<String> listProperty);",
1110+
" Builder listProperty(Integer listPropertyValues);",
1111+
" Test build();",
1112+
" }",
1113+
"}");
1114+
// We don't actually expect the extension to be invoked. Previously it was, and that led to a
1115+
// NullPointerException when calling .setters() in the checker.
1116+
ContextChecker checker =
1117+
context -> {
1118+
assertThat(context.builder()).isPresent();
1119+
assertThat(context.builder().get().setters()).isEmpty();
1120+
};
1121+
ContextCheckingExtension extension = new ContextCheckingExtension(checker);
1122+
assertThat(autoValueClass)
1123+
.processedWith(new AutoValueProcessor(ImmutableList.of(extension)))
1124+
.failsToCompile()
1125+
.withErrorContaining("Parameter type java.lang.Integer of setter method")
1126+
.in(autoValueClass)
1127+
.onLine(14);
1128+
}
1129+
10921130
private interface ContextChecker extends Consumer<AutoValueExtension.Context> {}
10931131

10941132
private static class ContextCheckingExtension extends AutoValueExtension {

0 commit comments

Comments
 (0)