Skip to content

Commit

Permalink
Issue checkstyle#3381: Update Google style coverage to state of 12 Ju…
Browse files Browse the repository at this point in the history
…ly 2016
  • Loading branch information
agcuda committed Sep 1, 2016
1 parent b4e884c commit bfb2ee9
Show file tree
Hide file tree
Showing 20 changed files with 279 additions and 45 deletions.
Expand Up @@ -38,13 +38,10 @@ protected String getPath(String fileName) throws IOException {
public void catchParameterNameTest() throws Exception {
final Configuration checkConfig = getCheckConfig("CatchParameterName");
final String msgKey = "name.invalidPattern";
final String format = "^[a-z][a-z0-9][a-zA-Z0-9]*$";
final String format = "^[a-z][a-zA-Z0-9]*$";

final String[] expected = {
"6:28: " + getCheckMessage(checkConfig.getMessages(), msgKey, "e", format),
"24:28: " + getCheckMessage(checkConfig.getMessages(), msgKey, "t", format),
"47:28: " + getCheckMessage(checkConfig.getMessages(), msgKey, "iException", format),
"50:28: " + getCheckMessage(checkConfig.getMessages(), msgKey, "x", format),
"24:28: " + getCheckMessage(checkConfig.getMessages(), msgKey, "T", format),
};

final String filePath = getPath("InputCatchParameterName.java");
Expand Down
Expand Up @@ -57,12 +57,8 @@ public void parameterNameTest() throws Exception {
"11:21: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "a_rg4", format),
"12:21: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "_arg5", format),
"13:21: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "arg6_", format),
"14:21: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aArg7", format),
"15:21: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aArg8", format),
"16:21: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aar_g", format),
"26:21: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "bB", format),
"49:22: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "llll_llll", format),
"50:21: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "bB", format),
};

final String filePath = getPath("InputParameterNameSimple.java");
Expand Down
Expand Up @@ -51,17 +51,15 @@ public static void setConfigurationBuilder() throws CheckstyleException {
public void localVariableNameTest() throws Exception {

final String[] expected = {
"26:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "a", format),
"27:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aA", format),
"28:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "a1_a", format),
"29:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "A_A", format),
"30:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aa2_a", format),
"31:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "_a", format),
"32:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "_aa", format),
"33:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aa_", format),
"34:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aaa$aaa", format),
"35:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "$aaaaaa", format),
"36:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aaaaaa$", format),
"26:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "a1_a", format),
"27:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "A_A", format),
"28:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aa2_a", format),
"29:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "_a", format),
"30:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "_aa", format),
"31:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aa_", format),
"32:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aaa$aaa", format),
"33:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "$aaaaaa", format),
"34:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aaaaaa$", format),
};

final String filePath = getPath("InputLocalVariableNameSimple.java");
Expand All @@ -74,7 +72,6 @@ public void localVariableNameTest() throws Exception {
public void oneCharTest() throws Exception {

final String[] expected = {
"15:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "i", format),
"21:17: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "I_ndex", format),
"45:17: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "i_ndex", format),
"49:17: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "ii_i1", format),
Expand Down
Expand Up @@ -3,7 +3,7 @@
public class InputCatchParameterName {
{
try {
} catch (Exception e) { // warn
} catch (Exception e) { // ok
}
try {
} catch (Exception ex) { // ok
Expand All @@ -21,7 +21,7 @@ public class InputCatchParameterName {
} catch (Exception noWorries) { // ok
}
try {
} catch (Throwable t) { // warn
} catch (Throwable T) { // warn
}
try {
throw new InterruptedException("interruptedException");
Expand All @@ -44,10 +44,10 @@ public class InputCatchParameterName {
}
}
try {
} catch (Exception iException) { // warn
} catch (Exception iException) { // ok
}
try {
} catch (Exception x) { // warn
} catch (Exception x) { // ok
}
}
}
Expand Up @@ -11,8 +11,8 @@ void toManyArgs(
int a_rg4, //warn
int _arg5, //warn
int arg6_, //warn
int aArg7, //warn
int aArg8, //warn
int aArg7, //ok
int aArg8, //ok
int aar_g) //warn

{}
Expand All @@ -23,7 +23,7 @@ class InputSimple2

/** Some more Javadoc. */
public void doSomething(int aaa, int abn, String aaA,
boolean bB) //warn
boolean bB) //ok
{
for (Object O : new java.util.ArrayList())
{
Expand All @@ -47,5 +47,5 @@ enum MyEnum1

public void doEnum(int aaaL,
long llll_llll, //warn
boolean bB) {} //warn
boolean bB) {} //ok
}
Expand Up @@ -12,7 +12,7 @@ public void fooMethod()
//some code
}

int i = 0; //warn
int i = 0; //ok

for(int index = 1; index < 10; index++) { //ok
//some code
Expand Down
Expand Up @@ -23,8 +23,6 @@ final class InputSimple
private void localVariables()
{
//bad examples
int a; //warn
int aA; //warn
int a1_a; //warn
int A_A; //warn
int aa2_a; //warn
Expand Down
Expand Up @@ -41,6 +41,7 @@
<li><span class="code">private</span></li>
<li><span class="code">abstract</span></li>
<li><span class="code">default</span></li>
<li><span class="code">static</span></li>
<li><span class="code">final</span></li>
<li><span class="code">transient</span></li>
Expand Down Expand Up @@ -84,8 +85,8 @@ public class ModifierOrderCheck
* 8.3.1 and 8.4.3 of the JLS.
*/
private static final String[] JLS_ORDER = {
"public", "protected", "private", "abstract", "static", "final",
"transient", "volatile", "synchronized", "native", "strictfp", "default",
"public", "protected", "private", "abstract", "default", "static",
"final", "transient", "volatile", "synchronized", "native", "strictfp",
};

@Override
Expand Down
Expand Up @@ -22,8 +22,10 @@
import java.util.Optional;

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>
Expand All @@ -33,9 +35,13 @@
* and defaults to
* <strong>^[a-z][a-zA-Z0-9]*$</strong>.
* </p>
* <p>The check has the following option:</p>
* <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 values is <b>private</b> .</p>
* <p><b>excludeScope</b> - visibility scope of methods not to be checked.
* Default values is <b>null</b> .</p>
* <p>
* An example of how to configure the check is:
* </p>
Expand Down Expand Up @@ -72,6 +78,12 @@ public class ParameterNameCheck
*/
private boolean ignoreOverridden;

/** The visibility scope where methods are checked. */
private Scope scope = Scope.PRIVATE;

/** The visibility scope where methods shouldn't be checked. */
private Scope excludeScope;

/**
* Creates a new {@code ParameterNameCheck} instance.
*/
Expand All @@ -88,6 +100,22 @@ public void setIgnoreOverridden(boolean ignoreOverridden) {
this.ignoreOverridden = ignoreOverridden;
}

/**
* Set the scope.
* @param from a {@code String} value
*/
public void setScope(String from) {
scope = Scope.getInstance(from);
}

/**
* Set the excludeScope.
* @param excludeScope a {@code String} value
*/
public void setExcludeScope(String excludeScope) {
this.excludeScope = Scope.getInstance(excludeScope);
}

@Override
public int[] getDefaultTokens() {
return getAcceptableTokens();
Expand All @@ -105,15 +133,53 @@ public int[] getRequiredTokens() {

@Override
protected boolean mustCheckName(DetailAST ast) {
final Scope theScope = calculateScope(ast);
boolean checkName = true;
if (ignoreOverridden && isOverriddenMethod(ast)
|| ast.getParent().getType() == TokenTypes.LITERAL_CATCH
|| CheckUtils.isReceiverParameter(ast)) {
|| CheckUtils.isReceiverParameter(ast)
|| !matchScope(ast, theScope)) {
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
* to be public.
*
* @param ast the token of the method/constructor
* @return the scope of the method/constructor
*/
private static Scope calculateScope(final DetailAST ast) {
if (ast.getParent().getType() == TokenTypes.LITERAL_CATCH) {
return Scope.PRIVATE;
}
final DetailAST params = ast.getParent();
final DetailAST meth = params.getParent();
final DetailAST mods = meth.findFirstToken(TokenTypes.MODIFIERS);
Scope declaredScope = ScopeUtils.getScopeFromMods(mods);
if (ScopeUtils.isInInterfaceOrAnnotationBlock(ast)) {
declaredScope = Scope.PUBLIC;
}
return declaredScope;
}

/**
* Checks whether a method has the correct scope to be checked.
* @param ast a fiven node
* @param nodeScope the scope of the method
* @return whether the method matches the expected scope
*/
private boolean matchScope(final DetailAST ast, final Scope nodeScope) {
final Scope surroundingScope = ScopeUtils.getSurroundingScope(ast);
return nodeScope.isIn(scope) && surroundingScope.isIn(scope)
&& (excludeScope == null
|| !nodeScope.isIn(excludeScope)
|| !surroundingScope.isIn(excludeScope));
}

/**
* Checks whether a method is annotated with Override annotation.
* @param ast method parameter definition token.
Expand Down
Expand Up @@ -70,6 +70,7 @@
* {@link TokenTypes#SL_ASSIGN SL_ASSIGN},
* {@link TokenTypes#SR_ASSIGN SR_ASSIGN},
* {@link TokenTypes#STAR_ASSIGN STAR_ASSIGN}.
* {@link TokenTypes#DOUBLE_COLON DOUBLE_COLON}.
* </p>
* <p>
* An example of how to configure the check is:
Expand Down Expand Up @@ -190,6 +191,7 @@ public int[] getAcceptableTokens() {
TokenTypes.BXOR_ASSIGN, // "^="
TokenTypes.BOR_ASSIGN, // "|="
TokenTypes.BAND_ASSIGN, // "&="
TokenTypes.METHOD_REF, // "::"

};
}
Expand Down
15 changes: 11 additions & 4 deletions src/main/resources/google_checks.xml
Expand Up @@ -102,18 +102,25 @@
value="Member 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="format" value="^[a-z][a-zA-Z0-9]*$"/>
<property name="excludeScope" value="public"/>
<message key="name.invalidPattern"
value="Parameter name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="ParameterName">
<property name="format" value="^[a-z][a-zA-Z0-9]+$"/>
<property name="scope" value="public"/>
<message key="name.invalidPattern"
value="Public parameter name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="CatchParameterName">
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
<property name="format" value="^[a-z][a-zA-Z0-9]*$"/>
<message key="name.invalidPattern"
value="Catch parameter name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="LocalVariableName">
<property name="tokens" value="VARIABLE_DEF"/>
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
<property name="format" value="^[a-z][a-zA-Z0-9]*$"/>
<property name="allowOneCharVarInForLoop" value="true"/>
<message key="name.invalidPattern"
value="Local variable name ''{0}'' must match pattern ''{1}''."/>
Expand Down Expand Up @@ -166,7 +173,7 @@
<module name="MethodParamPad"/>
<module name="OperatorWrap">
<property name="option" value="NL"/>
<property name="tokens" value="BAND, BOR, BSR, BXOR, DIV, EQUAL, GE, GT, LAND, LE, LITERAL_INSTANCEOF, LOR, LT, MINUS, MOD, NOT_EQUAL, PLUS, QUESTION, SL, SR, STAR "/>
<property name="tokens" value="BAND, BOR, BSR, BXOR, DIV, EQUAL, GE, GT, LAND, LE, LITERAL_INSTANCEOF, LOR, LT, MINUS, MOD, NOT_EQUAL, PLUS, QUESTION, SL, SR, STAR, METHOD_REF "/>
</module>
<module name="AnnotationLocation">
<property name="tokens" value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF"/>
Expand Down
Expand Up @@ -66,6 +66,7 @@ public void testIt() throws Exception {
"34:13: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@MyAnnotation2"),
"39:13: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@MyAnnotation2"),
"49:35: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@MyAnnotation4"),
"157:13: " + getCheckMessage(MSG_MODIFIER_ORDER, "public"),
};
verify(checkConfig, getPath("InputModifier.java"), expected);
}
Expand Down
Expand Up @@ -78,6 +78,8 @@ public void testIt() throws Exception {
"118:5: " + getCheckMessage(MSG_KEY, "static"),
"120:5: " + getCheckMessage(MSG_KEY, "public"),
"121:5: " + getCheckMessage(MSG_KEY, "abstract"),
"152:5: " + getCheckMessage(MSG_KEY, "public"),
"157:13: " + getCheckMessage(MSG_KEY, "public"),
};
verify(checkConfig, getPath("InputModifier.java"), expected);
}
Expand Down

0 comments on commit bfb2ee9

Please sign in to comment.