| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package com.puppycrawl.tools.checkstyle.checks.javadoc; |
| 20 | |
|
| 21 | |
import com.puppycrawl.tools.checkstyle.api.Check; |
| 22 | |
import com.puppycrawl.tools.checkstyle.api.DetailAST; |
| 23 | |
import com.puppycrawl.tools.checkstyle.api.FileContents; |
| 24 | |
import com.puppycrawl.tools.checkstyle.api.JavadocTagInfo; |
| 25 | |
import com.puppycrawl.tools.checkstyle.api.Scope; |
| 26 | |
import com.puppycrawl.tools.checkstyle.api.ScopeUtils; |
| 27 | |
import com.puppycrawl.tools.checkstyle.api.TextBlock; |
| 28 | |
import com.puppycrawl.tools.checkstyle.api.TokenTypes; |
| 29 | |
import com.puppycrawl.tools.checkstyle.api.Utils; |
| 30 | |
import com.puppycrawl.tools.checkstyle.checks.CheckUtils; |
| 31 | |
import java.util.List; |
| 32 | |
import java.util.regex.Matcher; |
| 33 | |
import java.util.regex.Pattern; |
| 34 | |
import java.util.regex.PatternSyntaxException; |
| 35 | |
import org.apache.commons.beanutils.ConversionException; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | 21 | public class JavadocTypeCheck |
| 44 | |
extends Check |
| 45 | |
{ |
| 46 | |
|
| 47 | 21 | private Scope mScope = Scope.PRIVATE; |
| 48 | |
|
| 49 | |
private Scope mExcludeScope; |
| 50 | |
|
| 51 | |
private Pattern mAuthorFormatPattern; |
| 52 | |
|
| 53 | |
private Pattern mVersionFormatPattern; |
| 54 | |
|
| 55 | |
private String mAuthorFormat; |
| 56 | |
|
| 57 | |
private String mVersionFormat; |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
private boolean mAllowMissingParamTags; |
| 63 | |
|
| 64 | |
private boolean mAllowUnknownTags; |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public void setScope(String aFrom) |
| 71 | |
{ |
| 72 | 7 | mScope = Scope.getInstance(aFrom); |
| 73 | 7 | } |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public void setExcludeScope(String aScope) |
| 80 | |
{ |
| 81 | 1 | mExcludeScope = Scope.getInstance(aScope); |
| 82 | 1 | } |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
public void setAuthorFormat(String aFormat) |
| 90 | |
throws ConversionException |
| 91 | |
{ |
| 92 | |
try { |
| 93 | 3 | mAuthorFormat = aFormat; |
| 94 | 3 | mAuthorFormatPattern = Utils.getPattern(aFormat); |
| 95 | |
} |
| 96 | 0 | catch (final PatternSyntaxException e) { |
| 97 | 0 | throw new ConversionException("unable to parse " + aFormat, e); |
| 98 | 3 | } |
| 99 | 3 | } |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
public void setVersionFormat(String aFormat) |
| 107 | |
throws ConversionException |
| 108 | |
{ |
| 109 | |
try { |
| 110 | 3 | mVersionFormat = aFormat; |
| 111 | 3 | mVersionFormatPattern = Utils.getPattern(aFormat); |
| 112 | |
} |
| 113 | 0 | catch (final PatternSyntaxException e) { |
| 114 | 0 | throw new ConversionException("unable to parse " + aFormat, e); |
| 115 | 3 | } |
| 116 | |
|
| 117 | 3 | } |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
public void setAllowMissingParamTags(boolean aFlag) |
| 126 | |
{ |
| 127 | 1 | mAllowMissingParamTags = aFlag; |
| 128 | 1 | } |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
public void setAllowUnknownTags(boolean aFlag) |
| 135 | |
{ |
| 136 | 1 | mAllowUnknownTags = aFlag; |
| 137 | 1 | } |
| 138 | |
|
| 139 | |
@Override |
| 140 | |
public int[] getDefaultTokens() |
| 141 | |
{ |
| 142 | 21 | return new int[] { |
| 143 | |
TokenTypes.INTERFACE_DEF, |
| 144 | |
TokenTypes.CLASS_DEF, |
| 145 | |
TokenTypes.ENUM_DEF, |
| 146 | |
TokenTypes.ANNOTATION_DEF, |
| 147 | |
}; |
| 148 | |
} |
| 149 | |
|
| 150 | |
@Override |
| 151 | |
public void visitToken(DetailAST aAST) |
| 152 | |
{ |
| 153 | 145 | if (shouldCheck(aAST)) { |
| 154 | 107 | final FileContents contents = getFileContents(); |
| 155 | 107 | final int lineNo = aAST.getLineNo(); |
| 156 | 107 | final TextBlock cmt = contents.getJavadocBefore(lineNo); |
| 157 | 107 | if (cmt == null) { |
| 158 | 39 | log(lineNo, "javadoc.missing"); |
| 159 | |
} |
| 160 | 68 | else if (ScopeUtils.isOuterMostType(aAST)) { |
| 161 | |
|
| 162 | 63 | final List<JavadocTag> tags = getJavadocTags(cmt); |
| 163 | 63 | checkTag(lineNo, tags, JavadocTagInfo.AUTHOR.getName(), |
| 164 | |
mAuthorFormatPattern, mAuthorFormat); |
| 165 | 63 | checkTag(lineNo, tags, JavadocTagInfo.VERSION.getName(), |
| 166 | |
mVersionFormatPattern, mVersionFormat); |
| 167 | |
|
| 168 | 63 | final List<String> typeParamNames = |
| 169 | |
CheckUtils.getTypeParameterNames(aAST); |
| 170 | |
|
| 171 | 63 | if (!mAllowMissingParamTags) { |
| 172 | |
|
| 173 | 62 | for (final String string : typeParamNames) { |
| 174 | 3 | checkTypeParamTag( |
| 175 | |
lineNo, tags, string); |
| 176 | |
} |
| 177 | |
} |
| 178 | |
|
| 179 | 63 | checkUnusedTypeParamTags(tags, typeParamNames); |
| 180 | |
} |
| 181 | |
} |
| 182 | 145 | } |
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
private boolean shouldCheck(final DetailAST aAST) |
| 190 | |
{ |
| 191 | 145 | final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS); |
| 192 | 145 | final Scope declaredScope = ScopeUtils.getScopeFromMods(mods); |
| 193 | 145 | final Scope scope = |
| 194 | |
ScopeUtils.inInterfaceOrAnnotationBlock(aAST) |
| 195 | |
? Scope.PUBLIC : declaredScope; |
| 196 | 145 | final Scope surroundingScope = ScopeUtils.getSurroundingScope(aAST); |
| 197 | |
|
| 198 | 145 | return scope.isIn(mScope) |
| 199 | |
&& ((surroundingScope == null) || surroundingScope.isIn(mScope)) |
| 200 | |
&& ((mExcludeScope == null) |
| 201 | |
|| !scope.isIn(mExcludeScope) |
| 202 | |
|| ((surroundingScope != null) |
| 203 | |
&& !surroundingScope.isIn(mExcludeScope))); |
| 204 | |
} |
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
private List<JavadocTag> getJavadocTags(TextBlock aCmt) |
| 212 | |
{ |
| 213 | 63 | final JavadocTags tags = JavadocUtils.getJavadocTags(aCmt, |
| 214 | |
JavadocUtils.JavadocTagType.BLOCK); |
| 215 | 63 | if (!mAllowUnknownTags) { |
| 216 | 62 | for (final InvalidJavadocTag tag : tags.getInvalidTags()) { |
| 217 | 1 | log(tag.getLine(), tag.getCol(), "javadoc.unknownTag", |
| 218 | |
tag.getName()); |
| 219 | |
} |
| 220 | |
} |
| 221 | 63 | return tags.getValidTags(); |
| 222 | |
} |
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
private void checkTag(int aLineNo, List<JavadocTag> aTags, String aTag, |
| 233 | |
Pattern aFormatPattern, String aFormat) |
| 234 | |
{ |
| 235 | 126 | if (aFormatPattern == null) { |
| 236 | 72 | return; |
| 237 | |
} |
| 238 | |
|
| 239 | 54 | int tagCount = 0; |
| 240 | 134 | for (int i = aTags.size() - 1; i >= 0; i--) { |
| 241 | 80 | final JavadocTag tag = aTags.get(i); |
| 242 | 80 | if (tag.getTagName().equals(aTag)) { |
| 243 | 40 | tagCount++; |
| 244 | 40 | if (!aFormatPattern.matcher(tag.getArg1()).find()) { |
| 245 | 15 | log(aLineNo, "type.tagFormat", "@" + aTag, aFormat); |
| 246 | |
} |
| 247 | |
} |
| 248 | |
} |
| 249 | 54 | if (tagCount == 0) { |
| 250 | 14 | log(aLineNo, "type.missingTag", "@" + aTag); |
| 251 | |
} |
| 252 | 54 | } |
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
private void checkTypeParamTag(final int aLineNo, |
| 262 | |
final List<JavadocTag> aTags, final String aTypeParamName) |
| 263 | |
{ |
| 264 | 3 | boolean found = false; |
| 265 | 18 | for (int i = aTags.size() - 1; i >= 0; i--) { |
| 266 | 15 | final JavadocTag tag = aTags.get(i); |
| 267 | 15 | if (tag.isParamTag() |
| 268 | |
&& (tag.getArg1() != null) |
| 269 | |
&& (tag.getArg1().indexOf("<" + aTypeParamName + ">") == 0)) |
| 270 | |
{ |
| 271 | 2 | found = true; |
| 272 | |
} |
| 273 | |
} |
| 274 | 3 | if (!found) { |
| 275 | 1 | log(aLineNo, "type.missingTag", |
| 276 | |
JavadocTagInfo.PARAM.getText() + " <" + aTypeParamName + ">"); |
| 277 | |
} |
| 278 | 3 | } |
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
private void checkUnusedTypeParamTags( |
| 286 | |
final List<JavadocTag> aTags, |
| 287 | |
final List<String> aTypeParamNames) |
| 288 | |
{ |
| 289 | 63 | final Pattern pattern = Utils.getPattern("\\s*<([^>]+)>.*"); |
| 290 | 156 | for (int i = aTags.size() - 1; i >= 0; i--) { |
| 291 | 93 | final JavadocTag tag = aTags.get(i); |
| 292 | 93 | if (tag.isParamTag()) { |
| 293 | |
|
| 294 | 6 | if (tag.getArg1() != null) { |
| 295 | |
|
| 296 | 6 | final Matcher matcher = pattern.matcher(tag.getArg1()); |
| 297 | 6 | String typeParamName = null; |
| 298 | |
|
| 299 | 6 | if (matcher.matches()) { |
| 300 | 6 | typeParamName = matcher.group(1).trim(); |
| 301 | 6 | if (!aTypeParamNames.contains(typeParamName)) { |
| 302 | 2 | log(tag.getLineNo(), tag.getColumnNo(), |
| 303 | |
"javadoc.unusedTag", |
| 304 | |
JavadocTagInfo.PARAM.getText(), |
| 305 | |
"<" + typeParamName + ">"); |
| 306 | |
} |
| 307 | |
} |
| 308 | |
else { |
| 309 | 0 | log(tag.getLineNo(), tag.getColumnNo(), |
| 310 | |
"javadoc.unusedTagGeneral"); |
| 311 | |
} |
| 312 | 6 | } |
| 313 | |
else { |
| 314 | 0 | log(tag.getLineNo(), tag.getColumnNo(), |
| 315 | |
"javadoc.unusedTagGeneral"); |
| 316 | |
} |
| 317 | |
} |
| 318 | |
} |
| 319 | 63 | } |
| 320 | |
} |