Coverage Report - com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
ConstantNameCheck
100%
12/12
86%
19/22
4
 
 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  
 
 20  
 package com.puppycrawl.tools.checkstyle.checks.naming;
 21  
 
 22  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 23  
 import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
 24  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 25  
 
 26  
 /**
 27  
  * <p>
 28  
  * Checks that constant names conform to a format specified
 29  
  * by the format property.
 30  
  * A <em>constant</em> is a <strong>static</strong> and <strong>final</strong>
 31  
  * field or an interface/annotation field, except
 32  
  * <strong>serialVersionUID</strong> and <strong>serialPersistentFields
 33  
  * </strong>.  The format is a regular expression
 34  
  * and defaults to <strong>^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$</strong>.
 35  
  * </p>
 36  
  * <p>
 37  
  * An example of how to configure the check is:
 38  
  * </p>
 39  
  * <pre>
 40  
  * &lt;module name="ConstantName"/&gt;
 41  
  * </pre>
 42  
  *
 43  
  * <p>
 44  
  * An example of how to configure the check for names that are only upper case
 45  
  * letters and digits is:
 46  
  * </p>
 47  
  * <pre>
 48  
  * &lt;module name="ConstantName"&gt;
 49  
  *    &lt;property name="format" value="^[A-Z][A-Z0-9]*$"/&gt;
 50  
  * &lt;/module&gt;
 51  
  * </pre>
 52  
  *
 53  
  *
 54  
  * @author Rick Giles
 55  
  * @version 1.0
 56  
  */
 57  
 public class ConstantNameCheck
 58  
     extends AbstractAccessControlNameCheck
 59  
 {
 60  
     /** Creates a new <code>ConstantNameCheck</code> instance. */
 61  
     public ConstantNameCheck()
 62  
     {
 63  25
         super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$");
 64  25
     }
 65  
 
 66  
     @Override
 67  
     public int[] getDefaultTokens()
 68  
     {
 69  23
         return new int[] {TokenTypes.VARIABLE_DEF};
 70  
     }
 71  
 
 72  
     @Override
 73  
     protected final boolean mustCheckName(DetailAST aAST)
 74  
     {
 75  383
         boolean retVal = false;
 76  
 
 77  383
         final DetailAST modifiersAST =
 78  
             aAST.findFirstToken(TokenTypes.MODIFIERS);
 79  383
         final boolean isStatic = (modifiersAST != null)
 80  
             && modifiersAST.branchContains(TokenTypes.LITERAL_STATIC);
 81  383
         final boolean isFinal = (modifiersAST != null)
 82  
             && modifiersAST.branchContains(TokenTypes.FINAL);
 83  
 
 84  383
         if ((isStatic  && isFinal && shouldCheckInScope(modifiersAST))
 85  
             || ScopeUtils.inInterfaceOrAnnotationBlock(aAST))
 86  
         {
 87  
             // Handle the serialVersionUID and serialPersistentFields  constants
 88  
             // which are used for Serialization. Cannot enforce rules on it. :-)
 89  72
             final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT);
 90  72
             if ((nameAST != null)
 91  
                 && !("serialVersionUID".equals(nameAST.getText()))
 92  
                 && !("serialPersistentFields".equals(nameAST.getText())))
 93  
             {
 94  70
                 retVal = true;
 95  
             }
 96  
         }
 97  
 
 98  383
         return retVal;
 99  
     }
 100  
 }