Skip to content

Commit

Permalink
Issue #12443: Fix null pointer exception on config value with trailin…
Browse files Browse the repository at this point in the history
…g whitespace
  • Loading branch information
nrmancuso authored and romani committed Nov 26, 2022
1 parent f127566 commit 7855254
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Expand Up @@ -4571,6 +4571,10 @@
<param>com.puppycrawl.tools.checkstyle.filters.SuppressWithPlainTextCommentFilterTest</param>
<param>com.puppycrawl.tools.checkstyle.checks.whitespace.FileTabCharacterCheckTest</param>
<param>com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheckTest</param>
<!-- kills mutation in AutomaticBean -->
<param>com.puppycrawl.tools.checkstyle.checks.naming.ParameterNameCheckTest</param>
<!-- kills mutation in AutomaticBean -->
<param>com.puppycrawl.tools.checkstyle.checks.coding.IllegalTypeCheckTest</param>
</targetTests>
<excludedTestClasses>
<param>*.Input*</param>
Expand Down
Expand Up @@ -361,7 +361,7 @@ private static class RelaxedStringArrayConverter implements Converter {
@Override
public Object convert(Class type, Object value) {
final StringTokenizer tokenizer = new StringTokenizer(
value.toString(), COMMA_SEPARATOR);
value.toString().trim(), COMMA_SEPARATOR);
final List<String> result = new ArrayList<>();

while (tokenizer.hasMoreTokens()) {
Expand Down Expand Up @@ -390,7 +390,7 @@ private static class RelaxedAccessModifierArrayConverter implements Converter {
public Object convert(Class type, Object value) {
// Converts to a String and trims it for the tokenizer.
final StringTokenizer tokenizer = new StringTokenizer(
value.toString(), COMMA_SEPARATOR);
value.toString().trim(), COMMA_SEPARATOR);
final List<AccessModifierOption> result = new ArrayList<>();

while (tokenizer.hasMoreTokens()) {
Expand Down
Expand Up @@ -389,6 +389,14 @@ public void testRecordComponentsPublicProtectedStatic() throws Exception {
expected);
}

@Test
public void testTrailingWhitespaceInConfig() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(
getPath("InputIllegalTypeWhitespaceInConfig.java"),
expected);
}

@Test
public void testTokensNotNull() {
final IllegalTypeCheck check = new IllegalTypeCheck();
Expand Down
Expand Up @@ -178,4 +178,11 @@ public void testLambdaParameterNoViolationAtAll() throws Exception {
getPath("InputParameterNameLambda.java"), expected);
}

@Test
public void testWhitespaceInConfig() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(
getPath("InputParameterNameWhitespaceInConfig.java"), expected);
}

}
@@ -0,0 +1,28 @@
/*
IllegalType
validateAbstractClassNames = (default)false
illegalClassNames = java.lang.StringBuffer,\t
legalAbstractClassNames = (default)
ignoredMethodNames = (default)getEnvironment, getInitialContext
illegalAbstractClassNameFormat = (default)^(.*[.])?Abstract.*$
memberModifiers = (default)
tokens = (default)ANNOTATION_FIELD_DEF, CLASS_DEF, INTERFACE_DEF, METHOD_CALL, METHOD_DEF, \
METHOD_REF, PARAMETER_DEF, VARIABLE_DEF, PATTERN_VARIABLE_DEF, RECORD_DEF, \
RECORD_COMPONENT_DEF
*/
package com.puppycrawl.tools.checkstyle.checks.coding.illegaltype;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.util.List;

public class InputIllegalTypeWhitespaceInConfig {
public void example(List<@MyPattern String> strings) { // ok
}

@Target(ElementType.TYPE_USE)
public @interface MyPattern {}
}

@@ -0,0 +1,14 @@
/*
ParameterName
format = (default)^[a-z][a-zA-Z0-9]*$
ignoreOverridden = (default)false
accessModifiers = public\t,\t
*/

package com.puppycrawl.tools.checkstyle.checks.naming.parametername;

public class InputParameterNameWhitespaceInConfig {
int method(){return 1;} // ok
}

0 comments on commit 7855254

Please sign in to comment.