001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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     * Returns first argument.
070     * @return the first argument. null if not set.
071     */
072    public String getFirstArg() {
073        return firstArg;
074    }
075
076    /**
077     * Gets the line number.
078     * @return the line number
079     */
080    public int getLineNo() {
081        return lineNo;
082    }
083
084    /**
085     * Gets column number.
086     * @return the column number
087     */
088    public int getColumnNo() {
089        return columnNo;
090    }
091
092    @Override
093    public String toString() {
094        return "JavadocTag{tag='" + getTagName() + "' lineNo=" + lineNo + ", columnNo=" + columnNo
095                + ", firstArg='" + firstArg + "'}";
096    }
097
098    /**
099     * Checks that the tag is an 'return' tag.
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     * @return whether the tag is an 'param' tag
109     */
110    public boolean isParamTag() {
111        return tagInfo == JavadocTagInfo.PARAM;
112    }
113
114    /**
115     * Checks that the tag is an 'throws' or 'exception' tag.
116     * @return whether the tag is an 'throws' or 'exception' tag
117     */
118    public boolean isThrowsTag() {
119        return tagInfo == JavadocTagInfo.THROWS
120            || tagInfo == JavadocTagInfo.EXCEPTION;
121    }
122
123    /**
124     * Checks that the tag is a 'see' or 'inheritDoc' tag.
125     * @return whether the tag is a 'see' or 'inheritDoc' tag
126     */
127    public boolean isSeeOrInheritDocTag() {
128        return tagInfo == JavadocTagInfo.SEE || isInheritDocTag();
129    }
130
131    /**
132     * Checks that the tag is a 'inheritDoc' tag.
133     * @return whether the tag is a 'inheritDoc' tag
134     */
135    public boolean isInheritDocTag() {
136        return tagInfo == JavadocTagInfo.INHERIT_DOC;
137    }
138
139    /**
140     * Checks that the tag can contain references to imported classes.
141     * @return whether the tag can contain references to imported classes
142     */
143    public boolean canReferenceImports() {
144        return tagInfo == JavadocTagInfo.SEE
145                || tagInfo == JavadocTagInfo.LINK
146                || tagInfo == JavadocTagInfo.VALUE
147                || tagInfo == JavadocTagInfo.LINKPLAIN
148                || tagInfo == JavadocTagInfo.THROWS
149                || tagInfo == JavadocTagInfo.EXCEPTION;
150    }
151}