Class DeclarationOrderCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheck
- All Implemented Interfaces:
Configurable,Contextualizable
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:
- Class (static) variables. First the public class variables, then protected, then package level (no access modifier), and then private.
- Instance variables. First the public class variables, then protected, then package level (no access modifier), and then private.
- Constructors
- Methods
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
}
- Since:
- 3.2
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classPrivate class to encapsulate the state.Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionSet of all class field names.private booleanControl whether to ignore constructors.private booleanControl whether to ignore modifiers (fields, ...).static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file.private Deque<DeclarationOrderCheck.ScopeState>List of Declaration States.private static final intState for the CTOR_DEF.private static final intState for the VARIABLE_DEF.private static final intState for the METHOD_DEF.private static final intState for the VARIABLE_DEF. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidCalled before the starting to process a tree.int[]The configurable token set.getAllTokensOfType(DetailAST ast, int tokenType) Collects all tokens of specific type starting with the current ast node.int[]Returns the default token a check is interested in.int[]The tokens that this check must be registered for.private booleanisForwardReference(DetailAST fieldDef) Checks whether an identifier references a field which has been already defined in class.voidleaveToken(DetailAST ast) Called after all the child nodes have been process.private voidProcesses constructor.private voidProcesses modifiers.private booleanprocessModifiersState(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 voidprocessModifiersSubState(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.voidsetIgnoreConstructors(boolean ignoreConstructors) Setter to control whether to ignore constructors.voidsetIgnoreModifiers(boolean ignoreModifiers) Setter to control whether to ignore modifiers (fields, ...).voidvisitToken(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_CONSTRUCTOR
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_STATIC
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_INSTANCE
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_ACCESS
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
STATE_STATIC_VARIABLE_DEF
State for the VARIABLE_DEF.- See Also:
-
STATE_INSTANCE_VARIABLE_DEF
State for the VARIABLE_DEF.- See Also:
-
STATE_CTOR_DEF
State for the CTOR_DEF.- See Also:
-
STATE_METHOD_DEF
State for the METHOD_DEF.- See Also:
-
scopeStates
List of Declaration States. This is necessary due to inner classes that have their own state. -
classFieldNames
Set of all class field names. -
ignoreConstructors
Control whether to ignore constructors. -
ignoreModifiers
Control whether to ignore modifiers (fields, ...).
-
-
Constructor Details
-
DeclarationOrderCheck
public DeclarationOrderCheck()
-
-
Method Details
-
getDefaultTokens
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheckThe 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:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
beginTree
Description copied from class:AbstractCheckCalled before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTreein classAbstractCheck- Parameters:
rootAST- the root of the tree
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
processConstructor
Processes constructor.- Parameters:
ast- constructor AST.
-
processModifiers
Processes modifiers.- Parameters:
ast- ast of Modifiers.
-
processModifiersState
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.- Parameters:
modifierAst- modifiers to processstate- current state- Returns:
- true if modifierAst is valid in given state, false otherwise
-
processModifiersSubState
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.- Parameters:
modifiersAst- modifiers to processstate- current stateisStateValid- is main state for given modifiers is valid
-
isForwardReference
Checks whether an identifier references a field which has been already defined in class.- Parameters:
fieldDef- a field definition.- Returns:
- true if an identifier references a field which has been already defined in class.
-
getAllTokensOfType
Collects all tokens of specific type starting with the current ast node.- Parameters:
ast- ast node.tokenType- token type.- Returns:
- a set of all tokens of specific type starting with the current ast node.
-
leaveToken
Description copied from class:AbstractCheckCalled after all the child nodes have been process.- Overrides:
leaveTokenin classAbstractCheck- Parameters:
ast- the token leaving
-
setIgnoreConstructors
Setter to control whether to ignore constructors.- Parameters:
ignoreConstructors- whether to ignore constructors.- Since:
- 5.2
-
setIgnoreModifiers
Setter to control whether to ignore modifiers (fields, ...).- Parameters:
ignoreModifiers- whether to ignore modifiers.- Since:
- 5.2
-