View Javadoc
1   /*
2    * Copyright (c) 2011-2024 Qulice.com
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.checkstyle;
32  
33  import com.puppycrawl.tools.checkstyle.api.DetailAST;
34  
35  /**
36   * Utility class that checks the existence
37   * of tokens of specific type in the AST node subtree.
38   *
39   * Some checks used branchContains() method in DetailAST
40   * which recursively searched node subtree for the child of a given type.
41   * However, this method was deprecated in upstream due to unintended too
42   * deep scanning. It is recommended to write traversal implementation
43   * for your needs by yourself to avoid unexpected side effects. So here follows it's
44   * simple implementation.
45   *
46   * @since 1.0
47   */
48  class BranchContains {
49      /**
50       * Node, represented by this object.
51       */
52      private final DetailAST node;
53  
54      /**
55       * Creates a decorator which is able to search in the node's subtree.
56       * @param node Node which will be represented by the new BranchContains instance.
57       */
58      BranchContains(final DetailAST node) {
59          this.node = node;
60      }
61  
62      /**
63       * Checks if there is a node of type `type` in this node subtree.
64       * The root node itself may also match, i.e.
65       * `new BranchContains(node).contains(node.getType())` is always true
66       * @param type Desired type
67       * @return Whether node of given type exists somewhere in the subtree
68       */
69      boolean check(final int type) {
70          return this.node.getType() == type
71              || new ChildStream(this.node)
72              .children()
73              .anyMatch(child -> new BranchContains(child).check(type));
74      }
75  }