简体   繁体   English

如何在PMD中为Java注释创建规则?

[英]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 . 您可以在官方链接旁执行以下步骤。

  • Start with the src package that comes with PMD (eg pmd-4.2.x\\src), create your java class inside an existing package (eg pmd-4.2.5\\src\\net\\sourceforge\\pmd\\rules\\basic). 从PMD随附的src软件包开始(例如pmd-4.2.x \\ src),在现有软件包中创建Java类(例如pmd-4.2.5 \\ src \\ net \\ sourceforge \\ pmd \\ rules \\ basic)。 In this case it is the following sample code (WhileLoopsMustUseBracesRule) : 在这种情况下,它是以下示例代码(WhileLoopsMustUseBracesRule):

    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 

  • For the nix os (linux/MacOS), use pmd.sh instead of pmd.bat 对于nix os(linux / MacOS),请使用pmd.sh而不是pmd.bat
  • Once you are able to get it running, you can create your own rule inside your own package. 一旦能够运行它,就可以在自己的包中创建自己的规则。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM