001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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.Arrays; 023import java.util.Optional; 024 025import com.puppycrawl.tools.checkstyle.api.DetailAST; 026import com.puppycrawl.tools.checkstyle.api.TokenTypes; 027import com.puppycrawl.tools.checkstyle.utils.CheckUtils; 028import com.puppycrawl.tools.checkstyle.utils.ScopeUtils; 029 030/** 031 * <p> 032 * Checks that method and {@code catch} parameter names conform to a format specified 033 * by the format property. The format is a 034 * {@link java.util.regex.Pattern regular expression} 035 * and defaults to 036 * <strong>^[a-z][a-zA-Z0-9]*$</strong>. 037 * </p> 038 * <p>The check has the following options:</p> 039 * <p><b>ignoreOverridden</b> - allows to skip methods with Override annotation from 040 * validation. Default values is <b>false</b> .</p> 041 * <p><b>accessModifiers</b> - access modifiers of methods which should to be checked. 042 * Default value is <b>public, protected, package, private</b> .</p> 043 * An example of how to configure the check: 044 * <pre> 045 * <module name="ParameterName"/> 046 * </pre> 047 * <p> 048 * An example of how to configure the check for names that begin with 049 * a lower case letter, followed by letters, digits, and underscores: 050 * </p> 051 * <pre> 052 * <module name="ParameterName"> 053 * <property name="format" value="^[a-z][_a-zA-Z0-9]+$"/> 054 * </module> 055 * </pre> 056 * <p> 057 * An example of how to configure the check to skip methods with Override annotation from 058 * validation: 059 * </p> 060 * <pre> 061 * <module name="ParameterName"> 062 * <property name="ignoreOverridden" value="true"/> 063 * </module> 064 * </pre> 065 * 066 */ 067public class ParameterNameCheck extends AbstractNameCheck { 068 069 /** 070 * Allows to skip methods with Override annotation from validation. 071 */ 072 private boolean ignoreOverridden; 073 074 /** Access modifiers of methods which should be checked. */ 075 private AccessModifier[] accessModifiers = { 076 AccessModifier.PUBLIC, 077 AccessModifier.PROTECTED, 078 AccessModifier.PACKAGE, 079 AccessModifier.PRIVATE, 080 }; 081 082 /** 083 * Creates a new {@code ParameterNameCheck} instance. 084 */ 085 public ParameterNameCheck() { 086 super("^[a-z][a-zA-Z0-9]*$"); 087 } 088 089 /** 090 * Sets whether to skip methods with Override annotation from validation. 091 * @param ignoreOverridden Flag for skipping methods with Override annotation. 092 */ 093 public void setIgnoreOverridden(boolean ignoreOverridden) { 094 this.ignoreOverridden = ignoreOverridden; 095 } 096 097 /** 098 * Sets access modifiers of methods which should be checked. 099 * @param accessModifiers access modifiers of methods which should be checked. 100 */ 101 public void setAccessModifiers(AccessModifier... accessModifiers) { 102 this.accessModifiers = 103 Arrays.copyOf(accessModifiers, accessModifiers.length); 104 } 105 106 @Override 107 public int[] getDefaultTokens() { 108 return getRequiredTokens(); 109 } 110 111 @Override 112 public int[] getAcceptableTokens() { 113 return getRequiredTokens(); 114 } 115 116 @Override 117 public int[] getRequiredTokens() { 118 return new int[] {TokenTypes.PARAMETER_DEF}; 119 } 120 121 @Override 122 protected boolean mustCheckName(DetailAST ast) { 123 boolean checkName = true; 124 if (ignoreOverridden && isOverriddenMethod(ast) 125 || ast.getParent().getType() == TokenTypes.LITERAL_CATCH 126 || CheckUtils.isReceiverParameter(ast) 127 || !matchAccessModifiers(getAccessModifier(ast))) { 128 checkName = false; 129 } 130 return checkName; 131 } 132 133 /** 134 * Returns the access modifier of the method/constructor at the specified AST. If 135 * the method is in an interface or annotation block, the access modifier is assumed 136 * to be public. 137 * 138 * @param ast the token of the method/constructor. 139 * @return the access modifier of the method/constructor. 140 */ 141 private static AccessModifier getAccessModifier(final DetailAST ast) { 142 final DetailAST params = ast.getParent(); 143 final DetailAST meth = params.getParent(); 144 AccessModifier accessModifier = AccessModifier.PRIVATE; 145 146 if (meth.getType() == TokenTypes.METHOD_DEF 147 || meth.getType() == TokenTypes.CTOR_DEF) { 148 if (ScopeUtils.isInInterfaceOrAnnotationBlock(ast)) { 149 accessModifier = AccessModifier.PUBLIC; 150 } 151 else { 152 final DetailAST modsToken = meth.findFirstToken(TokenTypes.MODIFIERS); 153 accessModifier = CheckUtils.getAccessModifierFromModifiersToken(modsToken); 154 } 155 } 156 157 return accessModifier; 158 } 159 160 /** 161 * Checks whether a method has the correct access modifier to be checked. 162 * @param accessModifier the access modifier of the method. 163 * @return whether the method matches the expected access modifier. 164 */ 165 private boolean matchAccessModifiers(final AccessModifier accessModifier) { 166 return Arrays.stream(accessModifiers).anyMatch(el -> el == accessModifier); 167 } 168 169 /** 170 * Checks whether a method is annotated with Override annotation. 171 * @param ast method parameter definition token. 172 * @return true if a method is annotated with Override annotation. 173 */ 174 private static boolean isOverriddenMethod(DetailAST ast) { 175 boolean overridden = false; 176 177 final DetailAST parent = ast.getParent().getParent(); 178 final Optional<DetailAST> annotation = 179 Optional.ofNullable(parent.getFirstChild().getFirstChild()); 180 181 if (annotation.isPresent() 182 && annotation.get().getType() == TokenTypes.ANNOTATION) { 183 final Optional<DetailAST> overrideToken = 184 Optional.ofNullable(annotation.get().findFirstToken(TokenTypes.IDENT)); 185 if (overrideToken.isPresent() && "Override".equals(overrideToken.get().getText())) { 186 overridden = true; 187 } 188 } 189 return overridden; 190 } 191 192}