I am working on a Java project that uses PMD-eclipse plugin to check the code quality. I want to write a custom PMD ruleset that can identify hardcoded variables in the code, such as strings, numbers, or booleans. I have read the PMD documentation, but I am still confused about how to write the xpath expression for this rule.
This is the sample java code that I am testing on PMD-Designer
class Main{
public static void main(String [] args) {
String message = "helllo this is stirng";
String str = properties.getProperty("FilterQueueSet.CADObject");
boolean b = true;
String str_emptty = "";
int k = 0;
Scanner obj = new Scanner(System.in);
int z = Integer.MaxValue;
}
}
And finally here is the XPath expression I have tried for PDM do detect StringLiteral, BooleanLiteral and NumericLiteral from the sample java code.
//Literal [not (ancestor :: Annotation)] |
//NumericLiteral [not (ancestor :: Annotation)] |
//StringLiteral [not (ancestor :: Annotation) and string-length (@Image) > 10 ] |
//BooleanLiteral [ not (ancestor :: Annotation)]
i.e, it should only identify message, b, and k and exclude str,str_empty,obj and z.
but its detecting the string in the getProperty method call, which i dont want it to detect as its inside and method call (it should only detect strings that are assigned to a variable) and unable to detect the boolean value.
Any help on this would be really appreciated