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
Check 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.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() { ... }
The check has the following options:
- allowSamelineMultipleAnnotations - to allow annotation to be located on the same line as the target element. Default value is false.
- allowSamelineSingleParameterlessAnnotation - to allow single parameterless annotation to be located on the same line as the target element. Default value is false.
- allowSamelineParameterizedAnnotation - to allow parameterized annotation to be located on the same line as the target element. Default value is false.
Example to allow single parameterless annotation on the same line:
@Override public int hashCode() { ... }
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 multiple parameterized annotations on the same line:
@SuppressWarnings("deprecation") @Mock DataLoader loader;
Use the following configuration:
<module name="AnnotationLocation"> <property name="allowSamelineMultipleAnnotations" value="true"/> <property name="allowSamelineSingleParameterlessAnnotation" value="true"/> <property name="allowSamelineParameterizedAnnotation" value="true" /> </module>
Example to allow multiple parameterless annotations on the same line:
@Partial @Mock DataLoader loader;
Use the following configuration:
<module name="AnnotationLocation"> <property name="allowSamelineMultipleAnnotations" value="true"/> <property name="allowSamelineSingleParameterlessAnnotation" value="true"/> <property name="allowSamelineParameterizedAnnotation" value="false" /> </module>
The following example demonstrates how the check validates annotation of method parameters, catch parameters, foreach, for-loop variable definitions.
Configuration:
<module name="AnnotationLocation"> <property name="allowSamelineMultipleAnnotations" value="false"/> <property name="allowSamelineSingleParameterlessAnnotation" value="false"/> <property name="allowSamelineParameterizedAnnotation" value="false" /> <property name="tokens" value="VARIABLE_DEF, PARAMETER_DEF"/> </module>
Code example
... public void test(@MyAnnotation String s) { // OK ... for (@MyAnnotation char c : s.toCharArray()) { ... } // OK ... try { ... } catch (@MyAnnotation Exception ex) { ... } // OK ... for (@MyAnnotation int i = 0; i < 10; i++) { ... } // OK ... MathOperation c = (@MyAnnotation int a, @MyAnnotation int b) -> a + b; // OK ... }
- Author:
- maxvetrenko
-
-
Field Summary
Fields Modifier and Type Field Description static String
MSG_KEY_ANNOTATION_LOCATION
A key is pointing to the warning message text in "messages.properties" file.static 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 Static 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.static boolean
isAllowedPosition(DetailAST annotation, int... allowedPositions)
Checks whether position of annotation is allowed.void
setAllowSamelineMultipleAnnotations(boolean allow)
Sets if allow annotation to be located on the same line as target element.void
setAllowSamelineParameterizedAnnotation(boolean allow)
Sets if allow parameterized annotation to be located on the same line as target element.void
setAllowSamelineSingleParameterlessAnnotation(boolean allow)
Sets if allow same line single parameterless annotation.void
visitToken(DetailAST ast)
Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY_ANNOTATION_LOCATION_ALONE
public static final 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 String MSG_KEY_ANNOTATION_LOCATION
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
AnnotationLocationCheck
public AnnotationLocationCheck()
-
-
Method Detail
-
setAllowSamelineSingleParameterlessAnnotation
public final void setAllowSamelineSingleParameterlessAnnotation(boolean allow)
Sets if allow same line single parameterless annotation.- Parameters:
allow
- User's value of allowSamelineSingleParameterlessAnnotation.
-
setAllowSamelineParameterizedAnnotation
public final void setAllowSamelineParameterizedAnnotation(boolean allow)
Sets if allow 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)
Sets if allow annotation 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
-
isAllowedPosition
public static boolean isAllowedPosition(DetailAST annotation, int... allowedPositions)
Checks whether position of annotation is allowed.- Parameters:
annotation
- annotation token.allowedPositions
- an array of allowed annotation positions.- Returns:
- true if position of annotation is allowed.
-
-