001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 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;
024
025/**
026* <p>
027 * Checks that method and <code>catch</code> parameter 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 * <p>The check has the following option:</p>
034 * <p><b>ignoreOverridden</b> - allows to skip methods with Override annotation from
035 * validation. Default values is <b>false</b> .</p>
036 * <p>
037 * An example of how to configure the check is:
038 * </p>
039 * <pre>
040 * &lt;module name="ParameterName"/&gt;
041 * </pre>
042 * <p>
043  * An example of how to configure the check for names that begin with
044 * a lower case letter, followed by letters, digits, and underscores is:
045 * </p>
046 * <pre>
047 * &lt;module name="ParameterName"&gt;
048 *    &lt;property name="format" value="^^[a-z](_?[a-zA-Z0-9]+)*$"/&gt;
049 * &lt;/module&gt;
050 * </pre>
051 * <p>
052 * An example of how to configure the check to skip methods with Override annotation from
053 * validation:
054 * </p>
055 * <pre>
056 * &lt;module name="ParameterName"&gt;
057 *    &lt;property name="ignoreOverridden" value="true"/&gt;
058 * &lt;/module&gt;
059 * </pre>
060 *
061 * @author Oliver Burn
062 * @author Andrei Selkin
063 */
064public class ParameterNameCheck
065    extends AbstractNameCheck {
066
067    /**
068     * Allows to skip methods with Override annotation from validation.
069     */
070    private boolean ignoreOverridden;
071
072    /**
073     * Creates a new {@code ParameterNameCheck} instance.
074     */
075    public ParameterNameCheck() {
076        super("^[a-z][a-zA-Z0-9]*$");
077    }
078
079    /**
080     * Sets whether to skip methods with Override annotation from validation.
081     *
082     * @param ignoreOverridden Flag for skipping methods with Override annotation.
083     */
084    public void setIgnoreOverridden(boolean ignoreOverridden) {
085        this.ignoreOverridden = ignoreOverridden;
086    }
087
088    @Override
089    public int[] getDefaultTokens() {
090        return getAcceptableTokens();
091    }
092
093    @Override
094    public int[] getAcceptableTokens() {
095        return new int[] {TokenTypes.PARAMETER_DEF};
096    }
097
098    @Override
099    public int[] getRequiredTokens() {
100        return getAcceptableTokens();
101    }
102
103    @Override
104    protected boolean mustCheckName(DetailAST ast) {
105        boolean checkName = true;
106        if (ignoreOverridden && isOverriddenMethod(ast)
107                || ast.getParent().getType() == TokenTypes.LITERAL_CATCH) {
108            checkName = false;
109        }
110        return checkName;
111    }
112
113    /**
114     * Checks whether a method is annotated with Override annotation.
115     * @param ast method parameter definition token.
116     * @return true if a method is annotated with Override annotation.
117     */
118    private static boolean isOverriddenMethod(DetailAST ast) {
119        boolean overridden = false;
120        final DetailAST parent = ast.getParent().getParent();
121        if (parent.getFirstChild().getFirstChild() != null) {
122            final DetailAST annotation = parent.getFirstChild().getFirstChild();
123            if (annotation.getType() == TokenTypes.ANNOTATION) {
124                final DetailAST overrideToken = annotation.findFirstToken(TokenTypes.IDENT);
125                if ("Override".equals(overrideToken.getText())) {
126                    overridden = true;
127                }
128            }
129        }
130        return overridden;
131    }
132}