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 /**
23 * Represents a Javadoc tag. Provides methods to query what type of tag it is.
24 */
25 public class JavadocTag {
26
27 /** The line number of the tag. **/
28 private final int lineNo;
29 /** The column number of the tag. **/
30 private final int columnNo;
31 /** An optional first argument. For example the parameter name. **/
32 private final String firstArg;
33 /** The JavadocTagInfo representing this tag. **/
34 private final JavadocTagInfo tagInfo;
35
36 /**
37 * Constructs the object.
38 *
39 * @param line the line number of the tag
40 * @param column the column number of the tag
41 * @param tag the tag string
42 **/
43 public JavadocTag(int line, int column, String tag) {
44 this(line, column, tag, null);
45 }
46
47 /**
48 * Constructs the object.
49 *
50 * @param line the line number of the tag
51 * @param column the column number of the tag
52 * @param tag the tag string
53 * @param firstArg the tag argument
54 **/
55 public JavadocTag(int line, int column, String tag, String firstArg) {
56 lineNo = line;
57 columnNo = column;
58 this.firstArg = firstArg;
59 tagInfo = JavadocTagInfo.fromName(tag);
60 }
61
62 /**
63 * Returns first argument.
64 *
65 * @return the first argument. null if not set.
66 */
67 public String getFirstArg() {
68 return firstArg;
69 }
70
71 /**
72 * Gets the line number.
73 *
74 * @return the line number
75 */
76 public int getLineNo() {
77 return lineNo;
78 }
79
80 /**
81 * Gets column number.
82 *
83 * @return the column number
84 */
85 public int getColumnNo() {
86 return columnNo;
87 }
88
89 @Override
90 public String toString() {
91 return "JavadocTag[tag='" + tagInfo.getName()
92 + "' lineNo=" + lineNo
93 + ", columnNo=" + columnNo
94 + ", firstArg='" + firstArg + "']";
95 }
96
97 /**
98 * Checks that the tag is an 'return' tag.
99 *
100 * @return whether the tag is an 'return' tag
101 */
102 public boolean isReturnTag() {
103 return tagInfo == JavadocTagInfo.RETURN;
104 }
105
106 /**
107 * Checks that the tag is an 'param' tag.
108 *
109 * @return whether the tag is an 'param' tag
110 */
111 public boolean isParamTag() {
112 return tagInfo == JavadocTagInfo.PARAM;
113 }
114
115 /**
116 * Checks that the tag is an 'throws' or 'exception' tag.
117 *
118 * @return whether the tag is an 'throws' or 'exception' tag
119 */
120 public boolean isThrowsTag() {
121 return tagInfo == JavadocTagInfo.THROWS
122 || tagInfo == JavadocTagInfo.EXCEPTION;
123 }
124
125 /**
126 * Checks that the tag is a 'inheritDoc' tag.
127 *
128 * @return whether the tag is a 'inheritDoc' tag
129 */
130 public boolean isInheritDocTag() {
131 return tagInfo == JavadocTagInfo.INHERIT_DOC;
132 }
133
134 /**
135 * Checks that the tag can contain references to imported classes.
136 *
137 * @return whether the tag can contain references to imported classes
138 */
139 public boolean canReferenceImports() {
140 return tagInfo == JavadocTagInfo.SEE
141 || tagInfo == JavadocTagInfo.LINK
142 || tagInfo == JavadocTagInfo.VALUE
143 || tagInfo == JavadocTagInfo.LINKPLAIN
144 || tagInfo == JavadocTagInfo.THROWS
145 || tagInfo == JavadocTagInfo.EXCEPTION;
146 }
147
148 /**
149 * Checks that the tag is a inline tag.
150 *
151 * @return whether the tag is a inline tag
152 */
153 public boolean isInlineTag() {
154 return tagInfo.getType() == JavadocTagInfo.Type.INLINE;
155 }
156
157 }