Class CustomDeclarationOrderCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.github.sevntu.checkstyle.checks.coding.CustomDeclarationOrderCheck
-
- All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable
,com.puppycrawl.tools.checkstyle.api.Contextualizable
public class CustomDeclarationOrderCheck extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
Checks that the parts of a class(main, nested, member inner) declaration appear in the rules order set by user using regular expressions.
The check forms line which consists of class member annotations, modifiers, type and name from your code and compares it with your RegExp.
The rule consists of:ClassMember(RegExp)
To set class order use the following notation of the class members (case insensitive):- "Field" to denote the Fields
- "DeclareAnonClassField" to denote the fields keeping objects of anonymous classes
- "Ctor" to denote the Constructors
- "Method" to denote the Methods
- "GetterSetter" to denote the group of getter and setter methods
- "MainMethod" to denote the main method
- "InnerClass" to denote the Inner Classes
- "InnerInterface" to denote the Inner Interfaces
- "InnerEnum" to denote the Inner Enums
- Annotations
- Modifiers(public, protected, private, abstract, static, final)
- Type
- Name
Use separator
' ', '.', '\s'
between declaration in the RegExp. Whitespace should be added after each modifier.Example: Field(public .*final .*) Field(public final .*) Field(public
NOTICE!\s*
final .*)It is important to write exact order of modifiers in rules. So rule
Field(public final)
does not match tofinal public value;
. ModifierOrderCheck is recommended to use.If you set empty RegExp e.g.
Field()
, it means that class member doesn't have modifiers(default modifier) and checking the type and name of member doesn't occur.Between the declaration of a array and generic can't be whitespaces. E.g.:
ArrayList<String[]> someName
Use the separator '###' between the class declarations.
For Example:
Field(private static final long serialVersionUID) ### Field(public static final .*) ### Field(.*private .*) ### Ctor(.*) ### GetterSetter(.*) ### Method(.*public .*final .*|@Ignore.*public .*) ### Method(public static .*(final|(new|edit|create).*).*) ### InnerClass(public abstract .*) ### InnerInterface(.*) ### InnerEnum(.*)
What is group of getters and setters(
GetterSetter
)?It is ordered sequence of getters and setters like:
public int getValue() { log.info("Getting value"); return value; } public void setValue(int newValue) { value = newValue; } public Object getObj() { return obj; } public void setObj(Object obj) { if (obj != null) { this.obj = obj; } else { throw new IllegalArgumentException("Null value"); } } ...
Getter is public method that returns class field. Name of getter should be 'getFieldName' in camel case.
Setter is public method with one parameter that assigns this parameter to class field. Name of setter should be 'setFieldName' in camel case.
Setter of field X should be right after getter of field X.
- Since:
- 1.8.0
- Author:
- Danil Lopatin, Baratali Izmailov
-
-
Field Summary
Fields Modifier and Type Field Description static String
MSG_KEY_CLASS
A key is pointing to the warning message text in "messages.properties" file.static String
MSG_KEY_CONSTRUCTOR
A key is pointing to the warning message text in "messages.properties" file.static String
MSG_KEY_ENUM
A key is pointing to the warning message text in "messages.properties" file.static String
MSG_KEY_FIELD
A key is pointing to the warning message text in "messages.properties" file.static String
MSG_KEY_INTERFACE
A key is pointing to the warning message text in "messages.properties" file.static String
MSG_KEY_INVALID_SETTER
A key is pointing to the warning message text in "messages.properties" file.static String
MSG_KEY_METHOD
A key is pointing to the warning message text in "messages.properties" file.
-
Constructor Summary
Constructors Constructor Description CustomDeclarationOrderCheck()
Constructor to set default format.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]
getAcceptableTokens()
int[]
getDefaultTokens()
int[]
getRequiredTokens()
void
leaveToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
void
setCaseSensitive(boolean caseSensitive)
Set whether or not the match is case sensitive.void
setCustomDeclarationOrder(String inputOrderDeclaration)
Set custom order declaration from string with user rules.void
setFieldPrefix(String fieldPrefix)
Set prefix of class fields.void
visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, 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
-
-
-
-
Field Detail
-
MSG_KEY_FIELD
public static final String MSG_KEY_FIELD
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_KEY_METHOD
public static final String MSG_KEY_METHOD
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_KEY_CONSTRUCTOR
public static final String MSG_KEY_CONSTRUCTOR
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_KEY_CLASS
public static final String MSG_KEY_CLASS
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_KEY_INTERFACE
public static final String MSG_KEY_INTERFACE
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_KEY_ENUM
public static final String MSG_KEY_ENUM
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_KEY_INVALID_SETTER
public static final String MSG_KEY_INVALID_SETTER
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
-
Method Detail
-
setCustomDeclarationOrder
public final void setCustomDeclarationOrder(String inputOrderDeclaration)
Set custom order declaration from string with user rules.- Parameters:
inputOrderDeclaration
- The string line with the user custom declaration.- Throws:
IllegalArgumentException
- if the input rule could not be parsed
-
setFieldPrefix
public void setFieldPrefix(String fieldPrefix)
Set prefix of class fields.- Parameters:
fieldPrefix
- string
-
setCaseSensitive
public void setCaseSensitive(boolean caseSensitive)
Set whether or not the match is case sensitive.- Parameters:
caseSensitive
- true if the match is case sensitive.
-
getDefaultTokens
public int[] getDefaultTokens()
- Specified by:
getDefaultTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getAcceptableTokens
public int[] getAcceptableTokens()
- Specified by:
getAcceptableTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getRequiredTokens
public int[] getRequiredTokens()
- Specified by:
getRequiredTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
visitToken
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
- Overrides:
visitToken
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
leaveToken
public void leaveToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
- Overrides:
leaveToken
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
-