001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 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
022/**
023 * Represents a Javadoc tag. Provides methods to query what type of tag it is.
024 * @author Oliver Burn
025 */
026public class JavadocTag {
027    /** The line number of the tag. **/
028    private final int lineNo;
029    /** The column number of the tag. **/
030    private final int columnNo;
031    /** An optional first argument. For example the parameter name. **/
032    private final String firstArg;
033    /** The JavadocTagInfo representing this tag. **/
034    private final JavadocTagInfo tagInfo;
035
036    /**
037     * Constructs the object.
038     * @param line the line number of the tag
039     * @param column the column number of the tag
040     * @param tag the tag string
041     * @param firstArg the tag argument
042     **/
043    public JavadocTag(int line, int column, String tag, String firstArg) {
044        lineNo = line;
045        columnNo = column;
046        this.firstArg = firstArg;
047        tagInfo = JavadocTagInfo.fromName(tag);
048    }
049
050    /**
051     * Constructs the object.
052     * @param line the line number of the tag
053     * @param column the column number of the tag
054     * @param tag the tag string
055     **/
056    public JavadocTag(int line, int column, String tag) {
057        this(line, column, tag, null);
058    }
059
060    /**
061     * Gets tag name.
062     * @return the tag string
063     */
064    public String getTagName() {
065        return tagInfo.getName();
066    }
067
068    /**
069     * @return the first argument. null if not set.
070     */
071    public String getFirstArg() {
072        return firstArg;
073    }
074
075    /**
076     * Gets the line number.
077     * @return the line number
078     */
079    public int getLineNo() {
080        return lineNo;
081    }
082
083    /**
084     * Gets column number.
085     * @return the column number
086     */
087    public int getColumnNo() {
088        return columnNo;
089    }
090
091    @Override
092    public String toString() {
093        return "JavadocTag{tag='" + getTagName() + "' lineNo=" + lineNo + ", columnNo=" + columnNo
094                + ", firstArg='" + firstArg + "'}";
095    }
096
097    /**
098     * Checks that the tag is an 'return' tag.
099     * @return whether the tag is an 'return' tag
100     */
101    public boolean isReturnTag() {
102        return tagInfo == JavadocTagInfo.RETURN;
103    }
104
105    /**
106     * Checks that the tag is an 'param' tag.
107     * @return whether the tag is an 'param' tag
108     */
109    public boolean isParamTag() {
110        return tagInfo == JavadocTagInfo.PARAM;
111    }
112
113    /**
114     * Checks that the tag is an 'throws' or 'exception' tag.
115     * @return whether the tag is an 'throws' or 'exception' tag
116     */
117    public boolean isThrowsTag() {
118        return tagInfo == JavadocTagInfo.THROWS
119            || tagInfo == JavadocTagInfo.EXCEPTION;
120    }
121
122    /**
123     * Checks that the tag is a 'see' or 'inheritDoc' tag.
124     * @return whether the tag is a 'see' or 'inheritDoc' tag
125     */
126    public boolean isSeeOrInheritDocTag() {
127        return tagInfo == JavadocTagInfo.SEE || isInheritDocTag();
128    }
129
130    /**
131     * Checks that the tag is a 'inheritDoc' tag.
132     * @return whether the tag is a 'inheritDoc' tag
133     */
134    public boolean isInheritDocTag() {
135        return tagInfo == JavadocTagInfo.INHERIT_DOC;
136    }
137
138    /**
139     * Checks that the tag can contain references to imported classes.
140     * @return whether the tag can contain references to imported classes
141     */
142    public boolean canReferenceImports() {
143        return tagInfo == JavadocTagInfo.SEE
144                || tagInfo == JavadocTagInfo.LINK
145                || tagInfo == JavadocTagInfo.LINKPLAIN
146                || tagInfo == JavadocTagInfo.THROWS
147                || tagInfo == JavadocTagInfo.EXCEPTION;
148    }
149}