public class AnnotationOnSameLineCheck extends AbstractCheck
Checks that annotations are located on the same line with their targets. Verifying with this check is not good practice, but it is using by some style guides.
tokens
- tokens to check
Type is int[]
.
Default value is:
CLASS_DEF,
INTERFACE_DEF,
ENUM_DEF,
METHOD_DEF,
CTOR_DEF,
VARIABLE_DEF.
To configure the check:
<module name="AnnotationOnSameLine"/>
Example:
class Foo { @SuppressWarnings("deprecation") // violation, annotation should be on the same line public Foo() { } @SuppressWarnings("unchecked") public void fun2() { // OK } } @SuppressWarnings("unchecked") class Bar extends Foo { // OK @Deprecated public Bar() { // OK } @Override // violation, annotation should be on the same line public void fun1() { } @Before @Override public void fun2() { // OK } @SuppressWarnings("deprecation") // violation, annotation should be on the same line @Before public void fun3() { } }
To configure the check to check for annotations applied on interfaces, variables and constructors:
<module name="AnnotationOnSameLine"> <property name="tokens" value="INTERFACE_DEF, VARIABLE_DEF, CTOR_DEF"/> </module>
Example:
@Deprecated interface Foo { // OK void doSomething(); } class Bar implements Foo { @SuppressWarnings("deprecation") // violation, annotation should be on the same line public Bar() { } @Override // OK public void doSomething() { } @Nullable // violation, annotation should be on the same line String s; }
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
annotation.same.line
AutomaticBean.OutputStreamOptions
Modifier and Type | Field and Description |
---|---|
static String |
MSG_KEY_ANNOTATION_ON_SAME_LINE
A key is pointing to the warning message text in "messages.properties" file.
|
Constructor and Description |
---|
AnnotationOnSameLineCheck() |
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 |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
configure, contextualize, getConfiguration, setupChild
public static final String MSG_KEY_ANNOTATION_ON_SAME_LINE
public AnnotationOnSameLineCheck()
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–2020. All rights reserved.