[core] Trim rule property values
- Most single-valued properties are trimmed - Some types keep whitespaces, e.g. Character and Regex Fixes #3089
This commit is contained in:
1 parent
1f73ebc607
commit
27f007cb1f
5 files changed
+48
-13
No files matched your search
@@ -40,6 +40,8 @@ Note: Support for Java 14 preview language features have been removed. The versi
|
||||
|
||||
* apex-documentation
|
||||
* [#3075](https://github.com/pmd/pmd/issues/3075): \[apex] ApexDoc should support private access modifier
|
||||
* java-errorprone
|
||||
* [#3089](https://github.com/pmd/pmd/issues/3089): \[java] CloseResource rule throws exception on spaces in property types
|
||||
* plsql
|
||||
* [#3106](https://github.com/pmd/pmd/issues/3106): \[plsql] ParseException while parsing EXECUTE IMMEDIATE 'drop database link ' \|\| linkname;
|
||||
|
||||
|
||||
+1
-1
@@ -179,7 +179,7 @@ import net.sourceforge.pmd.Rule;
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
String[] strValues = valueString.split(Pattern.quote("" + multiValueDelimiter()));
|
||||
String[] strValues = valueString.split(Pattern.quote(String.valueOf(multiValueDelimiter())));
|
||||
|
||||
List<V> values = new ArrayList<>(strValues.length);
|
||||
for (String strValue : strValues) {
|
||||
|
||||
@@ -40,7 +40,7 @@ public final class ValueParserConstants {
|
||||
static final ValueParser<Method> METHOD_PARSER = new ValueParser<Method>() {
|
||||
@Override
|
||||
public Method valueOf(String value) throws IllegalArgumentException {
|
||||
return methodFrom(value, CLASS_METHOD_DELIMITER, METHOD_ARG_DELIMITER);
|
||||
return methodFrom(StringUtils.trim(value), CLASS_METHOD_DELIMITER, METHOD_ARG_DELIMITER);
|
||||
}
|
||||
|
||||
|
||||
@@ -158,21 +158,21 @@ public final class ValueParserConstants {
|
||||
static final ValueParser<String> STRING_PARSER = new ValueParser<String>() {
|
||||
@Override
|
||||
public String valueOf(String value) {
|
||||
return value;
|
||||
return StringUtils.trim(value);
|
||||
}
|
||||
};
|
||||
/** Extracts integers. */
|
||||
static final ValueParser<Integer> INTEGER_PARSER = new ValueParser<Integer>() {
|
||||
@Override
|
||||
public Integer valueOf(String value) {
|
||||
return Integer.valueOf(value);
|
||||
return Integer.valueOf(StringUtils.trim(value));
|
||||
}
|
||||
};
|
||||
/** Extracts booleans. */
|
||||
static final ValueParser<Boolean> BOOLEAN_PARSER = new ValueParser<Boolean>() {
|
||||
@Override
|
||||
public Boolean valueOf(String value) {
|
||||
return Boolean.valueOf(value);
|
||||
return Boolean.valueOf(StringUtils.trim(value));
|
||||
}
|
||||
};
|
||||
/** Extracts floats. */
|
||||
@@ -186,7 +186,7 @@ public final class ValueParserConstants {
|
||||
static final ValueParser<Long> LONG_PARSER = new ValueParser<Long>() {
|
||||
@Override
|
||||
public Long valueOf(String value) {
|
||||
return Long.valueOf(value);
|
||||
return Long.valueOf(StringUtils.trim(value));
|
||||
}
|
||||
};
|
||||
/** Extracts doubles. */
|
||||
@@ -200,7 +200,7 @@ public final class ValueParserConstants {
|
||||
static final ValueParser<File> FILE_PARSER = new ValueParser<File>() {
|
||||
@Override
|
||||
public File valueOf(String value) throws IllegalArgumentException {
|
||||
return new File(value);
|
||||
return new File(StringUtils.trim(value));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -216,17 +216,19 @@ public final class ValueParserConstants {
|
||||
static final ValueParser<Class> CLASS_PARSER = new ValueParser<Class>() {
|
||||
@Override
|
||||
public Class valueOf(String value) throws IllegalArgumentException {
|
||||
if (StringUtils.isBlank(value)) {
|
||||
String className = StringUtils.trimToNull(value);
|
||||
|
||||
if (className == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Class<?> cls = ClassUtil.getTypeFor(value);
|
||||
Class<?> cls = ClassUtil.getTypeFor(className);
|
||||
if (cls != null) {
|
||||
return cls;
|
||||
}
|
||||
|
||||
try {
|
||||
return Class.forName(value);
|
||||
return Class.forName(className);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
throw new IllegalArgumentException(value);
|
||||
}
|
||||
@@ -248,10 +250,11 @@ public final class ValueParserConstants {
|
||||
return new ValueParser<T>() {
|
||||
@Override
|
||||
public T valueOf(String value) throws IllegalArgumentException {
|
||||
if (!mappings.containsKey(value)) {
|
||||
throw new IllegalArgumentException("Value was not in the set " + mappings.keySet());
|
||||
String trimmedValue = StringUtils.trim(value);
|
||||
if (!mappings.containsKey(trimmedValue)) {
|
||||
throw new IllegalArgumentException("Value " + value + " was not in the set " + mappings.keySet());
|
||||
}
|
||||
return mappings.get(value);
|
||||
return mappings.get(trimmedValue);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -157,6 +157,7 @@ public class PropertyDescriptorTest {
|
||||
assertEquals("hello", descriptor.description());
|
||||
assertEquals(Integer.valueOf(1), descriptor.defaultValue());
|
||||
assertEquals(Integer.valueOf(5), descriptor.valueFrom("5"));
|
||||
assertEquals(Integer.valueOf(5), descriptor.valueFrom(" 5 "));
|
||||
|
||||
PropertyDescriptor<List<Integer>> listDescriptor = PropertyFactory.intListProperty("intListProp")
|
||||
.desc("hello")
|
||||
@@ -166,6 +167,7 @@ public class PropertyDescriptorTest {
|
||||
assertEquals("hello", listDescriptor.description());
|
||||
assertEquals(Arrays.asList(1, 2), listDescriptor.defaultValue());
|
||||
assertEquals(Arrays.asList(5, 7), listDescriptor.valueFrom("5,7"));
|
||||
assertEquals(Arrays.asList(5, 7), listDescriptor.valueFrom(" 5 , 7 "));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -189,6 +191,7 @@ public class PropertyDescriptorTest {
|
||||
assertEquals("hello", descriptor.description());
|
||||
assertEquals(Double.valueOf(1.0), descriptor.defaultValue());
|
||||
assertEquals(Double.valueOf(2.0), descriptor.valueFrom("2.0"));
|
||||
assertEquals(Double.valueOf(2.0), descriptor.valueFrom(" 2.0 "));
|
||||
|
||||
PropertyDescriptor<List<Double>> listDescriptor = PropertyFactory.doubleListProperty("doubleListProp")
|
||||
.desc("hello")
|
||||
@@ -198,6 +201,7 @@ public class PropertyDescriptorTest {
|
||||
assertEquals("hello", listDescriptor.description());
|
||||
assertEquals(Arrays.asList(1.0, 2.0), listDescriptor.defaultValue());
|
||||
assertEquals(Arrays.asList(2.0, 3.0), listDescriptor.valueFrom("2.0,3.0"));
|
||||
assertEquals(Arrays.asList(2.0, 3.0), listDescriptor.valueFrom(" 2.0 , 3.0 "));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -221,6 +225,7 @@ public class PropertyDescriptorTest {
|
||||
assertEquals("hello", descriptor.description());
|
||||
assertEquals("default value", descriptor.defaultValue());
|
||||
assertEquals("foo", descriptor.valueFrom("foo"));
|
||||
assertEquals("foo", descriptor.valueFrom(" foo "));
|
||||
|
||||
PropertyDescriptor<List<String>> listDescriptor = PropertyFactory.stringListProperty("stringListProp")
|
||||
.desc("hello")
|
||||
@@ -230,6 +235,7 @@ public class PropertyDescriptorTest {
|
||||
assertEquals("hello", listDescriptor.description());
|
||||
assertEquals(Arrays.asList("v1", "v2"), listDescriptor.defaultValue());
|
||||
assertEquals(Arrays.asList("foo", "bar"), listDescriptor.valueFrom("foo|bar"));
|
||||
assertEquals(Arrays.asList("foo", "bar"), listDescriptor.valueFrom(" foo | bar "));
|
||||
}
|
||||
|
||||
private enum SampleEnum { A, B, C }
|
||||
|
||||
+24
@@ -1515,6 +1515,30 @@ public class Foo {
|
||||
stream = stream.skip(2);
|
||||
}
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>[java] CloseResource rule throws exception on spaces in property types #3089</description>
|
||||
<!-- all whitespaces for the properties are important for the test: leading, tailing, and in the middle -->
|
||||
<rule-property name="types"> java.sql.Connection , java.sql.Statement , java.sql.ResultSet </rule-property>
|
||||
<rule-property name="allowedResourceTypes"> java.io.ByteArrayOutputStream | java.io.ByteArrayInputStream | java.io.StringWriter | java.io.CharArrayWriter | java.util.stream.Stream | java.util.stream.IntStream | java.util.stream.LongStream | java.util.stream.DoubleStream </rule-property>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
import java.io.*;
|
||||
public class Foo {
|
||||
public void bar() {
|
||||
InputStream in = null;
|
||||
try {
|
||||
in = new FileInputStream("test");
|
||||
} catch (IOException ignored) {
|
||||
} finally {
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
Reference in new issue
Block a user