Class OpenjdkAnnotationLocationCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.annotation.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
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newOpenjdkAnnotationLocationCheckinstance. -
Method Summary
Modifier and TypeMethodDescriptionprivate static booleanareAllOnSameLine(List<DetailAST> annotationList) Checks whether all annotations are on the same line.private static booleanareAllOnSeparateLines(List<DetailAST> annotationList) Checks whether all annotations are on a separate line.int[]The configurable token set.getAnnotations(DetailAST annotationParentNode) Gets all annotations of a target node.private static DetailASTGets the parent node of annotations.int[]Returns the default token a check is interested in.int[]The tokens that this check must be registered for.private static DetailASTgetStartingAst(DetailAST targetNode) Finds the first node other than the annotation node in target ast.private static StringgetTargetName(DetailAST targetNode) Returns the name of the given target node.private static booleanisAnyOnTargetLine(Iterable<DetailAST> annotationList, DetailAST startOfTargetNode) Checks whether an annotation is on the target line.private static booleanisLocalVariable(DetailAST ast) Checks whether the variable is local or not.private static booleanisSingleLineMethodOrField(DetailAST targetNode) Checks whether a method or field is single line or not.voidvisitToken(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY_ANNOTATION_ALONE_OR_SAME
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_KEY_ANNOTATION_ON_TARGET_LINE
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
-
Constructor Details
-
OpenjdkAnnotationLocationCheck
public OpenjdkAnnotationLocationCheck()Creates a newOpenjdkAnnotationLocationCheckinstance.
-
-
Method Details
-
getDefaultTokens
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheckThe 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:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
isLocalVariable
Checks whether the variable is local or not.- Parameters:
ast- variable.- Returns:
- true if local variable.
-
getStartingAst
Finds the first node other than the annotation node in target ast.- Parameters:
targetNode- target node.- Returns:
- the ast of the starting point
-
getAnnotationsNode
Gets the parent node of annotations.- Parameters:
ast- token.- Returns:
- the parent of annotations.
-
getAnnotations
Gets all annotations of a target node.- Parameters:
annotationParentNode- parent node of annotations.- Returns:
- the list of annotations.
-
areAllOnSameLine
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
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
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
Returns the name of the given target node.- Parameters:
targetNode- target node.- Returns:
- target name.
-