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