Class AnnotationLocationCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class AnnotationLocationCheck
    extends AbstractCheck

    Checks location of annotation on language elements. By default, Check enforce to locate annotations immediately after documentation block and before target element, annotation should be located on separate line from target element. This check also verifies that the annotations are on the same indenting level as the annotated element if they are not on the same line.

    Attention: Elements that cannot have JavaDoc comments like local variables are not in the scope of this check even though a token type like VARIABLE_DEF would match them.

    Attention: Annotations among modifiers are ignored (looks like false-negative) as there might be a problem with annotations for return types:

     public @Nullable Long getStartTimeOrNull() { ... }
     

    Such annotations are better to keep close to type. Due to limitations, Checkstyle can not examine the target of an annotation.

    Example:

     @Override
     @Nullable
     public String getNameIfPresent() { ... }
     
    • Property allowSamelineMultipleAnnotations - Allow annotation(s) to be located on the same line as target element. Type is boolean. Default value is false.
    • Property allowSamelineParameterizedAnnotation - Allow one and only parameterized annotation to be located on the same line as target element. Type is boolean. Default value is false.
    • Property allowSamelineSingleParameterlessAnnotation - Allow single parameterless annotation to be located on the same line as target element. Type is boolean. Default value is true.
    • Property tokens - tokens to check Type is java.lang.String[]. Validation type is tokenSet. Default value is: CLASS_DEF, INTERFACE_DEF, PACKAGE_DEF, ENUM_CONSTANT_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF, VARIABLE_DEF, RECORD_DEF, COMPACT_CTOR_DEF.

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation Message Keys:

    • annotation.location
    • annotation.location.alone
    Since:
    6.0
    • Method Detail

      • setAllowSamelineSingleParameterlessAnnotation

        public final void setAllowSamelineSingleParameterlessAnnotation​(boolean allow)
        Setter to allow single parameterless annotation to be located on the same line as target element.
        Parameters:
        allow - User's value of allowSamelineSingleParameterlessAnnotation.
        Since:
        6.1
      • setAllowSamelineParameterizedAnnotation

        public final void setAllowSamelineParameterizedAnnotation​(boolean allow)
        Setter to allow one and only parameterized annotation to be located on the same line as target element.
        Parameters:
        allow - User's value of allowSamelineParameterizedAnnotation.
        Since:
        6.4
      • setAllowSamelineMultipleAnnotations

        public final void setAllowSamelineMultipleAnnotations​(boolean allow)
        Setter to allow annotation(s) to be located on the same line as target element.
        Parameters:
        allow - User's value of allowSamelineMultipleAnnotations.
        Since:
        6.0
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Description copied from class: AbstractCheck
        The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.
        Specified by:
        getAcceptableTokens in class AbstractCheck
        Returns:
        the token set this check is designed for.
        See Also:
        TokenTypes
      • getExpectedAnnotationIndentation

        private static int getExpectedAnnotationIndentation​(DetailAST node)
        Returns an expected annotation indentation. The expected indentation should be the same as the indentation of the target node.
        Parameters:
        node - modifiers or annotations node.
        Returns:
        the annotation indentation.
      • checkAnnotations

        private void checkAnnotations​(DetailAST modifierNode,
                                      int correctIndentation)
        Checks annotations positions in code: 1) Checks whether the annotations locations are correct. 2) Checks whether the annotations have the valid indentation level.
        Parameters:
        modifierNode - modifiers node.
        correctIndentation - correct indentation of the annotation.
      • isParameterized

        private static boolean isParameterized​(DetailAST annotation)
        Checks whether an annotation has parameters.
        Parameters:
        annotation - annotation node.
        Returns:
        true if the annotation has parameters.
      • getAnnotationName

        private static String getAnnotationName​(DetailAST annotation)
        Returns the name of the given annotation.
        Parameters:
        annotation - annotation node.
        Returns:
        annotation name.
      • hasNodeBefore

        private static boolean hasNodeBefore​(DetailAST annotation)
        Checks whether an annotation node has any node before on the same line.
        Parameters:
        annotation - annotation node.
        Returns:
        true if an annotation node has any node before on the same line.
      • hasNodeBeside

        private static boolean hasNodeBeside​(DetailAST annotation)
        Checks whether an annotation node has any node before or after on the same line.
        Parameters:
        annotation - annotation node.
        Returns:
        true if an annotation node has any node before or after on the same line.
      • hasNodeAfter

        private static boolean hasNodeAfter​(DetailAST annotation)
        Checks whether an annotation node has any node after on the same line.
        Parameters:
        annotation - annotation node.
        Returns:
        true if an annotation node has any node after on the same line.