View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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       * @param firstArg the tag argument
43       **/
44      public JavadocTag(int line, int column, String tag, String firstArg) {
45          lineNo = line;
46          columnNo = column;
47          this.firstArg = firstArg;
48          tagInfo = JavadocTagInfo.fromName(tag);
49      }
50  
51      /**
52       * Constructs the object.
53       *
54       * @param line the line number of the tag
55       * @param column the column number of the tag
56       * @param tag the tag string
57       **/
58      public JavadocTag(int line, int column, String tag) {
59          this(line, column, tag, null);
60      }
61  
62      /**
63       * Gets tag name.
64       *
65       * @return the tag string
66       */
67      public String getTagName() {
68          return tagInfo.getName();
69      }
70  
71      /**
72       * Returns first argument.
73       *
74       * @return the first argument. null if not set.
75       */
76      public String getFirstArg() {
77          return firstArg;
78      }
79  
80      /**
81       * Gets the line number.
82       *
83       * @return the line number
84       */
85      public int getLineNo() {
86          return lineNo;
87      }
88  
89      /**
90       * Gets column number.
91       *
92       * @return the column number
93       */
94      public int getColumnNo() {
95          return columnNo;
96      }
97  
98      @Override
99      public String toString() {
100         return "JavadocTag[tag='" + tagInfo.getName()
101                 + "' lineNo=" + lineNo
102                 + ", columnNo=" + columnNo
103                 + ", firstArg='" + firstArg + "']";
104     }
105 
106     /**
107      * Checks that the tag is an 'return' tag.
108      *
109      * @return whether the tag is an 'return' tag
110      */
111     public boolean isReturnTag() {
112         return tagInfo == JavadocTagInfo.RETURN;
113     }
114 
115     /**
116      * Checks that the tag is an 'param' tag.
117      *
118      * @return whether the tag is an 'param' tag
119      */
120     public boolean isParamTag() {
121         return tagInfo == JavadocTagInfo.PARAM;
122     }
123 
124     /**
125      * Checks that the tag is an 'throws' or 'exception' tag.
126      *
127      * @return whether the tag is an 'throws' or 'exception' tag
128      */
129     public boolean isThrowsTag() {
130         return tagInfo == JavadocTagInfo.THROWS
131             || tagInfo == JavadocTagInfo.EXCEPTION;
132     }
133 
134     /**
135      * Checks that the tag is a 'see' or 'inheritDoc' tag.
136      *
137      * @return whether the tag is a 'see' or 'inheritDoc' tag
138      */
139     public boolean isSeeOrInheritDocTag() {
140         return tagInfo == JavadocTagInfo.SEE || isInheritDocTag();
141     }
142 
143     /**
144      * Checks that the tag is a 'inheritDoc' tag.
145      *
146      * @return whether the tag is a 'inheritDoc' tag
147      */
148     public boolean isInheritDocTag() {
149         return tagInfo == JavadocTagInfo.INHERIT_DOC;
150     }
151 
152     /**
153      * Checks that the tag can contain references to imported classes.
154      *
155      * @return whether the tag can contain references to imported classes
156      */
157     public boolean canReferenceImports() {
158         return tagInfo == JavadocTagInfo.SEE
159                 || tagInfo == JavadocTagInfo.LINK
160                 || tagInfo == JavadocTagInfo.VALUE
161                 || tagInfo == JavadocTagInfo.LINKPLAIN
162                 || tagInfo == JavadocTagInfo.THROWS
163                 || tagInfo == JavadocTagInfo.EXCEPTION;
164     }
165 
166 }