Search in sources :

Example 16 with AnnotationTree

use of com.sun.source.tree.AnnotationTree in project error-prone by google.

the class NullableVoid method matchMethod.

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
    MethodSymbol sym = ASTHelpers.getSymbol(tree);
    if (sym == null) {
        return NO_MATCH;
    }
    if (sym.getReturnType().getKind() != TypeKind.VOID) {
        return NO_MATCH;
    }
    AnnotationTree annotation = ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), "Nullable");
    if (annotation == null) {
        return NO_MATCH;
    }
    return describeMatch(annotation, SuggestedFix.delete(annotation));
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) AnnotationTree(com.sun.source.tree.AnnotationTree)

Example 17 with AnnotationTree

use of com.sun.source.tree.AnnotationTree in project error-prone by google.

the class ImmutableAnnotationChecker method matchClass.

@Override
public Description matchClass(ClassTree tree, VisitorState state) {
    ClassSymbol symbol = getSymbol(tree);
    if (symbol == null || symbol.isAnnotationType() || !WellKnownMutability.isAnnotation(state, symbol.type)) {
        return NO_MATCH;
    }
    if (ASTHelpers.hasAnnotation(symbol, Immutable.class, state)) {
        AnnotationTree annotation = ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), "Immutable");
        if (annotation != null) {
            state.reportMatch(buildDescription(annotation).setMessage(ANNOTATED_ANNOTATION_MESSAGE).addFix(SuggestedFix.delete(annotation)).build());
        } else {
            state.reportMatch(buildDescription(tree).setMessage(ANNOTATED_ANNOTATION_MESSAGE).build());
        }
    }
    Violation info = new ImmutableAnalysis(this, state, "annotations should be immutable, and cannot have non-final fields", "annotations should be immutable").checkForImmutability(Optional.of(tree), ImmutableSet.of(), getType(tree));
    if (!info.isPresent()) {
        return NO_MATCH;
    }
    String message = "annotations should be immutable: " + info.message();
    return buildDescription(tree).setMessage(message).build();
}
Also used : Violation(com.google.errorprone.bugpatterns.threadsafety.ImmutableAnalysis.Violation) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) AnnotationTree(com.sun.source.tree.AnnotationTree)

Aggregations

AnnotationTree (com.sun.source.tree.AnnotationTree)17 Tree (com.sun.source.tree.Tree)6 ModifiersTree (com.sun.source.tree.ModifiersTree)4 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)3 ClassTree (com.sun.source.tree.ClassTree)3 MethodTree (com.sun.source.tree.MethodTree)3 Modifier (javax.lang.model.element.Modifier)3 ImmutableList (com.google.common.collect.ImmutableList)2 Violation (com.google.errorprone.bugpatterns.threadsafety.ImmutableAnalysis.Violation)2 ExpressionTree (com.sun.source.tree.ExpressionTree)2 VariableTree (com.sun.source.tree.VariableTree)2 TreePath (com.sun.source.util.TreePath)2 Symbol (com.sun.tools.javac.code.Symbol)2 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)2 JCTree (com.sun.tools.javac.tree.JCTree)2 Target (java.lang.annotation.Target)2 VisitorState (com.google.errorprone.VisitorState)1 Immutable (com.google.errorprone.annotations.Immutable)1 RequiredModifiers (com.google.errorprone.annotations.RequiredModifiers)1 Nullness (com.google.errorprone.dataflow.nullnesspropagation.Nullness)1