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):
Use separator ' ', '.', '\s' between declaration in the RegExp.
Whitespace should be added after each modifier.
Example:
Field(public .*final .*)
Field(public final .*)
Field(public\s*final .*)
NOTICE!
It is important to write exact order of modifiers in rules. So rule
Field(public final) does not match to
final 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.
| Modifier and Type | Field and 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 and Description |
|---|
CustomDeclarationOrderCheck()
Constructor to set default format.
|
| Modifier and Type | Method and 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) |
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeveritypublic static final String MSG_KEY_FIELD
public static final String MSG_KEY_METHOD
public static final String MSG_KEY_CONSTRUCTOR
public static final String MSG_KEY_CLASS
public static final String MSG_KEY_INTERFACE
public static final String MSG_KEY_ENUM
public static final String MSG_KEY_INVALID_SETTER
public CustomDeclarationOrderCheck()
public final void setCustomDeclarationOrder(String inputOrderDeclaration)
inputOrderDeclaration - The string line with the user custom
declaration.IllegalArgumentException - if the input rule could not be parsedpublic void setFieldPrefix(String fieldPrefix)
fieldPrefix - stringpublic void setCaseSensitive(boolean caseSensitive)
caseSensitive - true if the match is case sensitive.public int[] getDefaultTokens()
getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getAcceptableTokens()
getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getRequiredTokens()
getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void leaveToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
leaveToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheckCopyright © 2021. All rights reserved.