Class AnnotationLocationCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.annotation.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. Default value isfalse
. -
Property
allowSamelineSingleParameterlessAnnotation
- Allow single parameterless annotation to be located on the same line as target element. Default value istrue
. -
Property
allowSamelineParameterizedAnnotation
- Allow one and only parameterized annotation to be located on the same line as target element. Default value isfalse
. -
Property
tokens
- tokens to check Default value is: CLASS_DEF, INTERFACE_DEF, PACKAGE_DEF, ENUM_CONSTANT_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF, VARIABLE_DEF.
Example to allow multiple annotations on the same line
@SuppressWarnings("deprecation") @Mock DataLoader loader; // no violations
Use the following configuration:
<module name="AnnotationLocation"> <property name="allowSamelineMultipleAnnotations" value="true"/> <property name="allowSamelineSingleParameterlessAnnotation" value="false"/> <property name="allowSamelineParameterizedAnnotation" value="false"/> </module>
Example to allow one single parameterless annotation on the same line
@Override public int hashCode() { ... } // no violations @SuppressWarnings("deprecation") public int foo() { ... } // violation
Use the following configuration:
<module name="AnnotationLocation"> <property name="allowSamelineMultipleAnnotations" value="false"/> <property name="allowSamelineSingleParameterlessAnnotation" value="true"/> <property name="allowSamelineParameterizedAnnotation" value="false"/> </module>
Example to allow only one and only parameterized annotation on the same line
@SuppressWarnings("deprecation") DataLoader loader; // no violations @Mock DataLoader loader; // violation
Use the following configuration:
<module name="AnnotationLocation"> <property name="allowSamelineMultipleAnnotations" value="false"/> <property name="allowSamelineSingleParameterlessAnnotation" value="false"/> <property name="allowSamelineParameterizedAnnotation" value="true"/> </module>
- Since:
- 6.0
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
MSG_KEY_ANNOTATION_LOCATION
A key is pointing to the warning message text in "messages.properties" file.static java.lang.String
MSG_KEY_ANNOTATION_LOCATION_ALONE
A key is pointing to the warning message text in "messages.properties" file.
-
Constructor Summary
Constructors Constructor Description AnnotationLocationCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]
getAcceptableTokens()
The configurable token set.int[]
getDefaultTokens()
Returns the default token a check is interested in.int[]
getRequiredTokens()
The tokens that this check must be registered for.void
setAllowSamelineMultipleAnnotations(boolean allow)
Setter to allow annotation(s) to be located on the same line as target element.void
setAllowSamelineParameterizedAnnotation(boolean allow)
Setter to allow one and only parameterized annotation to be located on the same line as target element.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, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, 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.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY_ANNOTATION_LOCATION_ALONE
public static final java.lang.String MSG_KEY_ANNOTATION_LOCATION_ALONE
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_KEY_ANNOTATION_LOCATION
public static final java.lang.String MSG_KEY_ANNOTATION_LOCATION
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
-
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.
-
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.
-
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.
-
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 classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
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 classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] 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:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
-