Since Checkstyle 8.39
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 be used only with token types that are listed in XpathUtil.
name | description | type | default value | since |
---|---|---|---|---|
query | Specify Xpath query. | String | "" |
8.39 |
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="Checker"> <module name="TreeWalker"> <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> </module> </module>
Example:
public class Example1 { public void method1() { } private void method2() { } // violation public void method3() { } private void method4() { } // violation public void method5() { } private void method6() { } }
To violate if there are any parametrized constructors
<module name="Checker"> <module name="TreeWalker"> <module name="MatchXpath"> <property name="query" value="//CTOR_DEF[count(./PARAMETERS/*) > 0]"/> <message key="matchxpath.match" value="Parameterized constructors are not allowed"/> </module> </module> </module>
Example:
public class Example2 { public Example2(Object c) { } // violation public Example2(int a, HashMap<String, Integer> b) { } // violation public Example2() { } }
To violate if method name is 'test' or 'foo'
<module name="Checker"> <module name="TreeWalker"> <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> </module> </module>
Example:
public class Example3 { public void test() {} // violation public void getName() {} public void foo() {} // violation public void sayHello() {} }
To violate if new instance creation was done without var type
<module name="Checker"> <module name="TreeWalker"> <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> </module> </module>
Example:
public class Example4 { public void foo() { Object a = new Object(); // violation var b = new Object(); } }
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.coding