001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.naming;
020
021import com.puppycrawl.tools.checkstyle.api.AnnotationUtility;
022import com.puppycrawl.tools.checkstyle.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.TokenTypes;
024
025/**
026 * <p>
027 * Checks that method names conform to a format specified
028 * by the format property. The format is a
029 * {@link java.util.regex.Pattern regular expression}
030 * and defaults to
031 * <strong>^[a-z][a-zA-Z0-9]*$</strong>.
032 * </p>
033 *
034 * <p>
035 * Also, checks if a method name has the same name as the residing class.
036 * The default is false (it is not allowed).  It is legal in Java to have
037 * method with the same name as a class.  As long as a return type is specified
038 * it is a method and not a constructor which it could be easily confused as.
039 * <h3>Does not check-style the name of an overriden methods</h3> because the developer does not
040 * have a choice in renaming such methods.
041 * </p>
042 *
043 * <p>
044 * An example of how to configure the check is:
045 * </p>
046 * <pre>
047 * &lt;module name="MethodName"/&gt;
048 * </pre>
049 * <p>
050 * An example of how to configure the check for names that begin with
051 * a lower case letter, followed by letters, digits, and underscores is:
052 * </p>
053 * <pre>
054 * &lt;module name="MethodName"&gt;
055 *    &lt;property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/&gt;
056 * &lt;/module&gt;
057 * </pre>
058 *
059 * <p>
060 * An example of how to configure the check to allow method names
061 * to be equal to the residing class name is:
062 * </p>
063 * <pre>
064 * &lt;module name="MethodName"&gt;
065 *    &lt;property name="allowClassName" value="true"/&gt;
066 * &lt;/module&gt;
067 * </pre>
068 * @author Oliver Burn
069 * @author Travis Schneeberger
070 * @author Utkarsh Srivastava
071 * @version 1.1
072 */
073public class MethodNameCheck
074    extends AbstractAccessControlNameCheck
075{
076    /**
077     * for allowing method name to be the same as the class name.
078     */
079    private boolean allowClassName;
080
081    /**
082     * {@link Override Override} annotation name.
083     */
084    private static final String OVERRIDE = "Override";
085
086    /**
087     * Canonical {@link Override Override} annotation name.
088     */
089    private static final String CANONICAL_OVERRIDE = "java.lang." + OVERRIDE;
090
091    /** Creates a new <code>MethodNameCheck</code> instance. */
092    public MethodNameCheck()
093    {
094        super("^[a-z][a-zA-Z0-9]*$");
095    }
096
097    @Override
098    public int[] getDefaultTokens()
099    {
100        return new int[] {TokenTypes.METHOD_DEF, };
101    }
102
103    @Override
104    public void visitToken(DetailAST ast)
105    {
106        if (!AnnotationUtility.containsAnnotation(ast, OVERRIDE)
107            && !AnnotationUtility.containsAnnotation(ast, CANONICAL_OVERRIDE))
108        {
109            super.visitToken(ast); // Will check the name against the format.
110        }
111
112        if (!allowClassName) {
113            final DetailAST method =
114                ast.findFirstToken(TokenTypes.IDENT);
115            //in all cases this will be the classDef type except anon inner
116            //with anon inner classes this will be the Literal_New keyword
117            final DetailAST classDefOrNew = ast.getParent().getParent();
118            final DetailAST classIdent =
119                classDefOrNew.findFirstToken(TokenTypes.IDENT);
120            // Following logic is to handle when a classIdent can not be
121            // found. This is when you have a Literal_New keyword followed
122            // a DOT, which is when you have:
123            // new Outclass.InnerInterface(x) { ... }
124            // Such a rare case, will not have the logic to handle parsing
125            // down the tree looking for the first ident.
126            if ((null != classIdent)
127                && method.getText().equals(classIdent.getText()))
128            {
129                log(method.getLineNo(), method.getColumnNo(),
130                    "method.name.equals.class.name", method.getText());
131            }
132        }
133    }
134
135    /**
136     * Sets the property for allowing a method to be the same name as a class.
137     * @param allowClassName true to allow false to disallow
138     */
139    public void setAllowClassName(boolean allowClassName)
140    {
141        this.allowClassName = allowClassName;
142    }
143}