View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.checks.javadoc;
21  
22  import org.antlr.v4.runtime.Token;
23  
24  import com.puppycrawl.tools.checkstyle.api.DetailNode;
25  
26  /**
27   * Implementation of DetailNode interface that is mutable.
28   *
29   * @noinspection FieldNotUsedInToString
30   * @noinspectionreason FieldNotUsedInToString - We require a specific string format for
31   *       printing to CLI.
32   */
33  public class JavadocNodeImpl implements DetailNode {
34  
35      /**
36       * Node type.
37       */
38      private int type;
39  
40      /**
41       * Node's text content.
42       */
43      private String text;
44  
45      /**
46       * Line number.
47       */
48      private int lineNumber;
49  
50      /**
51       * Column number.
52       */
53      private int columnNumber;
54  
55      /**
56       * Parent node.
57       */
58      private JavadocNodeImpl parent;
59  
60      /**
61       * Next sibling node.
62       */
63      private JavadocNodeImpl nextSibling;
64  
65      /**
66       * Previous sibling.
67       */
68      private JavadocNodeImpl previousSibling;
69  
70      /**
71       * First child of this DetailAST.
72       */
73      private JavadocNodeImpl firstChild;
74  
75      /**
76       * Initializes the node from the given token.
77       *
78       * @param token the token to initialize from.
79       */
80      public void initialize(Token token) {
81          type = token.getType();
82          text = token.getText();
83          lineNumber = token.getLine() - 1;
84          columnNumber = token.getCharPositionInLine();
85      }
86  
87      @Override
88      public int getType() {
89          return type;
90      }
91  
92      @Override
93      public String getText() {
94          return text;
95      }
96  
97      @Override
98      public int getLineNumber() {
99          final JavadocNodeImpl node = firstChild;
100         if (node != null) {
101             lineNumber = node.getLineNumber();
102         }
103         return lineNumber;
104     }
105 
106     @Override
107     public int getColumnNumber() {
108         final JavadocNodeImpl node = firstChild;
109         if (node != null) {
110             columnNumber = node.getColumnNumber();
111         }
112         return columnNumber;
113     }
114 
115     @Override
116     public DetailNode getParent() {
117         return parent;
118     }
119 
120     @Override
121     public DetailNode getNextSibling() {
122         return nextSibling;
123     }
124 
125     @Override
126     public DetailNode getPreviousSibling() {
127         return previousSibling;
128     }
129 
130     @Override
131     public JavadocNodeImpl getFirstChild() {
132         return firstChild;
133     }
134 
135     /**
136      * Sets node's type.
137      *
138      * @param type Node's type.
139      */
140     public void setType(int type) {
141         this.type = type;
142     }
143 
144     /**
145      * Sets node's text content.
146      *
147      * @param text Node's text content.
148      */
149     public void setText(String text) {
150         this.text = text;
151     }
152 
153     /**
154      * Sets line number.
155      *
156      * @param lineNumber Line number.
157      */
158     public void setLineNumber(int lineNumber) {
159         this.lineNumber = lineNumber;
160     }
161 
162     /**
163      * Sets column number.
164      *
165      * @param columnNumber Column number.
166      */
167     public void setColumnNumber(int columnNumber) {
168         this.columnNumber = columnNumber;
169     }
170 
171     /**
172      * Sets parent node.
173      *
174      * @param node Parent node.
175      */
176     public void setParent(DetailNode node) {
177         JavadocNodeImpl instance = this;
178         final JavadocNodeImpl newParent = (JavadocNodeImpl) node;
179         do {
180             instance.parent = newParent;
181             instance = instance.nextSibling;
182         } while (instance != null);
183     }
184 
185     /**
186      * Sets next sibling node.
187      *
188      * @param nextSibling Next sibling node.
189      */
190     public void setNextSibling(DetailNode nextSibling) {
191         this.nextSibling = (JavadocNodeImpl) nextSibling;
192         ((JavadocNodeImpl) nextSibling).setParent(parent);
193         ((JavadocNodeImpl) nextSibling).previousSibling = this;
194     }
195 
196     /**
197      * Adds a child node to this node.
198      *
199      * @param newChild Child node to be added.
200      */
201     public void addChild(DetailNode newChild) {
202         final JavadocNodeImpl astImpl = (JavadocNodeImpl) newChild;
203         astImpl.setParent(this);
204 
205         DetailNode temp = firstChild;
206         if (temp == null) {
207             firstChild = (JavadocNodeImpl) newChild;
208         }
209         else {
210             while (temp.getNextSibling() != null) {
211                 temp = temp.getNextSibling();
212             }
213 
214             ((JavadocNodeImpl) temp).setNextSibling(newChild);
215         }
216     }
217 
218     @Override
219     public String toString() {
220         return text + "[" + getLineNumber() + "x" + getColumnNumber() + "]";
221     }
222 
223 }