how to use PMD 5.8.1 Java API to create a rule and to raise a ruleviolation #3052
-
Hi, I am new to custom rule development using PMD java API. I am trying to create a simple rule which scans for classes for empty package name. From a high level perspective , I believe the code would look something like below (please correct me if I am incorrect). public class PackageEmptyRule extends AbstractJavaRule {
String packageName;
public Object visit(ASTPackageDeclaration node, Object data) {
packageName = node.getPackageNameImage();
return data;
}
// I chose to listen for class declarations because we are
// guaranteed to have it, and it will always be called after
// the package declaration.
public Object visit(ASTClassOrInterfaceType node, Object data) {
if (packageName == null) {
// Messages printed to standard output will be shown when
// PMD is run from the command line or as an Ant target.
System.out.println("Found a class with no package declaration.");
//Add code here to add a rule violation.
}
return data;
}
} Request you to provide an example of writing a custom rule using java api. |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
@Neha-Dhonde your first approach at the problem seems quite on track. You are simply missing the There are a couple of improvements to be done, but mostly nitpicking (such as using the rule chain for performance, which is unfortunately under documented #597 ). In this case however, I'd personally not go with a Java rule and prefer XPath, mostly due to simplicity. |
Beta Was this translation helpful? Give feedback.
-
@Neha-Dhonde Bear in mind that a single instance of the rule will analyse several files, so you might want to override the @jsotuyod is right though, an XPath rule would be simpler. There's a guide here |
Beta Was this translation helpful? Give feedback.
-
@oowekyala that's a good catch! I'd personally not visit |
Beta Was this translation helpful? Give feedback.
-
@jsotuyod TIL that existed, thanks! |
Beta Was this translation helpful? Give feedback.
-
Thanks @jsotuyod & @oowekyala for your inputs. The reason i am going for a java based custom rule is for scenarios like below: |
Beta Was this translation helpful? Give feedback.
-
@Neha-Dhonde That looks like an entirely different rule. It's advisable to create one rule for each type of violation you want to catch, mixing the detection logic for missing package statements and logger calls seems to ask for trouble imho. I'd recommend making an XPath rule for your first rule, and another Java rule for logger call detection. You could use the symbol table to find the declaration of the logger and its type, like in ForLoopCanBeForeach, because unfortunately type resolution still loses some information in calls such as |
Beta Was this translation helpful? Give feedback.
-
I believe the original question has already been answered. Shall there be any follow up questions, just open a new issue. |
Beta Was this translation helpful? Give feedback.
@Neha-Dhonde That looks like an entirely different rule. It's advisable to create one rule for each type of violation you want to catch, mixing the detection logic for missing package statements and logger calls seems to ask for trouble imho.
I'd recommend making an XPath rule for your first rule, and another Java rule for logger call detection. You could use the symbol table to find the declaration of the logger and its type, like in ForLoopCanBeForeach, because unfortunately type resolution still loses some information in calls such as
myLogger.info(..)
(see #497).