Coverage Report - com.puppycrawl.tools.checkstyle.checks.naming.MethodNameCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
MethodNameCheck
100%
13/13
100%
6/6
1.75
 
 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.naming;
 20  
 
 21  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 22  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 23  
 
 24  
 /**
 25  
  * <p>
 26  
  * Checks that method names conform to a format specified
 27  
  * by the format property. The format is a
 28  
  * {@link java.util.regex.Pattern regular expression}
 29  
  * and defaults to
 30  
  * <strong>^[a-z][a-zA-Z0-9]*$</strong>.
 31  
  * </p>
 32  
  *
 33  
  * <p>
 34  
  * Also, checks if a method name has the same name as the residing class.
 35  
  * The default is false (it is not allowed).  It is legal in Java to have
 36  
  * method with the same name as a class.  As long as a return type is specified
 37  
  * it is a method and not a constructor which it could be easily confused as.
 38  
  * </p>
 39  
  *
 40  
  * <p>
 41  
  * An example of how to configure the check is:
 42  
  * </p>
 43  
  * <pre>
 44  
  * &lt;module name="MethodName"/&gt;
 45  
  * </pre>
 46  
  * <p>
 47  
  * An example of how to configure the check for names that begin with
 48  
  * a lower case letter, followed by letters, digits, and underscores is:
 49  
  * </p>
 50  
  * <pre>
 51  
  * &lt;module name="MethodName"&gt;
 52  
  *    &lt;property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/&gt;
 53  
  * &lt;/module&gt;
 54  
  * </pre>
 55  
  *
 56  
  * <p>
 57  
  * An example of how to configure the check to allow method names
 58  
  * to be equal to the residing class name is:
 59  
  * </p>
 60  
  * <pre>
 61  
  * &lt;module name="MethodName"&gt;
 62  
  *    &lt;property name="allowClassName" value="true"/&gt;
 63  
  * &lt;/module&gt;
 64  
  * </pre>
 65  
  * @author Oliver Burn
 66  
  * @author Travis Schneeberger
 67  
  * @version 1.1
 68  
  */
 69  
 public class MethodNameCheck
 70  
     extends AbstractAccessControlNameCheck
 71  
 {
 72  
     /**
 73  
      * for allowing method name to be the same as the class name.
 74  
      */
 75  
     private boolean mAllowClassName;
 76  
 
 77  
     /** Creates a new <code>MethodNameCheck</code> instance. */
 78  
     public MethodNameCheck()
 79  
     {
 80  5
         super("^[a-z][a-zA-Z0-9]*$");
 81  5
     }
 82  
 
 83  
     @Override
 84  
     public int[] getDefaultTokens()
 85  
     {
 86  5
         return new int[] {TokenTypes.METHOD_DEF, };
 87  
     }
 88  
 
 89  
     @Override
 90  
     public void visitToken(DetailAST aAst)
 91  
     {
 92  34
         super.visitToken(aAst); // Will check the name against the format.
 93  
 
 94  34
         if (!mAllowClassName) {
 95  18
             final DetailAST method =
 96  
                 aAst.findFirstToken(TokenTypes.IDENT);
 97  
             //in all cases this will be the classDef type except anon inner
 98  
             //with anon inner classes this will be the Literal_New keyword
 99  18
             final DetailAST classDefOrNew = aAst.getParent().getParent();
 100  18
             final DetailAST classIdent =
 101  
                 classDefOrNew.findFirstToken(TokenTypes.IDENT);
 102  
             // Following logic is to handle when a classIdent can not be
 103  
             // found. This is when you have a Literal_New keyword followed
 104  
             // a DOT, which is when you have:
 105  
             // new Outclass.InnerInterface(x) { ... }
 106  
             // Such a rare case, will not have the logic to handle parsing
 107  
             // down the tree looking for the first ident.
 108  18
             if ((null != classIdent)
 109  
                 && method.getText().equals(classIdent.getText()))
 110  
             {
 111  5
                 log(method.getLineNo(), method.getColumnNo(),
 112  
                     "method.name.equals.class.name", method.getText());
 113  
             }
 114  
         }
 115  34
     }
 116  
 
 117  
     /**
 118  
      * Sets the property for allowing a method to be the same name as a class.
 119  
      * @param aAllowClassName true to allow false to disallow
 120  
      */
 121  
     public void setAllowClassName(boolean aAllowClassName)
 122  
     {
 123  2
         mAllowClassName = aAllowClassName;
 124  2
     }
 125  
 }