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() { ... }
allowSamelineMultipleAnnotations - Allow annotation(s) to be located on
the same line as target element.
Type is boolean.
Default value is false.
allowSamelineSingleParameterlessAnnotation - Allow single parameterless
annotation to be located on the same line as target element.
Type is boolean.
Default value is true.
allowSamelineParameterizedAnnotation - Allow one and only parameterized
annotation to be located on the same line as target element.
Type is boolean.
Default value is false.
tokens - tokens to check
Type is java.lang.String[].
Validation type is tokenSet.
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.
To configure the default check to allow one single parameterless annotation on the same line:
<module name="AnnotationLocation"/>
Example for above configuration:
@NotNull private boolean field1; //ok
@Override public int hashCode() { return 1; } //ok
@NotNull //ok
private boolean field2;
@Override //ok
public boolean equals(Object obj) { return true; }
@Mock DataLoader loader; //ok
@SuppressWarnings("deprecation") DataLoader loader; //violation
@SuppressWarnings("deprecation") public int foo() { return 1; } //violation
@NotNull @Mock DataLoader loader; //violation
Use the following configuration to allow multiple annotations on the same line:
<module name="AnnotationLocation">
<property name="allowSamelineMultipleAnnotations" value="true"/>
<property name="allowSamelineSingleParameterlessAnnotation"
value="false"/>
<property name="allowSamelineParameterizedAnnotation" value="false"/>
</module>
Example to allow any location multiple annotations:
@NotNull private boolean field1; //ok
@Override public int hashCode() { return 1; } //ok
@NotNull //ok
private boolean field2;
@Override //ok
public boolean equals(Object obj) { return true; }
@Mock DataLoader loader; //ok
@SuppressWarnings("deprecation") DataLoader loader; //ok
@SuppressWarnings("deprecation") public int foo() { return 1; } //ok
@NotNull @Mock DataLoader loader; //ok
Use the following configuration to allow only one and only parameterized annotation on the same line:
<module name="AnnotationLocation">
<property name="allowSamelineMultipleAnnotations" value="false"/>
<property name="allowSamelineSingleParameterlessAnnotation"
value="false"/>
<property name="allowSamelineParameterizedAnnotation" value="true"/>
</module>
Example to allow only one and only parameterized annotation on the same line:
@NotNull private boolean field1; //violation
@Override public int hashCode() { return 1; } //violation
@NotNull //ok
private boolean field2;
@Override //ok
public boolean equals(Object obj) { return true; }
@Mock DataLoader loader; //violation
@SuppressWarnings("deprecation") DataLoader loader; //ok
@SuppressWarnings("deprecation") public int foo() { return 1; } //ok
@NotNull @Mock DataLoader loader; //violation
Use the following configuration to only validate annotations on methods to allow one single parameterless annotation on the same line:
<module name="AnnotationLocation">
<property name="tokens" value="METHOD_DEF"/>
<property name="allowSamelineMultipleAnnotations" value="false"/>
<property name="allowSamelineSingleParameterlessAnnotation"
value="true"/>
<property name="allowSamelineParameterizedAnnotation" value="false"/>
</module>
Example for above configuration to check only methods:
@NotNull private boolean field1; //ok
@Override public int hashCode() { return 1; } //ok
@NotNull //ok
private boolean field2;
@Override //ok
public boolean equals(Object obj) { return true; }
@Mock DataLoader loader; //ok
@SuppressWarnings("deprecation") DataLoader loader; //ok
@SuppressWarnings("deprecation") public int foo() { return 1; } //violation
@NotNull @Mock DataLoader loader; //ok
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
annotation.location
annotation.location.alone
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
private boolean |
allowSamelineMultipleAnnotations
Allow annotation(s) to be located on the same line as
target element.
|
private boolean |
allowSamelineParameterizedAnnotation
Allow one and only parameterized annotation to be located on the same line as
target element.
|
private boolean |
allowSamelineSingleParameterlessAnnotation
Allow single parameterless annotation to be located on the same line as
target element.
|
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 and Description |
|---|
AnnotationLocationCheck() |
| Modifier and Type | Method and Description |
|---|---|
private void |
checkAnnotations(DetailAST modifierNode,
int correctIndentation)
Checks annotations positions in code:
1) Checks whether the annotations locations are correct.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
private static java.lang.String |
getAnnotationName(DetailAST annotation)
Returns the name of the given annotation.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
private static int |
getExpectedAnnotationIndentation(DetailAST node)
Returns an expected annotation indentation.
|
int[] |
getRequiredTokens()
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.
|
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.
|
beginTree, clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY_ANNOTATION_LOCATION_ALONE
public static final java.lang.String MSG_KEY_ANNOTATION_LOCATION
private boolean allowSamelineSingleParameterlessAnnotation
private boolean allowSamelineParameterizedAnnotation
private boolean allowSamelineMultipleAnnotations
public AnnotationLocationCheck()
public final void setAllowSamelineSingleParameterlessAnnotation(boolean allow)
allow - User's value of allowSamelineSingleParameterlessAnnotation.public final void setAllowSamelineParameterizedAnnotation(boolean allow)
allow - User's value of allowSamelineParameterizedAnnotation.public final void setAllowSamelineMultipleAnnotations(boolean allow)
allow - User's value of allowSamelineMultipleAnnotations.public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processprivate static int getExpectedAnnotationIndentation(DetailAST node)
node - modifiers or annotations node.private void checkAnnotations(DetailAST modifierNode, int correctIndentation)
modifierNode - modifiers node.correctIndentation - correct indentation of the annotation.private static boolean isParameterized(DetailAST annotation)
annotation - annotation node.private static java.lang.String getAnnotationName(DetailAST annotation)
annotation - annotation node.private boolean isCorrectLocation(DetailAST annotation, boolean hasParams)
allowSamelineMultipleAnnotations is set to true.
The method also:
1) checks parameterized annotation location considering
the value of allowSamelineParameterizedAnnotation;
2) checks parameterless annotation location considering
the value of allowSamelineSingleParameterlessAnnotation;
3) checks annotation location;annotation - annotation node.hasParams - whether an annotation has parameters.private static boolean hasNodeBefore(DetailAST annotation)
annotation - annotation node.private static boolean hasNodeBeside(DetailAST annotation)
annotation - annotation node.private static boolean hasNodeAfter(DetailAST annotation)
annotation - annotation node.Copyright © 2001-2022. All Rights Reserved.