Class OpenjdkAnnotationLocationCheck

All Implemented Interfaces:
Configurable, Contextualizable

Verifies that annotations are properly placed by OpenJDK Style. Declaration annotations must either reside entirely on a single line or have each annotation placed on its own separate line. Annotations should not share a line with the target declaration, except for single-line methods and fields.

Attention: Checkstyle ignores annotations placed among modifiers due to a technical limitation. The parser cannot distinguish whether an annotation applies to the method itself or to its return type.

Since:
13.9.0
  • Field Details

  • Constructor Details

  • Method Details

    • getDefaultTokens

      public int[] getDefaultTokens()
      Description copied from class: AbstractCheck
      Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.
      Specified by:
      getDefaultTokens in class AbstractCheck
      Returns:
      the default tokens
      See Also:
    • 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:
    • getRequiredTokens

      public int[] getRequiredTokens()
      Description copied from class: AbstractCheck
      The tokens that this check must be registered for.
      Specified by:
      getRequiredTokens in class AbstractCheck
      Returns:
      the token set this must be registered for.
      See Also:
    • visitToken

      public void visitToken(DetailAST ast)
      Description copied from class: AbstractCheck
      Called to process a token.
      Overrides:
      visitToken in class AbstractCheck
      Parameters:
      ast - the token to process
    • isLocalVariable

      private static boolean isLocalVariable(DetailAST ast)
      Checks whether the variable is local or not.
      Parameters:
      ast - variable.
      Returns:
      true if local variable.
    • getStartingAst

      private static DetailAST getStartingAst(DetailAST targetNode)
      Finds the first node other than the annotation node in target ast.
      Parameters:
      targetNode - target node.
      Returns:
      the ast of the starting point
    • getAnnotationsNode

      private static DetailAST getAnnotationsNode(DetailAST ast)
      Gets the parent node of annotations.
      Parameters:
      ast - token.
      Returns:
      the parent of annotations.
    • getAnnotations

      private static List<DetailAST> getAnnotations(DetailAST annotationParentNode)
      Gets all annotations of a target node.
      Parameters:
      annotationParentNode - parent node of annotations.
      Returns:
      the list of annotations.
    • areAllOnSameLine

      private static boolean areAllOnSameLine(List<DetailAST> annotationList)
      Checks whether all annotations are on the same line.
      Parameters:
      annotationList - list of annotations.
      Returns:
      true if all annotations are on the same line.
    • areAllOnSeparateLines

      private static boolean areAllOnSeparateLines(List<DetailAST> annotationList)
      Checks whether all annotations are on a separate line.
      Parameters:
      annotationList - list of annotations.
      Returns:
      true if all annotations are on separate lines.
    • isAnyOnTargetLine

      private static boolean isAnyOnTargetLine(Iterable<DetailAST> annotationList, DetailAST startOfTargetNode)
      Checks whether an annotation is on the target line.
      Parameters:
      annotationList - list of annotations.
      startOfTargetNode - ast of starting point of target node.
      Returns:
      true if an annotation is on the target line.
    • isSingleLineMethodOrField

      private static boolean isSingleLineMethodOrField(DetailAST targetNode)
      Checks whether a method or field is single line or not.
      Parameters:
      targetNode - ast of target node
      Returns:
      true if the method or field is single line.
    • getTargetName

      private static String getTargetName(DetailAST targetNode)
      Returns the name of the given target node.
      Parameters:
      targetNode - target node.
      Returns:
      target name.