Search in sources :

Example 1 with RequiredModifiers

use of com.google.errorprone.annotations.RequiredModifiers in project error-prone by google.

the class RequiredModifiersChecker method matchAnnotation.

@Override
public Description matchAnnotation(AnnotationTree tree, VisitorState state) {
    RequiredModifiers annotation = ASTHelpers.getAnnotation(tree, RequiredModifiers.class);
    if (annotation == null) {
        return Description.NO_MATCH;
    }
    Set<Modifier> requiredModifiers = ImmutableSet.copyOf(annotation.value());
    if (requiredModifiers.isEmpty()) {
        return Description.NO_MATCH;
    }
    Tree parent = state.getPath().getParentPath().getLeaf();
    if (!(parent instanceof ModifiersTree)) {
        // e.g. An annotated package name
        return Description.NO_MATCH;
    }
    Set<Modifier> missing = Sets.difference(requiredModifiers, ((ModifiersTree) parent).getFlags());
    if (missing.isEmpty()) {
        return Description.NO_MATCH;
    }
    String annotationName = ASTHelpers.getAnnotationName(tree);
    String nameString = annotationName != null ? String.format("The annotation '@%s'", annotationName) : "This annotation";
    String customMessage = String.format(MESSAGE_TEMPLATE, nameString, missing.toString());
    return buildDescription(tree).setMessage(customMessage).build();
}
Also used : ModifiersTree(com.sun.source.tree.ModifiersTree) AnnotationTree(com.sun.source.tree.AnnotationTree) ModifiersTree(com.sun.source.tree.ModifiersTree) Tree(com.sun.source.tree.Tree) Modifier(javax.lang.model.element.Modifier) RequiredModifiers(com.google.errorprone.annotations.RequiredModifiers)

Aggregations

RequiredModifiers (com.google.errorprone.annotations.RequiredModifiers)1 AnnotationTree (com.sun.source.tree.AnnotationTree)1 ModifiersTree (com.sun.source.tree.ModifiersTree)1 Tree (com.sun.source.tree.Tree)1 Modifier (javax.lang.model.element.Modifier)1