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.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       * Creates a new {@code JavadocNodeImpl} instance.
77       */
78      public JavadocNodeImpl() {
79          // no code by default
80      }
81  
82      /**
83       * Initializes the node from the given token.
84       *
85       * @param token the token to initialize from.
86       */
87      public void initialize(Token token) {
88          type = token.getType();
89          text = token.getText();
90          lineNumber = token.getLine() - 1;
91          columnNumber = token.getCharPositionInLine();
92      }
93  
94      @Override
95      public int getType() {
96          return type;
97      }
98  
99      @Override
100     public String getText() {
101         return text;
102     }
103 
104     @Override
105     public int getLineNumber() {
106         final JavadocNodeImpl node = firstChild;
107         if (node != null) {
108             lineNumber = node.getLineNumber();
109         }
110         return lineNumber;
111     }
112 
113     @Override
114     public int getColumnNumber() {
115         final JavadocNodeImpl node = firstChild;
116         if (node != null) {
117             columnNumber = node.getColumnNumber();
118         }
119         return columnNumber;
120     }
121 
122     @Override
123     public DetailNode getParent() {
124         return parent;
125     }
126 
127     @Override
128     public DetailNode getNextSibling() {
129         return nextSibling;
130     }
131 
132     @Override
133     public DetailNode getPreviousSibling() {
134         return previousSibling;
135     }
136 
137     @Override
138     public JavadocNodeImpl getFirstChild() {
139         return firstChild;
140     }
141 
142     /**
143      * Sets node's type.
144      *
145      * @param type Node's type.
146      */
147     public void setType(int type) {
148         this.type = type;
149     }
150 
151     /**
152      * Sets node's text content.
153      *
154      * @param text Node's text content.
155      */
156     public void setText(String text) {
157         this.text = text;
158     }
159 
160     /**
161      * Sets line number.
162      *
163      * @param lineNumber Line number.
164      */
165     public void setLineNumber(int lineNumber) {
166         this.lineNumber = lineNumber;
167     }
168 
169     /**
170      * Sets column number.
171      *
172      * @param columnNumber Column number.
173      */
174     public void setColumnNumber(int columnNumber) {
175         this.columnNumber = columnNumber;
176     }
177 
178     /**
179      * Sets parent node.
180      *
181      * @param node Parent node.
182      */
183     public void setParent(DetailNode node) {
184         JavadocNodeImpl instance = this;
185         final JavadocNodeImpl newParent = (JavadocNodeImpl) node;
186         do {
187             instance.parent = newParent;
188             instance = instance.nextSibling;
189         } while (instance != null);
190     }
191 
192     /**
193      * Sets next sibling node.
194      *
195      * @param nextSibling Next sibling node.
196      */
197     public void setNextSibling(DetailNode nextSibling) {
198         this.nextSibling = (JavadocNodeImpl) nextSibling;
199         ((JavadocNodeImpl) nextSibling).setParent(parent);
200         ((JavadocNodeImpl) nextSibling).previousSibling = this;
201     }
202 
203     /**
204      * Adds a child node to this node.
205      *
206      * @param newChild Child node to be added.
207      */
208     public void addChild(DetailNode newChild) {
209         final JavadocNodeImpl astImpl = (JavadocNodeImpl) newChild;
210         astImpl.setParent(this);
211 
212         DetailNode temp = firstChild;
213         if (temp == null) {
214             firstChild = (JavadocNodeImpl) newChild;
215         }
216         else {
217             while (temp.getNextSibling() != null) {
218                 temp = temp.getNextSibling();
219             }
220 
221             ((JavadocNodeImpl) temp).setNextSibling(newChild);
222         }
223     }
224 
225     @Override
226     public String toString() {
227         return text + "[" + getLineNumber() + "x" + getColumnNumber() + "]";
228     }
229 
230 }