Class AnnotationLocationCheck
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.AnnotationLocationCheck
- All Implemented Interfaces:
Configurable
,Contextualizable
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 isboolean
. Default value isfalse
. -
Property
allowSamelineParameterizedAnnotation
- Allow one and only parameterized annotation to be located on the same line as target element. Type isboolean
. Default value isfalse
. -
Property
allowSamelineSingleParameterlessAnnotation
- Allow single parameterless annotation to be located on the same line as target element. Type isboolean
. Default value istrue
. -
Property
tokens
- tokens to check Type isjava.lang.String[]
. Validation type istokenSet
. 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
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
Field Summary
Modifier and TypeFieldDescriptionprivate boolean
Allow annotation(s) to be located on the same line as target element.private boolean
Allow one and only parameterized annotation to be located on the same line as target element.private boolean
Allow single parameterless annotation to be located on the same line as target element.static final String
A key is pointing to the warning message text in "messages.properties" file.static final String
A key is pointing to the warning message text in "messages.properties" file. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprivate 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.int[]
The configurable token set.private static String
getAnnotationName
(DetailAST annotation) Returns the name of the given annotation.int[]
Returns the default token a check is interested in.private static int
Returns an expected annotation indentation.int[]
The tokens that this check must be registered for.private static boolean
hasNodeAfter
(DetailAST annotation) Checks whether an annotation node has any node after on the same line.private static boolean
hasNodeBefore
(DetailAST annotation) Checks whether an annotation node has any node before on the same line.private static boolean
hasNodeBeside
(DetailAST annotation) Checks whether an annotation node has any node before or after on the same line.private boolean
isCorrectLocation
(DetailAST annotation, boolean hasParams) Checks whether an annotation has a correct location.private static boolean
isParameterized
(DetailAST annotation) Checks whether an annotation has parameters.final void
setAllowSamelineMultipleAnnotations
(boolean allow) Setter to allow annotation(s) to be located on the same line as target element.final void
setAllowSamelineParameterizedAnnotation
(boolean allow) Setter to allow one and only parameterized annotation to be located on the same line as target element.final void
setAllowSamelineSingleParameterlessAnnotation
(boolean allow) Setter to allow single parameterless annotation to be located on the same line as target element.void
visitToken
(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, setTokens
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY_ANNOTATION_LOCATION_ALONE
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_KEY_ANNOTATION_LOCATION
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
allowSamelineSingleParameterlessAnnotation
Allow single parameterless annotation to be located on the same line as target element. -
allowSamelineParameterizedAnnotation
Allow one and only parameterized annotation to be located on the same line as target element. -
allowSamelineMultipleAnnotations
Allow annotation(s) to be located on the same line as target element.
-
-
Constructor Details
-
AnnotationLocationCheck
public AnnotationLocationCheck()
-
-
Method Details
-
setAllowSamelineSingleParameterlessAnnotation
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
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
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
-
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 classAbstractCheck
- Returns:
- the default tokens
- See Also:
-
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 classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
-
visitToken
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
getExpectedAnnotationIndentation
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
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
Checks whether an annotation has parameters.- Parameters:
annotation
- annotation node.- Returns:
- true if the annotation has parameters.
-
getAnnotationName
Returns the name of the given annotation.- Parameters:
annotation
- annotation node.- Returns:
- annotation name.
-
isCorrectLocation
Checks whether an annotation has a correct location. Annotation location is considered correct ifallowSamelineMultipleAnnotations
is set to true. The method also: 1) checks parameterized annotation location considering the value ofallowSamelineParameterizedAnnotation
; 2) checks parameterless annotation location considering the value ofallowSamelineSingleParameterlessAnnotation
; 3) checks annotation location;- Parameters:
annotation
- annotation node.hasParams
- whether an annotation has parameters.- Returns:
- true if the annotation has a correct location.
-
hasNodeBefore
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
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
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.
-