KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > javadoc > JavadocVariableCheck


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 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.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.Scope;
25 import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
26 import com.puppycrawl.tools.checkstyle.api.TextBlock;
27 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
28
29
30 /**
31  * Checks that a variable has Javadoc comment.
32  *
33  * @author Oliver Burn
34  * @version 1.0
35  */

36 public class JavadocVariableCheck
37     extends Check
38 {
39     /** the scope to check */
40     private Scope mScope = Scope.PRIVATE;
41
42     /** the visibility scope where Javadoc comments shouldn't be checked **/
43     private Scope mExcludeScope;
44
45     /**
46      * Sets the scope to check.
47      * @param aFrom string to get the scope from
48      */

49     public void setScope(String JavaDoc aFrom)
50     {
51         mScope = Scope.getInstance(aFrom);
52     }
53
54     /**
55      * Set the excludeScope.
56      * @param aScope a <code>String</code> value
57      */

58     public void setExcludeScope(String JavaDoc aScope)
59     {
60         mExcludeScope = Scope.getInstance(aScope);
61     }
62
63     /** {@inheritDoc} */
64     public int[] getDefaultTokens()
65     {
66         return new int[] {
67             TokenTypes.VARIABLE_DEF,
68             TokenTypes.ENUM_CONSTANT_DEF,
69         };
70     }
71
72     /** {@inheritDoc} */
73     public void visitToken(DetailAST aAST)
74     {
75         if (shouldCheck(aAST)) {
76             final FileContents contents = getFileContents();
77             final TextBlock cmt =
78                 contents.getJavadocBefore(aAST.getLineNo());
79
80             if (cmt == null) {
81                 log(aAST, "javadoc.missing");
82             }
83         }
84     }
85
86     /**
87      * Whether we should check this node.
88      * @param aAST a given node.
89      * @return whether we should check a given node.
90      */

91     private boolean shouldCheck(final DetailAST aAST)
92     {
93         if (ScopeUtils.inCodeBlock(aAST)) {
94             return false;
95         }
96
97         final Scope scope;
98         if (aAST.getType() == TokenTypes.ENUM_CONSTANT_DEF) {
99             scope = Scope.PUBLIC;
100         }
101         else {
102             final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS);
103             final Scope declaredScope = ScopeUtils.getScopeFromMods(mods);
104             scope =
105                 ScopeUtils.inInterfaceOrAnnotationBlock(aAST)
106                     ? Scope.PUBLIC : declaredScope;
107         }
108
109         final Scope surroundingScope = ScopeUtils.getSurroundingScope(aAST);
110
111         return scope.isIn(mScope) && surroundingScope.isIn(mScope)
112             && ((mExcludeScope == null)
113                 || !scope.isIn(mExcludeScope)
114                 || !surroundingScope.isIn(mExcludeScope));
115     }
116
117 }
118
Popular Tags