False negative on AbstractPoorMethodCall
A source code analyzer
Brought to you by:
adangel,
juansotuyo
I created a PMD rule that is intended to trigger when someone uses the methods of the String class that use regular expressions internally.
public class AvoidStringRegex extends AbstractPoorMethodCall {
private static final String avoidType = "String";
private static final String[] avoidMethods = { "split", "matches", "replace", "replaceAll", "replaceFirst" };
protected String targetTypename() {
return avoidType;
}
protected String[] methodNames() {
return avoidMethods.clone();
}
@Override
protected boolean isViolationArgument(final int argIndex, final String arg) {
return true;
}
Now I have these 3 lines of code:
/1/ String reversed1 = StringUtils.reverse(foo).replaceAll("-", "");
/2a/ String reversed2 = StringUtils.reverse(foo);
/2b/ reversed2 = reversed2.replaceAll("-", "");
I want to see this PMD message on lines '1' and '2b'. However I only see it on line '2b'.
In relationship of what I want this is a false negative.
This is either a bug in my class (if so please tell me what to fix) or a bug in the AbstractPoorMethodCall.
I applied 1 minor change to the above class:
protected boolean isViolationArgument(final int argIndex, final String arg) {
return (argIndex == 0);
}
I found a second 'false negative' for this situation.
Line marked as /1/ is marked (correct)
Line marked as /2/ is not marked (incorrect)