MatchXpath

Since Checkstyle 8.39

Description

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 be used only with token types that are listed in XpathUtil.

Properties

name description type default value since
query Specify Xpath query. String "" 8.39

Examples

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() { }
  // violation below 'Private methods must appear after public methods'
  private void method2() { }
  public void method3() { }
  // violation below 'Private methods must appear after public methods'
  private void method4() { }
  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 {
  // violation below 'Parameterized constructors are not allowed'
  public Example2(Object c) { }
  // violation below 'Parameterized constructors are not allowed'
  public Example2(int a, HashMap<String, Integer> b) { }
  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 {
  // violation below 'Method name should not be test or foo'
  public void test() {}
  public void getName() {}
  // violation below 'Method name should not be test or foo'
  public void foo() {}
  public void sayHello() {}
}

To violate if new instance creation was done without var type to avoid duplication of reference in statement


<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"/>
    </module>
  </module>
</module>

Example:


public class Example4 {
  public void foo() {
    // violation below 'New instances should be created via var keyword'
    Object a = new Object();
    var b = new Object();
  }
}

To violate if class has more than 1 constructor


<module name="Checker">
  <module name="TreeWalker">
    <module name="MatchXpath">
      <property name="query"
           value="//CLASS_DEF[count(./OBJBLOCK/CTOR_DEF) > 1]"/>
      <message key="matchxpath.match"
           value="Classes with more than 1 constructor are not allowed"/>
    </module>
  </module>
</module>

Example:


public class Example5 {
  public Example5() { }
  // violation below 'Classes with more than 1 constructor are not allowed'
  class Inner {
    public Inner(int a) { }
    public Inner() { }
  }
}

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.coding

Parent Module

TreeWalker