public class MatchXpathCheck extends AbstractCheck
Evaluates Xpath query and report violation on all matching AST nodes. This check allows user to implement custom checks using Xpath. If Xpath query is not specified explicitly, then the check does nothing.
It is recommended to define custom message for violation to explain what is not allowed and what
to use instead, default message might be too abstract. To customize a message you need to
add message element with matchxpath.match as key attribute and
desired message as value attribute.
Please read more about Xpath syntax at Xpath Syntax. Information regarding Xpath functions can be found at XSLT/XPath Reference. Note, that @text attribute can used only with token types that are listed in XpathUtil.
query - Specify Xpath query.
Type is java.lang.String.
Default value is "".
Checkstyle provides command line tool and GUI application with options to show AST and to ease usage of Xpath queries.
-T option prints AST tree of the checked file.
$ java -jar checkstyle-X.XX-all.jar -T Main.java
CLASS_DEF -> CLASS_DEF [1:0]
|--MODIFIERS -> MODIFIERS [1:0]
| `--LITERAL_PUBLIC -> public [1:0]
|--LITERAL_CLASS -> class [1:7]
|--IDENT -> Main [1:13]
`--OBJBLOCK -> OBJBLOCK [1:18]
|--LCURLY -> { [1:18]
|--METHOD_DEF -> METHOD_DEF [2:4]
| |--MODIFIERS -> MODIFIERS [2:4]
| | `--LITERAL_PUBLIC -> public [2:4]
| |--TYPE -> TYPE [2:11]
| | `--IDENT -> String [2:11]
| |--IDENT -> sayHello [2:18]
| |--LPAREN -> ( [2:26]
| |--PARAMETERS -> PARAMETERS [2:27]
| | `--PARAMETER_DEF -> PARAMETER_DEF [2:27]
| | |--MODIFIERS -> MODIFIERS [2:27]
| | |--TYPE -> TYPE [2:27]
| | | `--IDENT -> String [2:27]
| | `--IDENT -> name [2:34]
| |--RPAREN -> ) [2:38]
| `--SLIST -> { [2:40]
| |--LITERAL_RETURN -> return [3:8]
| | |--EXPR -> EXPR [3:25]
| | | `--PLUS -> + [3:25]
| | | |--STRING_LITERAL -> "Hello, " [3:15]
| | | `--IDENT -> name [3:27]
| | `--SEMI -> ; [3:31]
| `--RCURLY -> } [4:4]
`--RCURLY -> } [5:0]
-b option shows AST nodes that match given Xpath query. This command can be used to validate accuracy of Xpath query against given file.
$ java -jar checkstyle-X.XX-all.jar Main.java -b "//METHOD_DEF[./IDENT[@text='sayHello']]" CLASS_DEF -> CLASS_DEF [1:0] `--OBJBLOCK -> OBJBLOCK [1:18] |--METHOD_DEF -> METHOD_DEF [2:4]
The following example demonstrates validation of methods order, so that public methods should come before the private ones:
<module name="MatchXpath"> <property name="query" value="//METHOD_DEF[.//LITERAL_PRIVATE and following-sibling::METHOD_DEF[.//LITERAL_PUBLIC]]"/> <message key="matchxpath.match" value="Private methods must appear after public methods"/> </module>
Example:
public class Test {
public void method1() { }
private void method2() { } // violation
public void method3() { }
private void method4() { } // violation
public void method5() { }
private void method6() { } // ok
}
To violate if there are any parametrized constructors
<module name="MatchXpath"> <property name="query" value="//CTOR_DEF[count(./PARAMETERS/*) > 0]"/> <message key="matchxpath.match" value="Parameterized constructors are not allowed, there should be only default ctor"/> </module>
Example:
public class Test {
public Test(Object c) { } // violation
public Test(int a, HashMap<String, Integer> b) { } // violation
public Test() { } // ok
}
To violate if method name is 'test' or 'foo'
<module name="MatchXpath"> <property name="query" value="//METHOD_DEF[./IDENT[@text='test' or @text='foo']]"/> <message key="matchxpath.match" value="Method name should not be 'test' or 'foo'"/> </module>
Example:
public class Test {
public void test() {} // violation
public void getName() {} // ok
public void foo() {} // violation
public void sayHello() {} // ok
}
To violate if new instance creation was done without var type
<module name="MatchXpath"> <property name="query" value="//VARIABLE_DEF[./ASSIGN/EXPR/LITERAL_NEW and not(./TYPE/IDENT[@text='var'])]"/> <message key="matchxpath.match" value="New instances should be created via 'var' keyword to avoid duplication of type reference in statement"/> </module>
Example:
public class Test {
public void foo() {
SomeObject a = new SomeObject(); // violation
var b = new SomeObject(); // OK
}
}
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
matchxpath.match
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
MSG_KEY
A key is pointing to the warning message text provided by user.
|
| Constructor and Description |
|---|
MatchXpathCheck() |
| Modifier and Type | Method and Description |
|---|---|
void |
beginTree(DetailAST rootAST)
Called before the starting to process a tree.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
void |
setQuery(java.lang.String query)
Setter to specify Xpath query.
|
clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens, visitTokenfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY
public MatchXpathCheck()
public void setQuery(java.lang.String query)
query - Xpath query.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 treeCopyright © 2001-2020. All Rights Reserved.