Coverage Report - com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocVariableCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
JavadocVariableCheck
100%
22/22
100%
20/20
3
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2014  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  8
 public class JavadocVariableCheck
 37  
     extends Check
 38  
 {
 39  
     /** the scope to check */
 40  8
     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 aFrom)
 50  
     {
 51  4
         mScope = Scope.getInstance(aFrom);
 52  4
     }
 53  
 
 54  
     /**
 55  
      * Set the excludeScope.
 56  
      * @param aScope a <code>String</code> value
 57  
      */
 58  
     public void setExcludeScope(String aScope)
 59  
     {
 60  1
         mExcludeScope = Scope.getInstance(aScope);
 61  1
     }
 62  
 
 63  
     @Override
 64  
     public int[] getDefaultTokens()
 65  
     {
 66  8
         return new int[] {
 67  
             TokenTypes.VARIABLE_DEF,
 68  
             TokenTypes.ENUM_CONSTANT_DEF,
 69  
         };
 70  
     }
 71  
 
 72  
     @Override
 73  
     public void visitToken(DetailAST aAST)
 74  
     {
 75  164
         if (shouldCheck(aAST)) {
 76  99
             final FileContents contents = getFileContents();
 77  99
             final TextBlock cmt =
 78  
                 contents.getJavadocBefore(aAST.getLineNo());
 79  
 
 80  99
             if (cmt == null) {
 81  87
                 log(aAST, "javadoc.missing");
 82  
             }
 83  
         }
 84  164
     }
 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  164
         if (ScopeUtils.inCodeBlock(aAST)) {
 94  12
             return false;
 95  
         }
 96  
 
 97  
         final Scope scope;
 98  152
         if (aAST.getType() == TokenTypes.ENUM_CONSTANT_DEF) {
 99  7
             scope = Scope.PUBLIC;
 100  
         }
 101  
         else {
 102  145
             final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS);
 103  145
             final Scope declaredScope = ScopeUtils.getScopeFromMods(mods);
 104  145
             scope =
 105  
                 ScopeUtils.inInterfaceOrAnnotationBlock(aAST)
 106  
                     ? Scope.PUBLIC : declaredScope;
 107  
         }
 108  
 
 109  152
         final Scope surroundingScope = ScopeUtils.getSurroundingScope(aAST);
 110  
 
 111  152
         return scope.isIn(mScope) && surroundingScope.isIn(mScope)
 112  
             && ((mExcludeScope == null)
 113  
                 || !scope.isIn(mExcludeScope)
 114  
                 || !surroundingScope.isIn(mExcludeScope));
 115  
     }
 116  
 
 117  
 }