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='" + tagInfo.getName()
095                + "' lineNo=" + lineNo
096                + ", columnNo=" + columnNo
097                + ", firstArg='" + firstArg + "']";
098    }
099
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}