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 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>
034 * An example of how to configure the check is:
035 * </p>
036 * <pre>
037 * &lt;module name="ParameterName"/&gt;
038 * </pre>
039 * <p>
040  * An example of how to configure the check for names that begin with
041 * a lower case letter, followed by letters, digits, and underscores is:
042 * </p>
043 * <pre>
044 * &lt;module name="ParameterName"&gt;
045 *    &lt;property name="format" value="^^[a-z](_?[a-zA-Z0-9]+)*$"/&gt;
046 * &lt;/module&gt;
047 * </pre>
048 *
049 * @author Oliver Burn
050 */
051public class ParameterNameCheck
052    extends AbstractNameCheck {
053    /**
054     * Creates a new {@code ParameterNameCheck} instance.
055     */
056    public ParameterNameCheck() {
057        super("^[a-z][a-zA-Z0-9]*$");
058    }
059
060    @Override
061    public int[] getDefaultTokens() {
062        return getAcceptableTokens();
063    }
064
065    @Override
066    public int[] getAcceptableTokens() {
067        return new int[] {TokenTypes.PARAMETER_DEF};
068    }
069
070    @Override
071    public int[] getRequiredTokens() {
072        return getAcceptableTokens();
073    }
074
075    @Override
076    protected boolean mustCheckName(DetailAST ast) {
077        return ast.getParent().getType() != TokenTypes.LITERAL_CATCH;
078    }
079}