Search in sources :

Example 1 with Immutable

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();
}
Also used : Immutable(com.google.errorprone.annotations.Immutable) Violation(com.google.errorprone.bugpatterns.threadsafety.ImmutableAnalysis.Violation) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) AnnotationTree(com.sun.source.tree.AnnotationTree)

Example 2 with Immutable

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()));
}
Also used : Immutable(com.google.errorprone.annotations.Immutable)

Aggregations

Immutable (com.google.errorprone.annotations.Immutable)2 Violation (com.google.errorprone.bugpatterns.threadsafety.ImmutableAnalysis.Violation)1 AnnotationTree (com.sun.source.tree.AnnotationTree)1 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)1