Issue #3675: Replace Scope with AccessModifier in ParameterNameCheck to avoid wrong scopes comparison
This commit is contained in:
parent
f59e19992c
commit
f91b1af3d7
|
|
@ -121,7 +121,10 @@
|
|||
<module name="ForbidCertainImports">
|
||||
<property name="packageNameRegexp" value=".+\.checkstyle\.api.*|.+\.checkstyle\.utils.*"/>
|
||||
<property name="forbiddenImportsRegexp" value=".+\.checks\..+"/>
|
||||
<property name="forbiddenImportsExcludesRegexp" value=""/>
|
||||
<!-- AccessModifier is in util package (should be moved to api package) to disallow
|
||||
its usage by API clients till https://github.com/checkstyle/checkstyle/issues/3511-->
|
||||
<property name="forbiddenImportsExcludesRegexp"
|
||||
value="^com.puppycrawl.tools.checkstyle.checks.naming.AccessModifier$"/>
|
||||
</module>
|
||||
<module name="LineLengthExtended">
|
||||
<property name="max" value="100"/>
|
||||
|
|
|
|||
|
|
@ -61,6 +61,10 @@
|
|||
<allow pkg="java.text"/>
|
||||
<allow class="com.puppycrawl.tools.checkstyle.grammars.CommentListener"
|
||||
local-only="true"/>
|
||||
<!-- AccessModifier is in util package (should be moved to api package) to disallow
|
||||
its usage by API clients till https://github.com/checkstyle/checkstyle/issues/3511-->
|
||||
<allow class="com.puppycrawl.tools.checkstyle.checks.naming.AccessModifier"
|
||||
local-only="true"/>
|
||||
<allow class="com.puppycrawl.tools.checkstyle.grammars.GeneratedJavaTokenTypes"
|
||||
local-only="true"/>
|
||||
<allow class="com.puppycrawl.tools.checkstyle.Utils"
|
||||
|
|
|
|||
|
|
@ -34,9 +34,9 @@ import com.puppycrawl.tools.checkstyle.api.Configuration;
|
|||
public class ParameterNameTest extends BaseCheckTestSupport {
|
||||
|
||||
private static final String MSG_KEY = "name.invalidPattern";
|
||||
private static String privFormat;
|
||||
private static String genealFormat;
|
||||
private static String pubFormat;
|
||||
private static Configuration privConfig;
|
||||
private static Configuration generalConfig;
|
||||
private static Configuration pubConfig;
|
||||
|
||||
@Override
|
||||
|
|
@ -51,34 +51,44 @@ public class ParameterNameTest extends BaseCheckTestSupport {
|
|||
|
||||
Assert.assertEquals(configs.size(), 2);
|
||||
|
||||
privConfig = configs.get(0);
|
||||
Assert.assertEquals(privConfig.getAttribute("excludeScope"), "public");
|
||||
privFormat = privConfig.getAttribute("format");
|
||||
generalConfig = configs.get(0);
|
||||
Assert.assertEquals(generalConfig.getAttribute("accessModifiers"),
|
||||
"protected, package, private");
|
||||
genealFormat = generalConfig.getAttribute("format");
|
||||
|
||||
pubConfig = configs.get(1);
|
||||
Assert.assertEquals(pubConfig.getAttribute("scope"), "public");
|
||||
Assert.assertEquals(pubConfig.getAttribute("accessModifiers"), "public");
|
||||
pubFormat = pubConfig.getAttribute("format");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void privParameterNameTest() throws Exception {
|
||||
public void generalParameterNameTest() throws Exception {
|
||||
|
||||
final String[] expected = {
|
||||
"8:21: " + getCheckMessage(privConfig.getMessages(), MSG_KEY, "$arg1", privFormat),
|
||||
"9:21: " + getCheckMessage(privConfig.getMessages(), MSG_KEY, "ar$g2", privFormat),
|
||||
"10:21: " + getCheckMessage(privConfig.getMessages(), MSG_KEY, "arg3$", privFormat),
|
||||
"11:21: " + getCheckMessage(privConfig.getMessages(), MSG_KEY, "a_rg4", privFormat),
|
||||
"12:21: " + getCheckMessage(privConfig.getMessages(), MSG_KEY, "_arg5", privFormat),
|
||||
"13:21: " + getCheckMessage(privConfig.getMessages(), MSG_KEY, "arg6_", privFormat),
|
||||
"14:21: " + getCheckMessage(privConfig.getMessages(), MSG_KEY, "aArg7", privFormat),
|
||||
"15:21: " + getCheckMessage(privConfig.getMessages(), MSG_KEY, "aArg8", privFormat),
|
||||
"16:21: " + getCheckMessage(privConfig.getMessages(), MSG_KEY, "aar_g", privFormat),
|
||||
"8:21: "
|
||||
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "$arg1", genealFormat),
|
||||
"9:21: "
|
||||
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "ar$g2", genealFormat),
|
||||
"10:21: "
|
||||
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "arg3$", genealFormat),
|
||||
"11:21: "
|
||||
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "a_rg4", genealFormat),
|
||||
"12:21: "
|
||||
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "_arg5", genealFormat),
|
||||
"13:21: "
|
||||
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "arg6_", genealFormat),
|
||||
"14:21: "
|
||||
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "aArg7", genealFormat),
|
||||
"15:21: "
|
||||
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "aArg8", genealFormat),
|
||||
"16:21: "
|
||||
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "aar_g", genealFormat),
|
||||
};
|
||||
|
||||
final String filePath = getPath("InputParameterNameSimplePriv.java");
|
||||
final String filePath = getPath("InputParameterNameSimpleGeneral.java");
|
||||
|
||||
final Integer[] warnList = getLinesWithWarn(filePath);
|
||||
verify(privConfig, filePath, expected, warnList);
|
||||
verify(generalConfig, filePath, expected, warnList);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@ import org.apache.commons.beanutils.converters.IntegerConverter;
|
|||
import org.apache.commons.beanutils.converters.LongConverter;
|
||||
import org.apache.commons.beanutils.converters.ShortConverter;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.checks.naming.AccessModifier;
|
||||
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
|
||||
|
||||
/**
|
||||
|
|
@ -54,6 +55,10 @@ import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
|
|||
*/
|
||||
public class AutomaticBean
|
||||
implements Configurable, Contextualizable {
|
||||
|
||||
/** Comma separator for StringTokenizer. */
|
||||
private static final String COMMA_SEPARATOR = ",";
|
||||
|
||||
/** The configuration of this bean. */
|
||||
private Configuration configuration;
|
||||
|
||||
|
|
@ -130,6 +135,7 @@ public class AutomaticBean
|
|||
cub.register(new ServerityLevelConverter(), SeverityLevel.class);
|
||||
cub.register(new ScopeConverter(), Scope.class);
|
||||
cub.register(new UriConverter(), URI.class);
|
||||
cub.register(new RelaxedAccessModifierArrayConverter(), AccessModifier[].class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -328,7 +334,7 @@ public class AutomaticBean
|
|||
public Object convert(Class type, Object value) {
|
||||
// Convert to a String and trim it for the tokenizer.
|
||||
final StringTokenizer tokenizer = new StringTokenizer(
|
||||
value.toString().trim(), ",");
|
||||
value.toString().trim(), COMMA_SEPARATOR);
|
||||
final List<String> result = new ArrayList<>();
|
||||
|
||||
while (tokenizer.hasMoreTokens()) {
|
||||
|
|
@ -339,4 +345,28 @@ public class AutomaticBean
|
|||
return result.toArray(new String[result.size()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A converter that converts strings to {@link AccessModifier}.
|
||||
* This implementation does not care whether the array elements contain characters like '_'.
|
||||
* The normal {@link ArrayConverter} class has problems with this character.
|
||||
*/
|
||||
private static class RelaxedAccessModifierArrayConverter implements Converter {
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
@Override
|
||||
public Object convert(Class type, Object value) {
|
||||
// Converts to a String and trims it for the tokenizer.
|
||||
final StringTokenizer tokenizer = new StringTokenizer(
|
||||
value.toString().trim(), COMMA_SEPARATOR);
|
||||
final List<AccessModifier> result = new ArrayList<>();
|
||||
|
||||
while (tokenizer.hasMoreTokens()) {
|
||||
final String token = tokenizer.nextToken();
|
||||
result.add(AccessModifier.getInstance(token.trim()));
|
||||
}
|
||||
|
||||
return result.toArray(new AccessModifier[result.size()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,63 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// checkstyle: Checks Java source code for adherence to a set of rules.
|
||||
// Copyright (C) 2001-2016 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package com.puppycrawl.tools.checkstyle.checks.naming;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* This enum represents access modifiers.
|
||||
* Access modifiers names are taken from JLS:
|
||||
* https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6
|
||||
*
|
||||
* @author Andrei Selkin
|
||||
*/
|
||||
public enum AccessModifier {
|
||||
/** Public access modifier. */
|
||||
PUBLIC,
|
||||
/** Protected access modifier. */
|
||||
PROTECTED,
|
||||
/** Package access modifier. */
|
||||
PACKAGE,
|
||||
/** Private access modifier. */
|
||||
PRIVATE;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getName();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name().toLowerCase(Locale.ENGLISH);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method which returns an AccessModifier instance that corresponds to the
|
||||
* given access modifier name represented as a {@link String}.
|
||||
* The access modifier name can be formatted both as lower case or upper case string.
|
||||
* For example, passing PACKAGE or package as a modifier name
|
||||
* will return {@link AccessModifier#PACKAGE}.
|
||||
*
|
||||
* @param modifierName access modifier name represented as a {@link String}.
|
||||
* @return the AccessModifier associated with given access modifier name.
|
||||
*/
|
||||
public static AccessModifier getInstance(String modifierName) {
|
||||
return valueOf(AccessModifier.class, modifierName.trim().toUpperCase(Locale.ENGLISH));
|
||||
}
|
||||
}
|
||||
|
|
@ -19,16 +19,17 @@
|
|||
|
||||
package com.puppycrawl.tools.checkstyle.checks.naming;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
import com.puppycrawl.tools.checkstyle.api.Scope;
|
||||
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
||||
import com.puppycrawl.tools.checkstyle.utils.CheckUtils;
|
||||
import com.puppycrawl.tools.checkstyle.utils.ScopeUtils;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* <p>
|
||||
* Checks that method and <code>catch</code> parameter names conform to a format specified
|
||||
* by the format property. The format is a
|
||||
* {@link java.util.regex.Pattern regular expression}
|
||||
|
|
@ -38,19 +39,15 @@ import com.puppycrawl.tools.checkstyle.utils.ScopeUtils;
|
|||
* <p>The check has the following options:</p>
|
||||
* <p><b>ignoreOverridden</b> - allows to skip methods with Override annotation from
|
||||
* validation. Default values is <b>false</b> .</p>
|
||||
* <p><b>scope</b> - visibility scope of methods to be checked.
|
||||
* Default value is <b>anoninner</b> .</p>
|
||||
* <p><b>excludeScope</b> - visibility scope of methods not to be checked.
|
||||
* Default value is <b>null</b> .</p>
|
||||
* <p>
|
||||
* An example of how to configure the check is:
|
||||
* </p>
|
||||
* <p><b>accessModifiers</b> - access modifiers of methods which should to be checked.
|
||||
* Default value is <b>public, protected, package, private</b> .</p>
|
||||
* An example of how to configure the check:
|
||||
* <pre>
|
||||
* <module name="ParameterName"/>
|
||||
* </pre>
|
||||
* <p>
|
||||
* An example of how to configure the check for names that begin with
|
||||
* a lower case letter, followed by letters, digits, and underscores is:
|
||||
* An example of how to configure the check for names that begin with
|
||||
* a lower case letter, followed by letters, digits, and underscores:
|
||||
* </p>
|
||||
* <pre>
|
||||
* <module name="ParameterName">
|
||||
|
|
@ -70,19 +67,17 @@ import com.puppycrawl.tools.checkstyle.utils.ScopeUtils;
|
|||
* @author Oliver Burn
|
||||
* @author Andrei Selkin
|
||||
*/
|
||||
public class ParameterNameCheck
|
||||
extends AbstractNameCheck {
|
||||
public class ParameterNameCheck extends AbstractNameCheck {
|
||||
|
||||
/**
|
||||
* Allows to skip methods with Override annotation from validation.
|
||||
*/
|
||||
private boolean ignoreOverridden;
|
||||
|
||||
/** The visibility scope where methods are checked. */
|
||||
private Scope scope = Scope.ANONINNER;
|
||||
|
||||
/** The visibility scope where methods shouldn't be checked. */
|
||||
private Scope excludeScope;
|
||||
/** Access modifiers of methods which should be checked. */
|
||||
private AccessModifier[] accessModifiers = Stream.of(AccessModifier.PUBLIC,
|
||||
AccessModifier.PROTECTED, AccessModifier.PACKAGE, AccessModifier.PRIVATE)
|
||||
.toArray(AccessModifier[]::new);
|
||||
|
||||
/**
|
||||
* Creates a new {@code ParameterNameCheck} instance.
|
||||
|
|
@ -93,7 +88,6 @@ public class ParameterNameCheck
|
|||
|
||||
/**
|
||||
* Sets whether to skip methods with Override annotation from validation.
|
||||
*
|
||||
* @param ignoreOverridden Flag for skipping methods with Override annotation.
|
||||
*/
|
||||
public void setIgnoreOverridden(boolean ignoreOverridden) {
|
||||
|
|
@ -101,19 +95,12 @@ public class ParameterNameCheck
|
|||
}
|
||||
|
||||
/**
|
||||
* Set the scope.
|
||||
* @param from a scope.
|
||||
* Sets access modifiers of methods which should be checked.
|
||||
* @param accessModifiers access modifiers of methods which should be checked.
|
||||
*/
|
||||
public void setScope(Scope from) {
|
||||
scope = from;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the excludeScope.
|
||||
* @param excludeScope a scope.
|
||||
*/
|
||||
public void setExcludeScope(Scope excludeScope) {
|
||||
this.excludeScope = excludeScope;
|
||||
public void setAccessModifiers(AccessModifier... accessModifiers) {
|
||||
this.accessModifiers =
|
||||
Arrays.copyOf(accessModifiers, accessModifiers.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -134,53 +121,49 @@ public class ParameterNameCheck
|
|||
@Override
|
||||
protected boolean mustCheckName(DetailAST ast) {
|
||||
boolean checkName = true;
|
||||
final boolean isDefault = scope == Scope.ANONINNER && excludeScope == null;
|
||||
|
||||
if (ignoreOverridden && isOverriddenMethod(ast)
|
||||
|| ast.getParent().getType() == TokenTypes.LITERAL_CATCH
|
||||
|| CheckUtils.isReceiverParameter(ast)
|
||||
|| !isDefault && !matchScope(calculateScope(ast))) {
|
||||
|| !matchAccessModifiers(getAccessModifier(ast))) {
|
||||
checkName = false;
|
||||
}
|
||||
return checkName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the scope for the method/constructor at the specified AST. If
|
||||
* the method is in an interface or annotation block, the scope is assumed
|
||||
* Returns the access modifier of the method/constructor at the specified AST. If
|
||||
* the method is in an interface or annotation block, the access modifier is assumed
|
||||
* to be public.
|
||||
*
|
||||
* @param ast the token of the method/constructor
|
||||
* @return the scope of the method/constructor
|
||||
* @param ast the token of the method/constructor.
|
||||
* @return the access modifier of the method/constructor.
|
||||
*/
|
||||
private static Scope calculateScope(final DetailAST ast) {
|
||||
private static AccessModifier getAccessModifier(final DetailAST ast) {
|
||||
final DetailAST params = ast.getParent();
|
||||
final DetailAST meth = params.getParent();
|
||||
Scope scope = Scope.PRIVATE;
|
||||
AccessModifier accessModifier = AccessModifier.PRIVATE;
|
||||
|
||||
if (meth.getType() == TokenTypes.METHOD_DEF
|
||||
|| meth.getType() == TokenTypes.CTOR_DEF) {
|
||||
|| meth.getType() == TokenTypes.CTOR_DEF) {
|
||||
if (ScopeUtils.isInInterfaceOrAnnotationBlock(ast)) {
|
||||
scope = Scope.PUBLIC;
|
||||
accessModifier = AccessModifier.PUBLIC;
|
||||
}
|
||||
else {
|
||||
final DetailAST mods = meth.findFirstToken(TokenTypes.MODIFIERS);
|
||||
scope = ScopeUtils.getScopeFromMods(mods);
|
||||
final DetailAST modsToken = meth.findFirstToken(TokenTypes.MODIFIERS);
|
||||
accessModifier = CheckUtils.getAccessModifierFromModifiersToken(modsToken);
|
||||
}
|
||||
}
|
||||
|
||||
return scope;
|
||||
return accessModifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a method has the correct scope to be checked.
|
||||
* @param nodeScope the scope of the method
|
||||
* @return whether the method matches the expected scope
|
||||
* Checks whether a method has the correct access modifier to be checked.
|
||||
* @param accessModifier the access modifier of the method.
|
||||
* @return whether the method matches the expected access modifier.
|
||||
*/
|
||||
private boolean matchScope(final Scope nodeScope) {
|
||||
return nodeScope.isIn(scope)
|
||||
&& (excludeScope == null
|
||||
|| !nodeScope.isIn(excludeScope));
|
||||
private boolean matchAccessModifiers(final AccessModifier accessModifier) {
|
||||
return Arrays.stream(accessModifiers).anyMatch(el -> el == accessModifier);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -195,7 +178,8 @@ public class ParameterNameCheck
|
|||
final Optional<DetailAST> annotation =
|
||||
Optional.ofNullable(parent.getFirstChild().getFirstChild());
|
||||
|
||||
if (annotation.isPresent() && annotation.get().getType() == TokenTypes.ANNOTATION) {
|
||||
if (annotation.isPresent()
|
||||
&& annotation.get().getType() == TokenTypes.ANNOTATION) {
|
||||
final Optional<DetailAST> overrideToken =
|
||||
Optional.ofNullable(annotation.get().findFirstToken(TokenTypes.IDENT));
|
||||
if (overrideToken.isPresent() && "Override".equals(overrideToken.get().getText())) {
|
||||
|
|
|
|||
|
|
@ -23,9 +23,11 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import antlr.collections.AST;
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
import com.puppycrawl.tools.checkstyle.api.FullIdent;
|
||||
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
||||
import com.puppycrawl.tools.checkstyle.checks.naming.AccessModifier;
|
||||
|
||||
/**
|
||||
* Contains utility methods for the checks.
|
||||
|
|
@ -425,4 +427,34 @@ public final class CheckUtils {
|
|||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@link AccessModifier} based on the information about access modifier
|
||||
* taken from the given token of type {@link TokenTypes#MODIFIERS}.
|
||||
* @param modifiersToken token of type {@link TokenTypes#MODIFIERS}.
|
||||
* @return {@link AccessModifier}.
|
||||
*/
|
||||
public static AccessModifier getAccessModifierFromModifiersToken(DetailAST modifiersToken) {
|
||||
if (modifiersToken == null || modifiersToken.getType() != TokenTypes.MODIFIERS) {
|
||||
throw new IllegalArgumentException("expected non-null AST-token with type 'MODIFIERS'");
|
||||
}
|
||||
|
||||
// default access modifier
|
||||
AccessModifier accessModifier = AccessModifier.PACKAGE;
|
||||
for (AST token = modifiersToken.getFirstChild(); token != null;
|
||||
token = token.getNextSibling()) {
|
||||
|
||||
final int tokenType = token.getType();
|
||||
if (tokenType == TokenTypes.LITERAL_PUBLIC) {
|
||||
accessModifier = AccessModifier.PUBLIC;
|
||||
}
|
||||
else if (tokenType == TokenTypes.LITERAL_PROTECTED) {
|
||||
accessModifier = AccessModifier.PROTECTED;
|
||||
}
|
||||
else if (tokenType == TokenTypes.LITERAL_PRIVATE) {
|
||||
accessModifier = AccessModifier.PRIVATE;
|
||||
}
|
||||
}
|
||||
return accessModifier;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -110,14 +110,14 @@
|
|||
<module name="ParameterName">
|
||||
<property name="id" value="ParameterNameNonPublic"/>
|
||||
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
|
||||
<property name="excludeScope" value="public"/>
|
||||
<property name="accessModifiers" value="protected, package, private"/>
|
||||
<message key="name.invalidPattern"
|
||||
value="Parameter name ''{0}'' must match pattern ''{1}''."/>
|
||||
</module>
|
||||
<module name="ParameterName">
|
||||
<property name="id" value="ParameterNamePublic"/>
|
||||
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
|
||||
<property name="scope" value="public"/>
|
||||
<property name="accessModifiers" value="public"/>
|
||||
<message key="name.invalidPattern"
|
||||
value="Parameter name ''{0}'' must match pattern ''{1}''."/>
|
||||
</module>
|
||||
|
|
|
|||
|
|
@ -29,7 +29,6 @@ import org.junit.Test;
|
|||
|
||||
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
|
||||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
|
||||
import com.puppycrawl.tools.checkstyle.api.Scope;
|
||||
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
||||
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
|
||||
|
||||
|
|
@ -140,65 +139,24 @@ public class ParameterNameCheckTest
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testScope()
|
||||
public void testPublicAccessModifier()
|
||||
throws Exception {
|
||||
final DefaultConfiguration checkConfig =
|
||||
createCheckConfig(ParameterNameCheck.class);
|
||||
checkConfig.addAttribute("format", "^h$");
|
||||
checkConfig.addAttribute("scope", Scope.PUBLIC.getName());
|
||||
checkConfig.addAttribute("accessModifiers", AccessModifier.PUBLIC.toString());
|
||||
|
||||
final String pattern = "^h$";
|
||||
|
||||
final String[] expected = {
|
||||
"5:27: " + getCheckMessage(MSG_INVALID_PATTERN, "pubconstr", pattern),
|
||||
"5:36: " + getCheckMessage(MSG_INVALID_PATTERN, "pubconstr", pattern),
|
||||
"9:31: " + getCheckMessage(MSG_INVALID_PATTERN, "inner", pattern),
|
||||
"19:24: " + getCheckMessage(MSG_INVALID_PATTERN, "pubpub", pattern),
|
||||
"30:21: " + getCheckMessage(MSG_INVALID_PATTERN, "pubifc", pattern),
|
||||
"44:24: " + getCheckMessage(MSG_INVALID_PATTERN, "packpub", pattern),
|
||||
"60:21: " + getCheckMessage(MSG_INVALID_PATTERN, "packifc", pattern),
|
||||
};
|
||||
verify(checkConfig, getPath("InputScope.java"), expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExcludeScope()
|
||||
throws Exception {
|
||||
final DefaultConfiguration checkConfig =
|
||||
createCheckConfig(ParameterNameCheck.class);
|
||||
checkConfig.addAttribute("format", "^h$");
|
||||
checkConfig.addAttribute("excludeScope", Scope.PROTECTED.getName());
|
||||
|
||||
final String pattern = "^h$";
|
||||
|
||||
final String[] expected = {
|
||||
"23:17: " + getCheckMessage(MSG_INVALID_PATTERN, "pubpack", pattern),
|
||||
"25:25: " + getCheckMessage(MSG_INVALID_PATTERN, "pubpriv", pattern),
|
||||
"48:17: " + getCheckMessage(MSG_INVALID_PATTERN, "packpack", pattern),
|
||||
"50:25: " + getCheckMessage(MSG_INVALID_PATTERN, "packpriv", pattern),
|
||||
"68:27: " + getCheckMessage(MSG_INVALID_PATTERN, "lexp", pattern),
|
||||
"70:23: " + getCheckMessage(MSG_INVALID_PATTERN, "limp", pattern),
|
||||
};
|
||||
verify(checkConfig, getPath("InputScope.java"), expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScopeExcludeScope()
|
||||
throws Exception {
|
||||
final DefaultConfiguration checkConfig =
|
||||
createCheckConfig(ParameterNameCheck.class);
|
||||
checkConfig.addAttribute("format", "^h$");
|
||||
checkConfig.addAttribute("scope", Scope.PACKAGE.getName());
|
||||
checkConfig.addAttribute("excludeScope", Scope.PUBLIC.getName());
|
||||
|
||||
final String pattern = "^h$";
|
||||
|
||||
final String[] expected = {
|
||||
"21:27: " + getCheckMessage(MSG_INVALID_PATTERN, "pubprot", pattern),
|
||||
"23:17: " + getCheckMessage(MSG_INVALID_PATTERN, "pubpack", pattern),
|
||||
"46:27: " + getCheckMessage(MSG_INVALID_PATTERN, "packprot", pattern),
|
||||
"48:17: " + getCheckMessage(MSG_INVALID_PATTERN, "packpack", pattern),
|
||||
};
|
||||
verify(checkConfig, getPath("InputScope.java"), expected);
|
||||
verify(checkConfig, getPath("InputAccessModifier.java"), expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
|
|
@ -64,6 +64,7 @@ import com.puppycrawl.tools.checkstyle.api.Configuration;
|
|||
import com.puppycrawl.tools.checkstyle.api.Scope;
|
||||
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
|
||||
import com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck;
|
||||
import com.puppycrawl.tools.checkstyle.checks.naming.AccessModifier;
|
||||
|
||||
public class XDocsPagesTest {
|
||||
private static final Path AVAILABLE_CHECKS_PATH = Paths.get("src/xdocs/checks.xml");
|
||||
|
|
@ -626,6 +627,9 @@ public class XDocsPagesTest {
|
|||
else if (clss == Scope.class) {
|
||||
result = "Scope";
|
||||
}
|
||||
else if (clss == AccessModifier[].class) {
|
||||
result = "Access Modifier Set";
|
||||
}
|
||||
else if (clss != String.class) {
|
||||
Assert.fail("Unknown property type: " + clss.getSimpleName());
|
||||
}
|
||||
|
|
@ -688,6 +692,9 @@ public class XDocsPagesTest {
|
|||
else if (value != null && (clss == SeverityLevel.class || clss == Scope.class)) {
|
||||
result = value.toString().toLowerCase(Locale.ENGLISH);
|
||||
}
|
||||
else if (value != null && clss == AccessModifier[].class) {
|
||||
result = Arrays.toString((Object[]) value).replace("[", "").replace("]", "");
|
||||
}
|
||||
|
||||
if (clss != String.class && clss != String[].class && result == null) {
|
||||
result = "null";
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ package com.puppycrawl.tools.checkstyle.utils;
|
|||
|
||||
import static com.puppycrawl.tools.checkstyle.internal.TestUtils.assertUtilsClassHasPrivateConstructor;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
|
@ -112,7 +113,34 @@ public class CheckUtilsTest {
|
|||
metDef.addChild(parameters);
|
||||
|
||||
Assert.assertFalse(CheckUtils.isEqualsMethod(metDef));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAccessModifierFromModifiersTokenWrongTokenType() {
|
||||
final DetailAST modifiers = new DetailAST();
|
||||
modifiers.setType(TokenTypes.METHOD_DEF);
|
||||
|
||||
try {
|
||||
CheckUtils.getAccessModifierFromModifiersToken(modifiers);
|
||||
fail(IllegalArgumentException.class.getSimpleName() + " was expcted.");
|
||||
}
|
||||
catch (IllegalArgumentException exc) {
|
||||
final String expectedExceptionMsg = "expected non-null AST-token with type 'MODIFIERS'";
|
||||
final String actualExceptionMsg = exc.getMessage();
|
||||
assertEquals(expectedExceptionMsg, actualExceptionMsg);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAccessModifierFromModifiersTokenWithNullParameter() {
|
||||
try {
|
||||
CheckUtils.getAccessModifierFromModifiersToken(null);
|
||||
fail(IllegalArgumentException.class.getSimpleName() + " was expcted.");
|
||||
}
|
||||
catch (IllegalArgumentException exc) {
|
||||
final String expectedExceptionMsg = "expected non-null AST-token with type 'MODIFIERS'";
|
||||
final String actualExceptionMsg = exc.getMessage();
|
||||
assertEquals(expectedExceptionMsg, actualExceptionMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
package com.puppycrawl.tools.checkstyle.checks.naming;
|
||||
|
||||
public class InputScope {
|
||||
public class InputAccessModifier {
|
||||
|
||||
public InputScope(int pubconstr) {}
|
||||
public InputAccessModifier(int pubconstr) {}
|
||||
|
||||
public void v1(int h) {
|
||||
new Object () {
|
||||
|
|
@ -1149,8 +1149,8 @@ class MyClass {
|
|||
<subsection name="Description">
|
||||
<p>
|
||||
Checks that method and <code>catch</code> parameter names conform to a format specified
|
||||
by the format property. By using <code>scope</code> and <code>excludeScope</code> properties
|
||||
it is possible to specify different formats for methods at different visibility levels.
|
||||
by the format property. By using <code>accessModifiers</code> property it is possible
|
||||
to specify different formats for methods at different visibility levels.
|
||||
</p>
|
||||
</subsection>
|
||||
|
||||
|
|
@ -1185,16 +1185,10 @@ public boolean equals(Object o) {
|
|||
<td><code>false</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>scope</td>
|
||||
<td>Visibility scope of methods where parameters are checked.</td>
|
||||
<td><a href="property_types.html#scope">Scope</a></td>
|
||||
<td><code>anoninner</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>excludeScope</td>
|
||||
<td>Visibility scope of methods where parameters are not checked.</td>
|
||||
<td><a href="property_types.html#scope">Scope</a></td>
|
||||
<td><code>null</code></td>
|
||||
<td>accessModifiers</td>
|
||||
<td>Access modifiers of methods where parameters are checked.</td>
|
||||
<td><a href="property_types.html#access_modifiers">Access Modifier Set</a></td>
|
||||
<td><code>public, protected, package, private</code></td>
|
||||
</tr>
|
||||
</table>
|
||||
</subsection>
|
||||
|
|
@ -1231,14 +1225,14 @@ public boolean equals(Object o) {
|
|||
</p>
|
||||
<source>
|
||||
<module name="ParameterName">
|
||||
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
|
||||
<property name="excludeScope" value="public"/>
|
||||
<message key="name.invalidPattern"
|
||||
value="Parameter name ''{0}'' must match pattern ''{1}''."/>
|
||||
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
|
||||
<property name="accessModifiers" value="protected, package, private"/>
|
||||
<message key="name.invalidPattern"
|
||||
value="Parameter name ''{0}'' must match pattern ''{1}''"/>
|
||||
</module>
|
||||
<module name="ParameterName">
|
||||
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
|
||||
<property name="scope" value="public"/>
|
||||
<property name="accessModifiers" value="public"/>
|
||||
<message key="name.invalidPattern"
|
||||
value="Parameter name ''{0}'' must match pattern ''{1}''"/>
|
||||
</module>
|
||||
|
|
|
|||
|
|
@ -403,6 +403,17 @@
|
|||
</ul>
|
||||
</section>
|
||||
|
||||
<section name="access_modifiers">
|
||||
<p>This property represents Java access modifiers.</p>
|
||||
|
||||
<ul>
|
||||
<li><code>public</code></li>
|
||||
<li><code>protected</code></li>
|
||||
<li><code>package</code></li>
|
||||
<li><code>private</code></li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<section name="severity">
|
||||
<p>
|
||||
This property represents the severity level of a check violation. The
|
||||
|
|
|
|||
Loading…
Reference in New Issue