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