[英]how to create rule for java comments in pmd?
Could someone please help me to create rule in pmd for eclipse? 有人可以帮我在Pmd中为月食创建规则吗? I am unable to start even i followed through PMD official site.
即使我通过PMD官方网站也无法启动。 I am planning to create rule in java instead of XPath rule.
我打算在Java中创建规则,而不是XPath规则。 Any simple guidelines to start it?
有什么简单的指导开始吗? Thanks
谢谢
Try the 'Write a rule using Java' method as you can easily start this way and later you can try with XPath expressions. 尝试使用“使用Java编写规则”方法,因为您可以轻松地以这种方式开始,以后可以尝试使用XPath表达式。 You can follow these steps alongside the official link .
您可以在官方链接旁执行以下步骤。
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));
}
}
Append the following rule inside basic.xml(pmd-4.2.5\\rulesets\\basic.xml) : 在basic.xml(pmd-4.2.5 \\ rulesets \\ basic.xml)内附加以下规则:
Copy paste the xml content from "Put the WhileLoopsMustUseBracesRule rule in a ruleset file" section to basic.xml. 将“将WhileLoopsMustUseBracesRule规则放入规则集文件中”部分的xml内容复制粘贴到basic.xml中。
Replace the line 更换线
class="WhileLoopsMustUseBracesRule">
with 与
class="net.sourceforge.pmd.rules.basic.WhileLoopsMustUseBracesRule">
as you must have created the java file (WhileLoopsMustUseBracesRule.java) inside package "net.sourceforge.pmd.rules.basic" 因为您必须在“ net.sourceforge.pmd.rules.basic”包中创建了Java文件(WhileLoopsMustUseBracesRule.java)
Run this command from cmd prompt if in windows. 如果在Windows中,请从cmd提示符运行此命令。
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
Hope this helps.! 希望这可以帮助。!
I wrote a tutorial a while back with some sample code that could be helpful. 我前一段时间写了一个教程,其中包含一些可能有用的示例代码。
http://www.techtraits.ca/writting-pretty-code-with-pmd/ http://www.techtraits.ca/writting-pretty-code-with-pmd/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.