001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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.checks.javadoc; 021 022import org.antlr.v4.runtime.Token; 023 024import com.puppycrawl.tools.checkstyle.api.DetailNode; 025 026/** 027 * Implementation of DetailNode interface that is mutable. 028 * 029 * @noinspection FieldNotUsedInToString 030 * @noinspectionreason FieldNotUsedInToString - We require a specific string format for 031 * printing to CLI. 032 */ 033public class JavadocNodeImpl implements DetailNode { 034 035 /** 036 * Node type. 037 */ 038 private int type; 039 040 /** 041 * Node's text content. 042 */ 043 private String text; 044 045 /** 046 * Line number. 047 */ 048 private int lineNumber; 049 050 /** 051 * Column number. 052 */ 053 private int columnNumber; 054 055 /** 056 * Parent node. 057 */ 058 private JavadocNodeImpl parent; 059 060 /** 061 * Next sibling node. 062 */ 063 private JavadocNodeImpl nextSibling; 064 065 /** 066 * Previous sibling. 067 */ 068 private JavadocNodeImpl previousSibling; 069 070 /** 071 * First child of this DetailAST. 072 */ 073 private JavadocNodeImpl firstChild; 074 075 /** 076 * Creates a new {@code JavadocNodeImpl} instance. 077 */ 078 public JavadocNodeImpl() { 079 // no code by default 080 } 081 082 /** 083 * Initializes the node from the given token. 084 * 085 * @param token the token to initialize from. 086 */ 087 public void initialize(Token token) { 088 type = token.getType(); 089 text = token.getText(); 090 lineNumber = token.getLine() - 1; 091 columnNumber = token.getCharPositionInLine(); 092 } 093 094 @Override 095 public int getType() { 096 return type; 097 } 098 099 @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}