001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2022 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.api;
021
022/**
023 * A interface of Checkstyle's AST nodes for traversing trees generated from the
024 * Java code. The main purpose of this interface is to abstract away ANTLR
025 * specific classes from API package so other libraries won't require it.
026 *
027 * @see <a href="https://www.antlr.org/">ANTLR Website</a>
028 */
029public interface DetailAST {
030
031    /**
032     * Returns the number of child nodes one level below this node. That is is
033     * does not recurse down the tree.
034     *
035     * @return the number of child nodes
036     */
037    int getChildCount();
038
039    /**
040     * Returns the number of direct child tokens that have the specified type.
041     *
042     * @param type the token type to match
043     * @return the number of matching token
044     */
045    int getChildCount(int type);
046
047    /**
048     * Returns the parent token.
049     *
050     * @return the parent token
051     */
052    DetailAST getParent();
053
054    /**
055     * Gets the text of this AST.
056     *
057     * @return the text.
058     */
059    String getText();
060
061    /**
062     * Gets the type of this AST.
063     *
064     * @return the type.
065     */
066    int getType();
067
068    /**
069     * Gets line number.
070     *
071     * @return the line number
072     */
073    int getLineNo();
074
075    /**
076     * Gets column number.
077     *
078     * @return the column number
079     */
080    int getColumnNo();
081
082    /**
083     * Gets the last child node.
084     *
085     * @return the last child node
086     */
087    DetailAST getLastChild();
088
089    /**
090     * Checks if this branch of the parse tree contains a token
091     * of the provided type.
092     *
093     * @param type a TokenType
094     * @return true if and only if this branch (including this node)
095     *     contains a token of type {@code type}.
096     * @deprecated
097     *      Usage of this method is no longer accepted. We encourage
098     *      traversal of subtrees to be written per the needs of each check
099     *      to avoid unintended side effects.
100     */
101    @Deprecated(since = "8.43")
102    boolean branchContains(int type);
103
104    /**
105     * Returns the previous sibling or null if no such sibling exists.
106     *
107     * @return the previous sibling or null if no such sibling exists.
108     */
109    DetailAST getPreviousSibling();
110
111    /**
112     * Returns the first child token that makes a specified type.
113     *
114     * @param type the token type to match
115     * @return the matching token, or null if no match
116     */
117    DetailAST findFirstToken(int type);
118
119    /**
120     * Get the next sibling in line after this one.
121     *
122     * @return the next sibling or null if none.
123     */
124    DetailAST getNextSibling();
125
126    /**
127     * Get the first child of this AST.
128     *
129     * @return the first child or null if none.
130     */
131    DetailAST getFirstChild();
132
133    /**
134     * Get number of children of this AST.
135     *
136     * @return the number of children.
137     * @deprecated This method will be removed in a future release.
138     *             Use {@link #getChildCount()} instead.
139     */
140    @Deprecated(since = "8.30")
141    int getNumberOfChildren();
142
143    /**
144     * Returns whether this AST has any children.
145     *
146     * @return {@code true} if this AST has any children.
147     */
148    boolean hasChildren();
149}