public class DeclarationOrderCheck extends AbstractCheck
Checks that the parts of a class, record, or interface declaration appear in the order suggested by the Code Conventions for the Java Programming Language.
According to Code Conventions for the Java Programming Language, the parts of a class or interface declaration should appear in the following order:
Purpose of ignore* option is to ignore related violations, however it still impacts on other class members.
ATTENTION: the check skips class fields which have forward references from validation due to the fact that we have Checkstyle's limitations to clearly detect user intention of fields location and grouping. For example:
public class A {
private double x = 1.0;
private double y = 2.0;
public double slope = x / y; // will be skipped from validation due to forward reference
}
ignoreConstructors - control whether to ignore constructors.
Type is boolean.
Default value is false.
ignoreModifiers - control whether to ignore modifiers (fields, ...).
Type is boolean.
Default value is false.
To configure the check:
<module name="DeclarationOrder"/>
Example:
public class Test {
public int a;
protected int b;
public int c; // violation, variable access definition in wrong order
Test() {
this.a = 0;
}
public void foo() {
// This method does nothing
}
Test(int a) { // violation, constructor definition in wrong order
this.a = a;
}
private String name; // violation, instance variable declaration in wrong order
}
To configure the check to ignore validation of constructors:
<module name="DeclarationOrder"> <property name="ignoreConstructors" value="true"/> </module>
Example:
public class Test {
public int a;
protected int b;
public int c; // violation, variable access definition in wrong order
Test() {
this.a = 0;
}
public void foo() {
// This method does nothing
}
Test(int a) { // OK, validation of constructors ignored
this.a = a;
}
private String name; // violation, instance variable declaration in wrong order
}
To configure the check to ignore modifiers:
<module name="DeclarationOrder"> <property name="ignoreModifiers" value="true"/> </module>
Example:
public class Test {
public int a;
protected int b;
public int c; // OK, access modifiers not considered while validating
Test() {
this.a = 0;
}
public void foo() {
// This method does nothing
}
Test(int a) { // violation, constructor definition in wrong order
this.a = a;
}
private String name; // violation, instance variable declaration in wrong order
}
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
declaration.order.access
declaration.order.constructor
declaration.order.instance
declaration.order.static
| Modifier and Type | Class and Description |
|---|---|
private static class |
DeclarationOrderCheck.ScopeState
Private class to encapsulate the state.
|
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
private java.util.Set<java.lang.String> |
classFieldNames
Set of all class field names.
|
private boolean |
ignoreConstructors
Control whether to ignore constructors.
|
private boolean |
ignoreModifiers
Control whether to ignore modifiers (fields, ...).
|
static java.lang.String |
MSG_ACCESS
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_CONSTRUCTOR
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_INSTANCE
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_STATIC
A key is pointing to the warning message text in "messages.properties"
file.
|
private java.util.Deque<DeclarationOrderCheck.ScopeState> |
scopeStates
List of Declaration States.
|
private static int |
STATE_CTOR_DEF
State for the CTOR_DEF.
|
private static int |
STATE_INSTANCE_VARIABLE_DEF
State for the VARIABLE_DEF.
|
private static int |
STATE_METHOD_DEF
State for the METHOD_DEF.
|
private static int |
STATE_STATIC_VARIABLE_DEF
State for the VARIABLE_DEF.
|
| Constructor and Description |
|---|
DeclarationOrderCheck() |
| Modifier and Type | Method and Description |
|---|---|
void |
beginTree(DetailAST rootAST)
Called before the starting to process a tree.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
private static java.util.Set<DetailAST> |
getAllTokensOfType(DetailAST ast,
int tokenType)
Collects all tokens of specific type starting with the current ast node.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
private boolean |
isForwardReference(DetailAST fieldDef)
Checks whether an identifier references a field which has been already defined in class.
|
void |
leaveToken(DetailAST ast)
Called after all the child nodes have been process.
|
private void |
processConstructor(DetailAST ast)
Processes constructor.
|
private void |
processModifiers(DetailAST ast)
Processes modifiers.
|
private boolean |
processModifiersState(DetailAST modifierAst,
DeclarationOrderCheck.ScopeState state)
Process if given modifiers are appropriate in given state
(
STATE_STATIC_VARIABLE_DEF, STATE_INSTANCE_VARIABLE_DEF,
(STATE_CTOR_DEF, STATE_METHOD_DEF), if it is
it updates states where appropriate or logs violation. |
private void |
processModifiersSubState(DetailAST modifiersAst,
DeclarationOrderCheck.ScopeState state,
boolean isStateValid)
Checks if given modifiers are valid in substate of given
state(
Scope), if it is it updates substate or else it
logs violation. |
void |
setIgnoreConstructors(boolean ignoreConstructors)
Setter to control whether to ignore constructors.
|
void |
setIgnoreModifiers(boolean ignoreModifiers)
Setter to control whether to ignore modifiers (fields, ...).
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_CONSTRUCTOR
public static final java.lang.String MSG_STATIC
public static final java.lang.String MSG_INSTANCE
public static final java.lang.String MSG_ACCESS
private static final int STATE_STATIC_VARIABLE_DEF
private static final int STATE_INSTANCE_VARIABLE_DEF
private static final int STATE_CTOR_DEF
private static final int STATE_METHOD_DEF
private java.util.Deque<DeclarationOrderCheck.ScopeState> scopeStates
private java.util.Set<java.lang.String> classFieldNames
private boolean ignoreConstructors
private boolean ignoreModifiers
public DeclarationOrderCheck()
public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void beginTree(DetailAST rootAST)
AbstractCheckbeginTree in class AbstractCheckrootAST - the root of the treepublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processprivate void processConstructor(DetailAST ast)
ast - constructor AST.private void processModifiers(DetailAST ast)
ast - ast of Modifiers.private boolean processModifiersState(DetailAST modifierAst, DeclarationOrderCheck.ScopeState state)
STATE_STATIC_VARIABLE_DEF, STATE_INSTANCE_VARIABLE_DEF,
(STATE_CTOR_DEF, STATE_METHOD_DEF), if it is
it updates states where appropriate or logs violation.modifierAst - modifiers to processstate - current stateprivate void processModifiersSubState(DetailAST modifiersAst, DeclarationOrderCheck.ScopeState state, boolean isStateValid)
Scope), if it is it updates substate or else it
logs violation.modifiersAst - modifiers to processstate - current stateisStateValid - is main state for given modifiers is validprivate boolean isForwardReference(DetailAST fieldDef)
fieldDef - a field definition.private static java.util.Set<DetailAST> getAllTokensOfType(DetailAST ast, int tokenType)
ast - ast node.tokenType - token type.public void leaveToken(DetailAST ast)
AbstractCheckleaveToken in class AbstractCheckast - the token leavingpublic void setIgnoreConstructors(boolean ignoreConstructors)
ignoreConstructors - whether to ignore constructors.public void setIgnoreModifiers(boolean ignoreModifiers)
ignoreModifiers - whether to ignore modifiers.Copyright © 2001-2022. All Rights Reserved.