public class AnnotationLocationCheck extends AbstractCheck
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:
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
...
}
AutomaticBean.OutputStreamOptions
Modifier and Type | Field and 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 and Description |
---|
AnnotationLocationCheck() |
Modifier and Type | Method and 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)
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.
|
beginTree, clearMessages, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setClassLoader, setFileContents, setTabWidth, setTokens
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
configure, contextualize, getConfiguration, setupChild
public static final String MSG_KEY_ANNOTATION_LOCATION_ALONE
public static final String MSG_KEY_ANNOTATION_LOCATION
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()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processCopyright © 2001–2018. All rights reserved.