Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try the 'Write a rule using Java' method as you can easily start this way and later you can try with XPath expressions. You can follow these steps alongside the official <a href="http://pmd.sourceforge.net/howtowritearule.html" rel="nofollow">link</a>.</p> <ul> <li>Start with the src package that comes with PMD (e.g. pmd-4.2.x\src), create your java class inside an existing package (e.g. pmd-4.2.5\src\net\sourceforge\pmd\rules\basic). In this case it is the following sample code (WhileLoopsMustUseBracesRule) :</li> </ul> <pre><code> package net.sourceforge.pmd.rules.basic; import net.sourceforge.pmd.*; import net.sourceforge.pmd.ast.*; public class WhileLoopsMustUseBracesRule extends AbstractRule { public Object visit(ASTWhileStatement node, Object data) { SimpleNode firstStmt = (SimpleNode)node.jjtGetChild(1); if (!hasBlockAsFirstChild(firstStmt)) { addViolation(data, node); } return super.visit(node,data); } private boolean hasBlockAsFirstChild(SimpleNode node) { return (node.jjtGetNumChildren() != 0 && (node.jjtGetChild(0) instanceof ASTBlock)); } } </code></pre> <ul> <li><p>Append the following rule inside basic.xml(pmd-4.2.5\rulesets\basic.xml) :</p> <p>Copy paste the xml content from "Put the WhileLoopsMustUseBracesRule rule in a ruleset file" section to basic.xml.</p> <p>Replace the line </p> <pre><code>class="WhileLoopsMustUseBracesRule"&gt; </code></pre> <p>with</p> <pre><code>class="net.sourceforge.pmd.rules.basic.WhileLoopsMustUseBracesRule"&gt; </code></pre> <p>as you must have created the java file (WhileLoopsMustUseBracesRule.java) inside package "net.sourceforge.pmd.rules.basic"</p></li> <li><p>Run this command from cmd prompt if in windows.</p></li> </ul> <pre><code> pmd.bat C:\JAVAFILE_ON_WHICH_YOU_WANT_TO_RUN_THIS_RULE xml C:\PMD\pmd-4.2.5\pmd-4.2.5\rulesets\basic.xml </code></pre> <ul> <li>For the nix os (linux/MacOS), use pmd.sh instead of pmd.bat</li> <li>Once you are able to get it running, you can create your own rule inside your own package.</li> </ul> <p>Hope this helps.!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload