Coverage Report - com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
MemberNameCheck
100%
11/11
100%
10/10
1.6
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2017 the original author or authors.
 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  
 
 20  
 package com.puppycrawl.tools.checkstyle.checks.naming;
 21  
 
 22  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 23  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 24  
 import com.puppycrawl.tools.checkstyle.utils.ScopeUtils;
 25  
 
 26  
 /**
 27  
  * <p>
 28  
  * Checks that instance variable names conform to a format specified
 29  
  * by the format property. The format is a
 30  
  * {@link java.util.regex.Pattern regular expression}
 31  
  * and defaults to
 32  
  * <strong>^[a-z][a-zA-Z0-9]*$</strong>.
 33  
  * </p>
 34  
  * <p>
 35  
  * An example of how to configure the check is:
 36  
  * </p>
 37  
  * <pre>
 38  
  * &lt;module name="MemberName"/&gt;
 39  
  * </pre>
 40  
  * <p>
 41  
  * An example of how to configure the check for names that begin with
 42  
  * &quot;m&quot;, followed by an upper case letter, and then letters and
 43  
  * digits is:
 44  
  * </p>
 45  
  * <pre>
 46  
  * &lt;module name="MemberName"&gt;
 47  
  *    &lt;property name="format" value="^m[A-Z][a-zA-Z0-9]*$"/&gt;
 48  
  * &lt;/module&gt;
 49  
  * </pre>
 50  
  * @author Rick Giles
 51  
  */
 52  
 public class MemberNameCheck
 53  
     extends AbstractAccessControlNameCheck {
 54  
     /** Creates a new {@code MemberNameCheck} instance. */
 55  
     public MemberNameCheck() {
 56  106
         super("^[a-z][a-zA-Z0-9]*$");
 57  106
     }
 58  
 
 59  
     @Override
 60  
     public int[] getDefaultTokens() {
 61  201
         return getRequiredTokens();
 62  
     }
 63  
 
 64  
     @Override
 65  
     public int[] getAcceptableTokens() {
 66  6
         return getRequiredTokens();
 67  
     }
 68  
 
 69  
     @Override
 70  
     public int[] getRequiredTokens() {
 71  409
         return new int[] {TokenTypes.VARIABLE_DEF};
 72  
     }
 73  
 
 74  
     @Override
 75  
     protected final boolean mustCheckName(DetailAST ast) {
 76  774
         final DetailAST modifiersAST =
 77  774
             ast.findFirstToken(TokenTypes.MODIFIERS);
 78  774
         final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
 79  
 
 80  1548
         return !isStatic && !ScopeUtils.isInInterfaceOrAnnotationBlock(ast)
 81  646
             && !ScopeUtils.isLocalVariableDef(ast)
 82  560
                 && shouldCheckInScope(modifiersAST);
 83  
     }
 84  
 }