use of com.google.errorprone.annotations.Immutable in project error-prone by google.
the class ImmutableEnumChecker method matchClass.
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
ClassSymbol symbol = getSymbol(tree);
if (symbol == null || !symbol.isEnum()) {
return NO_MATCH;
}
if (ASTHelpers.hasAnnotation(symbol, Immutable.class, state) && !implementsImmutableInterface(symbol)) {
AnnotationTree annotation = ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), "Immutable");
if (annotation != null) {
state.reportMatch(buildDescription(annotation).setMessage(ANNOTATED_ENUM_MESSAGE).addFix(SuggestedFix.delete(annotation)).build());
} else {
state.reportMatch(buildDescription(tree).setMessage(ANNOTATED_ENUM_MESSAGE).build());
}
}
Violation info = new ImmutableAnalysis(this, state, "enums should be immutable, and cannot have non-final fields", "enums should only have immutable fields").checkForImmutability(Optional.of(tree), ImmutableSet.of(), getType(tree));
if (!info.isPresent()) {
return NO_MATCH;
}
String message = "enums should be immutable: " + info.message();
return buildDescription(tree).setMessage(message).build();
}
use of com.google.errorprone.annotations.Immutable in project error-prone by google.
the class ImmutableAnalysis method getImmutableAnnotation.
/**
* Gets the {@link Symbol}'s {@code @Immutable} annotation info, either from an annotation on the
* symbol or from the list of well-known immutable types.
*/
static ImmutableAnnotationInfo getImmutableAnnotation(Symbol sym) {
String nameStr = sym.flatName().toString();
ImmutableAnnotationInfo known = WellKnownMutability.KNOWN_IMMUTABLE.get(nameStr);
if (known != null) {
return known;
}
Immutable immutable = ASTHelpers.getAnnotation(sym, Immutable.class);
if (immutable == null) {
return null;
}
return ImmutableAnnotationInfo.create(sym.getQualifiedName().toString(), ImmutableList.copyOf(immutable.containerOf()));
}
Aggregations