Class 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 }
-
Property
ignoreConstructors
- control whether to ignore constructors. Type isboolean
. Default value isfalse
. -
Property
ignoreModifiers
- control whether to ignore modifiers (fields, ...). Type isboolean
. Default value isfalse
.
To configure the check:
<module name="DeclarationOrder"/>
With default options:
class K { int a; void m(){} K(){} <-- "Constructor definition in wrong order" int b; <-- "Instance variable definition in wrong order" }
With ignoreConstructors option:
class K { int a; void m(){} K(){} int b; <-- "Instance variable definition in wrong order" }
With ignoreConstructors option and without a method definition in a source class:
class K { int a; K(){} int b; <-- "Instance variable definition 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
- Since:
- 3.2
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
A key is pointing to the warning message text in "messages.properties" file.static final String
A key is pointing to the warning message text in "messages.properties" file.static final String
A key is pointing to the warning message text in "messages.properties" file.static final String
A key is pointing to the warning message text in "messages.properties" file. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Called before the starting to process a tree.int[]
The configurable token set.int[]
Returns the default token a check is interested in.int[]
The tokens that this check must be registered for.void
leaveToken
(DetailAST ast) Called after all the child nodes have been process.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.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokens
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
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:
-
-
Constructor Details
-
DeclarationOrderCheck
public DeclarationOrderCheck()
-
-
Method Details
-
getDefaultTokens
public int[] getDefaultTokens()Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
public int[] getAcceptableTokens()Description copied from class:AbstractCheck
The 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:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
public int[] getRequiredTokens()Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
-
beginTree
Description copied from class:AbstractCheck
Called before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTree
in classAbstractCheck
- Parameters:
rootAST
- the root of the tree
-
visitToken
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
leaveToken
Description copied from class:AbstractCheck
Called after all the child nodes have been process.- Overrides:
leaveToken
in classAbstractCheck
- Parameters:
ast
- the token leaving
-
setIgnoreConstructors
public void setIgnoreConstructors(boolean ignoreConstructors) Setter to control whether to ignore constructors.- Parameters:
ignoreConstructors
- whether to ignore constructors.
-
setIgnoreModifiers
public void setIgnoreModifiers(boolean ignoreModifiers) Setter to control whether to ignore modifiers (fields, ...).- Parameters:
ignoreModifiers
- whether to ignore modifiers.
-