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.api;
21  
22  import javax.annotation.Nullable;
23  
24  /**
25   * An interface of Checkstyle's AST nodes for traversing trees generated from the
26   * Java code. The main purpose of this interface is to abstract away ANTLR
27   * specific classes from API package so other libraries won't require it.
28   *
29   * @see <a href="https://www.antlr.org/">ANTLR Website</a>
30   */
31  public interface DetailAST {
32  
33      /**
34       * Returns the number of child nodes one level below this node. That is,
35       * does not recurse down the tree.
36       *
37       * @return the number of child nodes
38       */
39      int getChildCount();
40  
41      /**
42       * Returns the number of direct child tokens that have the specified type.
43       *
44       * @param type the token type to match
45       * @return the number of matching token
46       */
47      int getChildCount(int type);
48  
49      /**
50       * Returns the parent token.
51       *
52       * @return the parent token
53       */
54      DetailAST getParent();
55  
56      /**
57       * Gets the text of this AST.
58       *
59       * @return the text.
60       */
61      String getText();
62  
63      /**
64       * Gets the type of this AST.
65       *
66       * @return the type.
67       */
68      int getType();
69  
70      /**
71       * Gets line number.
72       *
73       * @return the line number
74       */
75      int getLineNo();
76  
77      /**
78       * Gets column number.
79       *
80       * @return the column number
81       */
82      int getColumnNo();
83  
84      /**
85       * Gets the last child node.
86       *
87       * @return the last child node
88       */
89      DetailAST getLastChild();
90  
91      /**
92       * Checks if this branch of the parse tree contains a token of the provided type.
93       *
94       * @param type a TokenType
95       * @return true if and only if this branch (including this node)
96       *     contains a token of type {@code type}.
97       * @deprecated
98       *      Usage of this method is no longer accepted. We encourage
99       *      traversal of subtrees to be written per the needs of each check
100      *      to avoid unintended side effects.
101      * @noinspection DeprecatedIsStillUsed, RedundantSuppression
102      * @noinspectionreason DeprecatedIsStillUsed - Method used in unit testing
103      * @noinspectionreason RedundantSuppression - Inspections shows false positive for
104      *      redundant suppression, see
105      *      <a href="https://github.com/checkstyle/checkstyle/issues/12359">here</a>
106      *      for more details.
107      */
108     @Deprecated(since = "8.43")
109     boolean branchContains(int type);
110 
111     /**
112      * Returns the previous sibling or null if no such sibling exists.
113      *
114      * @return the previous sibling or null if no such sibling exists.
115      */
116     DetailAST getPreviousSibling();
117 
118     /**
119      * Returns the first child token that makes a specified type.
120      *
121      * @param type the token type to match
122      * @return the matching token, or null if no match
123      */
124     @Nullable
125     DetailAST findFirstToken(int type);
126 
127     /**
128      * Get the next sibling in line after this one.
129      *
130      * @return the next sibling or null if none.
131      */
132     DetailAST getNextSibling();
133 
134     /**
135      * Get the first child of this AST.
136      *
137      * @return the first child or null if none.
138      */
139     DetailAST getFirstChild();
140 
141     /**
142      * Get number of children of this AST.
143      *
144      * @return the number of children.
145      * @deprecated This method will be removed in a future release.
146      *             Use {@link #getChildCount()} instead.
147      */
148     @Deprecated(since = "8.30")
149     int getNumberOfChildren();
150 
151     /**
152      * Returns whether this AST has any children.
153      *
154      * @return {@code true} if this AST has any children.
155      */
156     boolean hasChildren();
157 
158 }