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