Skip to content

Commit c6e35e6

Browse files
eamonnmcmanusnick-someone
authored andcommitted
Add [tags] to AutoValue error messages. This will enable them to be correlated so that we can potentially see which ones are commonest.
RELNOTES=AutoValue error messages now have short `[tags]` so they can be correlated by tools. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=323252839
1 parent 43ff5f2 commit c6e35e6

File tree

6 files changed

+139
-83
lines changed

6 files changed

+139
-83
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ boolean propertiesCanBeVoid() {
7272
void processType(TypeElement autoOneOfType) {
7373
if (autoOneOfType.getKind() != ElementKind.CLASS) {
7474
errorReporter()
75-
.abortWithError(autoOneOfType, "@" + AUTO_ONE_OF_NAME + " only applies to classes");
75+
.abortWithError(
76+
autoOneOfType,
77+
"[AutoOneOfNotClass] @" + AUTO_ONE_OF_NAME + " only applies to classes");
7678
}
7779
checkModifiersIfNested(autoOneOfType);
7880
DeclaredType kindMirror = mirrorForKindType(autoOneOfType);
@@ -129,7 +131,7 @@ private DeclaredType mirrorForKindType(TypeElement autoOneOfType) {
129131
errorReporter()
130132
.abortWithError(
131133
autoOneOfType,
132-
"annotation processor for @AutoOneOf was invoked with a type"
134+
"[AutoOneOfCompilerBug] annotation processor for @AutoOneOf was invoked with a type"
133135
+ " that does not have that annotation; this is probably a compiler bug");
134136
}
135137
AnnotationValue kindValue =
@@ -184,7 +186,8 @@ private ImmutableMap<String, String> propertyToKindMap(
184186
errorReporter()
185187
.reportError(
186188
kindElement,
187-
"Enum has no constant with name corresponding to property '%s'",
189+
"[AutoOneOfNoEnumConstant] Enum has no constant with name corresponding to"
190+
+ " property '%s'",
188191
property);
189192
}
190193
});
@@ -195,7 +198,8 @@ private ImmutableMap<String, String> propertyToKindMap(
195198
errorReporter()
196199
.reportError(
197200
constant,
198-
"Name of enum constant '%s' does not correspond to any property name",
201+
"[AutoOneOfBadEnumConstant] Name of enum constant '%s' does not correspond to"
202+
+ " any property name",
199203
constant.getSimpleName());
200204
}
201205
});
@@ -221,7 +225,7 @@ private ExecutableElement findKindGetterOrAbort(
221225
errorReporter()
222226
.reportError(
223227
autoOneOfType,
224-
"%s must have a no-arg abstract method returning %s",
228+
"[AutoOneOfNoKindGetter] %s must have a no-arg abstract method returning %s",
225229
autoOneOfType,
226230
kindMirror);
227231
break;
@@ -230,7 +234,10 @@ private ExecutableElement findKindGetterOrAbort(
230234
default:
231235
for (ExecutableElement getter : kindGetters) {
232236
errorReporter()
233-
.reportError(getter, "More than one abstract method returns %s", kindMirror);
237+
.reportError(
238+
getter,
239+
"[AutoOneOfTwoKindGetters] More than one abstract method returns %s",
240+
kindMirror);
234241
}
235242
}
236243
throw new AbortProcessingException();
@@ -254,7 +261,8 @@ && objectMethodToOverride(method) == ObjectMethod.NONE) {
254261
// implement this alien method.
255262
errorReporter()
256263
.reportWarning(
257-
method, "Abstract methods in @AutoOneOf classes must have no parameters");
264+
method,
265+
"[AutoOneOfParams] Abstract methods in @AutoOneOf classes must have no parameters");
258266
}
259267
}
260268
errorReporter().abortIfAnyError();
@@ -278,7 +286,9 @@ private void defineVarsForType(
278286
@Override
279287
Optional<String> nullableAnnotationForMethod(ExecutableElement propertyMethod) {
280288
if (nullableAnnotationFor(propertyMethod, propertyMethod.getReturnType()).isPresent()) {
281-
errorReporter().reportError(propertyMethod, "@AutoOneOf properties cannot be @Nullable");
289+
errorReporter()
290+
.reportError(
291+
propertyMethod, "[AutoOneOfNullable] @AutoOneOf properties cannot be @Nullable");
282292
}
283293
return Optional.empty();
284294
}

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

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
import javax.lang.model.element.TypeElement;
7272
import javax.lang.model.element.TypeParameterElement;
7373
import javax.lang.model.element.VariableElement;
74-
import javax.lang.model.type.ArrayType;
7574
import javax.lang.model.type.DeclaredType;
7675
import javax.lang.model.type.TypeKind;
7776
import javax.lang.model.type.TypeMirror;
@@ -301,7 +300,8 @@ public final boolean process(Set<? extends TypeElement> annotations, RoundEnviro
301300
for (TypeElement type : deferredTypes) {
302301
errorReporter.reportError(
303302
type,
304-
"Did not generate @%s class for %s because it references undefined types",
303+
"[AutoValueUndefined] Did not generate @%s class for %s because it references"
304+
+ " undefined types",
305305
simpleAnnotationName,
306306
type.getQualifiedName());
307307
}
@@ -331,7 +331,10 @@ public final boolean process(Set<? extends TypeElement> annotations, RoundEnviro
331331
} catch (RuntimeException e) {
332332
String trace = Throwables.getStackTraceAsString(e);
333333
errorReporter.reportError(
334-
type, "@%s processor threw an exception: %s", simpleAnnotationName, trace);
334+
type,
335+
"[AutoValueException] @%s processor threw an exception: %s",
336+
simpleAnnotationName,
337+
trace);
335338
throw e;
336339
}
337340
}
@@ -378,8 +381,9 @@ final ImmutableSet<Property> propertySet(
378381
ImmutableSet.Builder<Property> props = ImmutableSet.builder();
379382
propertyMethodsAndTypes.forEach(
380383
(propertyMethod, returnType) -> {
381-
String propertyType = TypeEncoder.encodeWithAnnotations(
382-
returnType, getExcludedAnnotationTypes(propertyMethod));
384+
String propertyType =
385+
TypeEncoder.encodeWithAnnotations(
386+
returnType, getExcludedAnnotationTypes(propertyMethod));
383387
String propertyName = methodToPropertyName.get(propertyMethod);
384388
String identifier = methodToIdentifier.get(propertyMethod);
385389
ImmutableList<String> fieldAnnotations =
@@ -399,7 +403,9 @@ final ImmutableSet<Property> propertySet(
399403
nullableAnnotation);
400404
props.add(p);
401405
if (p.isNullable() && returnType.getKind().isPrimitive()) {
402-
errorReporter().reportError(propertyMethod, "Primitive types cannot be @Nullable");
406+
errorReporter()
407+
.reportError(
408+
propertyMethod, "[AutoValueNullPrimitive] Primitive types cannot be @Nullable");
403409
}
404410
});
405411
return props.build();
@@ -517,7 +523,10 @@ final ImmutableBiMap<String, ExecutableElement> propertyNameToMethodMap(
517523
// reportedDups prevents us from reporting more than one error for the same method.
518524
for (ExecutableElement context : contexts) {
519525
errorReporter.reportError(
520-
context, "More than one @%s property called %s", simpleAnnotationName, name);
526+
context,
527+
"[AutoValueDupProperty] More than one @%s property called %s",
528+
simpleAnnotationName,
529+
name);
521530
}
522531
}
523532
}
@@ -622,16 +631,18 @@ final void checkModifiersIfNested(TypeElement type) {
622631
if (enclosingKind.isClass() || enclosingKind.isInterface()) {
623632
if (type.getModifiers().contains(Modifier.PRIVATE)) {
624633
errorReporter.abortWithError(
625-
type, "@%s class must not be private", simpleAnnotationName);
634+
type, "[AutoValuePrivate] @%s class must not be private", simpleAnnotationName);
626635
} else if (Visibility.effectiveVisibilityOfElement(type).equals(Visibility.PRIVATE)) {
627636
// The previous case, where the class itself is private, is much commoner so it deserves
628637
// its own error message, even though it would be caught by the test here too.
629638
errorReporter.abortWithError(
630-
type, "@%s class must not be nested in a private class", simpleAnnotationName);
639+
type,
640+
"[AutoValueInPrivate] @%s class must not be nested in a private class",
641+
simpleAnnotationName);
631642
}
632643
if (!type.getModifiers().contains(Modifier.STATIC)) {
633644
errorReporter.abortWithError(
634-
type, "Nested @%s class must be static", simpleAnnotationName);
645+
type, "[AutoValueInner] Nested @%s class must be static", simpleAnnotationName);
635646
}
636647
}
637648
// In principle type.getEnclosingElement() could be an ExecutableElement (for a class
@@ -766,13 +777,14 @@ boolean propertiesCanBeVoid() {
766777
final void checkReturnType(TypeElement autoValueClass, ExecutableElement getter) {
767778
TypeMirror type = getter.getReturnType();
768779
if (type.getKind() == TypeKind.ARRAY) {
769-
TypeMirror componentType = ((ArrayType) type).getComponentType();
770-
if (componentType.getKind().isPrimitive()) {
780+
TypeMirror componentType = MoreTypes.asArray(type).getComponentType();
781+
if (componentType.getKind().isPrimitive()) {
771782
warnAboutPrimitiveArrays(autoValueClass, getter);
772783
} else {
773784
errorReporter.reportError(
774785
getter,
775-
"An @%s class cannot define an array-valued property unless it is a primitive array",
786+
"[AutoValueArray] An @%s class cannot define an array-valued property unless it is a"
787+
+ " primitive array",
776788
simpleAnnotationName);
777789
}
778790
}
@@ -799,10 +811,11 @@ private void warnAboutPrimitiveArrays(TypeElement autoValueClass, ExecutableElem
799811
String context = sameClass ? "" : (" Method: " + getter.getEnclosingElement() + "." + getter);
800812
errorReporter.reportWarning(
801813
element,
802-
"An @%s property that is a primitive array returns the original array, which can"
803-
+ " therefore be modified by the caller. If this is OK, you can suppress this warning"
804-
+ " with @SuppressWarnings(\"mutable\"). Otherwise, you should replace the property"
805-
+ " with an immutable type, perhaps a simple wrapper around the original array.%s",
814+
"[AutoValueMutable] An @%s property that is a primitive array returns the original"
815+
+ " array, which can therefore be modified by the caller. If this is OK, you can"
816+
+ " suppress this warning with @SuppressWarnings(\"mutable\"). Otherwise, you should"
817+
+ " replace the property with an immutable type, perhaps a simple wrapper around the"
818+
+ " original array.%s",
806819
simpleAnnotationName,
807820
context);
808821
}
@@ -815,8 +828,8 @@ private void warnAboutPrimitiveArrays(TypeElement autoValueClass, ExecutableElem
815828
private static class ContainsMutableVisitor extends SimpleAnnotationValueVisitor8<Boolean, Void> {
816829
@Override
817830
public Boolean visitArray(List<? extends AnnotationValue> list, Void p) {
818-
return list.stream().map(av -> av.getValue()).anyMatch("mutable"::equals);
819-
}
831+
return list.stream().map(AnnotationValue::getValue).anyMatch("mutable"::equals);
832+
}
820833
}
821834

822835
/**
@@ -1102,7 +1115,10 @@ final void writeSourceFile(String className, String text, TypeElement originatin
11021115
// error later because user code will have a reference to the code we were supposed to
11031116
// generate (new AutoValue_Foo() or whatever) and that reference will be undefined.
11041117
errorReporter.reportWarning(
1105-
originatingType, "Could not write generated class %s: %s", className, e);
1118+
originatingType,
1119+
"[AutoValueCouldNotWrite] Could not write generated class %s: %s",
1120+
className,
1121+
e);
11061122
}
11071123
}
11081124
}

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
121121
errorReporter()
122122
.reportWarning(
123123
null,
124-
"An exception occurred while looking for AutoValue extensions."
125-
+ " No extensions will function.%s\n%s",
124+
"[AutoValueExtensionsException] An exception occurred while looking for AutoValue"
125+
+ " extensions. No extensions will function.%s\n%s",
126126
explain,
127127
Throwables.getStackTraceAsString(e));
128128
extensions = ImmutableList.of();
@@ -170,19 +170,22 @@ void processType(TypeElement type) {
170170
errorReporter()
171171
.abortWithError(
172172
type,
173-
"annotation processor for @AutoValue was invoked with a type"
173+
"[AutoValueCompilerBug] annotation processor for @AutoValue was invoked with a type"
174174
+ " that does not have that annotation; this is probably a compiler bug");
175175
}
176176
if (type.getKind() != ElementKind.CLASS) {
177-
errorReporter().abortWithError(type, "@AutoValue only applies to classes");
177+
errorReporter()
178+
.abortWithError(type, "[AutoValueNotClass] @AutoValue only applies to classes");
178179
}
179180
if (ancestorIsAutoValue(type)) {
180-
errorReporter().abortWithError(type, "One @AutoValue class may not extend another");
181+
errorReporter()
182+
.abortWithError(type, "[AutoValueExtend] One @AutoValue class may not extend another");
181183
}
182184
if (implementsAnnotation(type)) {
183185
errorReporter()
184186
.abortWithError(
185-
type, "@AutoValue may not be used to implement an annotation"
187+
type,
188+
"[AutoValueImplAnnotation] @AutoValue may not be used to implement an annotation"
186189
+ " interface; try using @AutoAnnotation instead");
187190
}
188191
checkModifiersIfNested(type);
@@ -333,7 +336,8 @@ private ImmutableList<AutoValueExtension> applicableExtensions(
333336
errorReporter()
334337
.reportError(
335338
type,
336-
"More than one extension wants to generate the final class: %s",
339+
"[AutoValueMultiFinal] More than one extension wants to generate the final class:"
340+
+ " %s",
337341
finalExtensions.stream().map(this::extensionName).collect(joining(", ")));
338342
break;
339343
}
@@ -355,7 +359,8 @@ private ImmutableSet<ExecutableElement> methodsConsumedByExtensions(
355359
errorReporter()
356360
.reportError(
357361
type,
358-
"Extension %s wants to consume a property that does not exist: %s",
362+
"[AutoValueConsumeNonexist] Extension %s wants to consume a property that does"
363+
+ " not exist: %s",
359364
extensionName(extension),
360365
consumedProperty);
361366
} else {
@@ -367,8 +372,8 @@ private ImmutableSet<ExecutableElement> methodsConsumedByExtensions(
367372
errorReporter()
368373
.reportError(
369374
type,
370-
"Extension %s wants to consume a method that is not one of the abstract methods"
371-
+ " in this class: %s",
375+
"[AutoValueConsumeNotAbstract] Extension %s wants to consume a method that is"
376+
+ " not one of the abstract methods in this class: %s",
372377
extensionName(extension),
373378
consumedMethod);
374379
} else {
@@ -379,8 +384,8 @@ private ImmutableSet<ExecutableElement> methodsConsumedByExtensions(
379384
errorReporter()
380385
.reportError(
381386
repeat,
382-
"Extension %s wants to consume a method that was already consumed by another"
383-
+ " extension",
387+
"[AutoValueMultiConsume] Extension %s wants to consume a method that was already"
388+
+ " consumed by another extension",
384389
extensionName(extension));
385390
}
386391
consumed.addAll(consumedHere);
@@ -404,10 +409,12 @@ && objectMethodToOverride(method) == ObjectMethod.NONE) {
404409
// another, and therefore leaves us with both an abstract method and the subclass method
405410
// that overrides it. This shows up in AutoValueTest.LukesBase for example.
406411
String extensionMessage = extensionsPresent ? ", and no extension consumed it" : "";
407-
errorReporter().reportWarning(
408-
method,
409-
"Abstract method is neither a property getter nor a Builder converter%s",
410-
extensionMessage);
412+
errorReporter()
413+
.reportWarning(
414+
method,
415+
"[AutoValueBuilderWhat] Abstract method is neither a property getter nor a Builder"
416+
+ " converter%s",
417+
extensionMessage);
411418
}
412419
}
413420
errorReporter().abortIfAnyError();

0 commit comments

Comments
 (0)