001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 the original author or authors.
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///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.naming;
021
022import com.puppycrawl.tools.checkstyle.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.TokenTypes;
024import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil;
025
026/**
027 * <div>
028 * Checks that method names conform to a specified pattern.
029 * </div>
030 *
031 * <p>Also, checks if a method name has the same name as the residing class.
032 * The default is false (it is not allowed). It is legal in Java to have
033 * method with the same name as a class. As long as a return type is specified
034 * it is a method and not a constructor which it could be easily confused as.
035 * Does not check-style the name of an overridden methods because the developer does not
036 * have a choice in renaming such methods.
037 * </p>
038 *
039 * <ul>
040 * <li>
041 * Property {@code allowClassName} - Control whether to allow a method name to have the same name
042 * as the enclosing class name. Setting this property {@code false} helps to avoid
043 * confusion between constructors and methods.
044 * Type is {@code boolean}.
045 * Default value is {@code false}.
046 * </li>
047 * <li>
048 * Property {@code applyToPackage} - Control if check should apply to package-private members.
049 * Type is {@code boolean}.
050 * Default value is {@code true}.
051 * </li>
052 * <li>
053 * Property {@code applyToPrivate} - Control if check should apply to private members.
054 * Type is {@code boolean}.
055 * Default value is {@code true}.
056 * </li>
057 * <li>
058 * Property {@code applyToProtected} - Control if check should apply to protected members.
059 * Type is {@code boolean}.
060 * Default value is {@code true}.
061 * </li>
062 * <li>
063 * Property {@code applyToPublic} - Control if check should apply to public members.
064 * Type is {@code boolean}.
065 * Default value is {@code true}.
066 * </li>
067 * <li>
068 * Property {@code format} - Sets the pattern to match valid identifiers.
069 * Type is {@code java.util.regex.Pattern}.
070 * Default value is {@code "^[a-z][a-zA-Z0-9]*$"}.
071 * </li>
072 * </ul>
073 *
074 * <p>
075 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
076 * </p>
077 *
078 * <p>
079 * Violation Message Keys:
080 * </p>
081 * <ul>
082 * <li>
083 * {@code method.name.equals.class.name}
084 * </li>
085 * <li>
086 * {@code name.invalidPattern}
087 * </li>
088 * </ul>
089 *
090 * @since 3.0
091 */
092public class MethodNameCheck
093    extends AbstractAccessControlNameCheck {
094
095    /**
096     * A key is pointing to the warning message text in "messages.properties"
097     * file.
098     */
099    public static final String MSG_KEY = "method.name.equals.class.name";
100
101    /**
102     * Control whether to allow a method name to have the same name as the enclosing class name.
103     * Setting this property {@code false} helps to avoid confusion
104     * between constructors and methods.
105     */
106    private boolean allowClassName;
107
108    /** Creates a new {@code MethodNameCheck} instance. */
109    public MethodNameCheck() {
110        super("^[a-z][a-zA-Z0-9]*$");
111    }
112
113    @Override
114    public int[] getDefaultTokens() {
115        return getRequiredTokens();
116    }
117
118    @Override
119    public int[] getAcceptableTokens() {
120        return getRequiredTokens();
121    }
122
123    @Override
124    public int[] getRequiredTokens() {
125        return new int[] {TokenTypes.METHOD_DEF, };
126    }
127
128    @Override
129    public void visitToken(DetailAST ast) {
130        if (!AnnotationUtil.hasOverrideAnnotation(ast)) {
131            // Will check the name against the format.
132            super.visitToken(ast);
133        }
134
135        if (!allowClassName) {
136            final DetailAST method =
137                ast.findFirstToken(TokenTypes.IDENT);
138            // in all cases this will be the classDef type except anon inner
139            // with anon inner classes this will be the Literal_New keyword
140            final DetailAST classDefOrNew = ast.getParent().getParent();
141            final DetailAST classIdent =
142                classDefOrNew.findFirstToken(TokenTypes.IDENT);
143            // Following logic is to handle when a classIdent can not be
144            // found. This is when you have a Literal_New keyword followed
145            // a DOT, which is when you have:
146            // new Outclass.InnerInterface(x) { ... }
147            // Such a rare case, will not have the logic to handle parsing
148            // down the tree looking for the first ident.
149            if (classIdent != null
150                && method.getText().equals(classIdent.getText())) {
151                log(method, MSG_KEY, method.getText());
152            }
153        }
154    }
155
156    /**
157     * Setter to control whether to allow a method name to have the same name
158     * as the enclosing class name. Setting this property {@code false}
159     * helps to avoid confusion between constructors and methods.
160     *
161     * @param allowClassName true to allow false to disallow
162     * @since 5.0
163     */
164    public void setAllowClassName(boolean allowClassName) {
165        this.allowClassName = allowClassName;
166    }
167
168}