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