Source code: com/puppycrawl/tools/checkstyle/checks/JavadocVariableCheck.java
1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2002 Oliver Burn
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ////////////////////////////////////////////////////////////////////////////////
19 package com.puppycrawl.tools.checkstyle.checks;
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.Scope;
25 import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
26 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
27
28 /**
29 * Checks that a variable has Javadoc comment.
30 * The scope to verify is specified using the {@link Scope} class and
31 * defaults to {@link Scope#PRIVATE}. To verify another scope,
32 * set property scope to one of the {@link Scope} constants.
33 * An example of how to configure the check is:
34 * </p>
35 * <pre>
36 * <module name="JavadocVariable"/>
37 * </pre>
38 * <p> An example of how to configure the check for the
39 * {@link Scope#PUBLIC} scope is:
40 *</p>
41 * <pre>
42 * <module name="JavadocVariable">
43 * <property name="scope" value="public"/>
44 * </module>
45 * </pre>
46 * @author <a href="mailto:checkstyle@puppycrawl.com">Oliver Burn</a>
47 * @version 1.0
48 */
49 public class JavadocVariableCheck
50 extends Check
51 {
52 /** the scope to check */
53 private Scope mScope = Scope.PRIVATE;
54
55 /**
56 * Sets the scope to check.
57 * @param aFrom string to get the scope from
58 */
59 public void setScope(String aFrom)
60 {
61 mScope = Scope.getInstance(aFrom);
62 }
63
64 /** @see com.puppycrawl.tools.checkstyle.api.Check */
65 public int[] getDefaultTokens()
66 {
67 return new int[] {TokenTypes.VARIABLE_DEF};
68 }
69
70 /** @see com.puppycrawl.tools.checkstyle.api.Check */
71 public void visitToken(DetailAST aAST)
72 {
73 if (!ScopeUtils.inCodeBlock(aAST)) {
74 final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS);
75 final Scope declaredScope = ScopeUtils.getScopeFromMods(mods);
76 final Scope variableScope =
77 ScopeUtils.inInterfaceBlock(aAST)
78 ? Scope.PUBLIC
79 : declaredScope;
80
81 if (variableScope.isIn(mScope)) {
82 final Scope surroundingScope =
83 ScopeUtils.getSurroundingScope(aAST);
84
85 if (surroundingScope.isIn(mScope)) {
86 final FileContents contents = getFileContents();
87 final String[] cmt =
88 contents.getJavadocBefore(aAST.getLineNo());
89
90 if (cmt == null) {
91 log(aAST.getLineNo(),
92 aAST.getColumnNo(),
93 "javadoc.missing");
94 }
95 }
96 }
97 }
98 }
99 }