View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.xpath;
21  
22  import java.util.List;
23  
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.utils.XpathUtil;
26  
27  /**
28   * Represents DetailAST's root node of Xpath-tree.
29   */
30  public class RootNode extends AbstractRootNode {
31  
32      /** The ast node. */
33      private final DetailAST detailAst;
34  
35      /**
36       * Creates a new {@code RootNode} instance.
37       *
38       * @param detailAst reference to {@code DetailAST}
39       */
40      public RootNode(DetailAST detailAst) {
41          this.detailAst = detailAst;
42      }
43  
44      /**
45       * Iterates siblings of the current node and recursively creates new Xpath-nodes.
46       *
47       * @return children list
48       */
49      @Override
50      protected List<AbstractNode> createChildren() {
51          return XpathUtil.createChildren(this, this, detailAst);
52      }
53  
54      /**
55       * Determine whether the node has any children.
56       *
57       * @return {@code true} is the node has any children.
58       */
59      @Override
60      public boolean hasChildNodes() {
61          return detailAst != null;
62      }
63  
64      /**
65       * Returns line number.
66       *
67       * @return line number
68       */
69      @Override
70      public int getLineNumber() {
71          return detailAst.getLineNo();
72      }
73  
74      /**
75       * Returns column number.
76       *
77       * @return column number
78       */
79      @Override
80      public int getColumnNumber() {
81          return detailAst.getColumnNo();
82      }
83  
84      /**
85       * Getter method for token type. Returns the actual type of the underlying AST root, which is
86       * either {@code COMPILATION_UNIT} for an ordinary compilation unit or
87       * {@code COMPACT_COMPILATION_UNIT} for a JEP 512 compact source file.
88       *
89       * @return token type of the underlying AST root
90       */
91      @Override
92      public int getTokenType() {
93          return detailAst.getType();
94      }
95  
96      /**
97       * Returns underlying node.
98       *
99       * @return underlying node
100      */
101     @Override
102     public DetailAST getUnderlyingNode() {
103         return detailAst;
104     }
105 
106 }