使用 PMD 自定义规则 - 在运行时将值传递给规则

发布于 2024-09-12 06:12:33 字数 1014 浏览 0 评论 0原文

我的要求是解析java文件并找到实现特定接口的类或接口。因此,我开始在 PMD 中实现自定义规则。我能够编写一个 XPath 表达式来搜索类和类。接口,但无法找出将要执行搜索的接口名称传递给 XPath 规则的正确方法。一种方法是在调用 PMD 之前定义属性并更新 xml 文件。

<rule name="Implement or extend an interface"
    message="Implement or extend an interface"
    class="net.sourceforge.pmd.rules.XPathRule">
<description>
This rule will help us to find out all the classes/interface which implement a particular interface
</description>
<properties>
    <property name="xpath">
        <value>
        <![CDATA[
        //ImplementsList/ClassOrInterfaceType[@Image=$interfaceName] |
        //ExtendsList/ClassOrInterfaceType[@Image=$interfaceName]
        ]]>
        </value>
    </property>
    <property name="interfaceName">
        <value>Should be set at run time</value>
    </property>
</properties>
<example>

上述方法的问题是 PMD 无法在线程中调用,因为 xml 将被共享。

有没有人遇到过这样的 PMD 问题:值在运行时传递给规则?

My requirement is to parse java files and find the classes or interfaces which implement a particular interface. Hence I started with implementing custom rules in PMD. I was able to write an XPath expression to search the classes & interfaces but was not able to figure out the right way to pass the interface name for which the search is to be done to the XPath rule. One way was to define a property and update the xml file before invoking PMD.

<rule name="Implement or extend an interface"
    message="Implement or extend an interface"
    class="net.sourceforge.pmd.rules.XPathRule">
<description>
This rule will help us to find out all the classes/interface which implement a particular interface
</description>
<properties>
    <property name="xpath">
        <value>
        <![CDATA[
        //ImplementsList/ClassOrInterfaceType[@Image=$interfaceName] |
        //ExtendsList/ClassOrInterfaceType[@Image=$interfaceName]
        ]]>
        </value>
    </property>
    <property name="interfaceName">
        <value>Should be set at run time</value>
    </property>
</properties>
<example>

The problem with the above approach is that PMD cannot be invoked in threads since the xml would be shared.

Has anyone faced such a problem with PMD where values are to be passed to a rule at runtime?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月无声 2024-09-19 06:12:33

如果您希望能够在 XPath 规则中使用不同的值,则需要拥有该规则的多个副本 - 每个值一个。使用 PMD 的另一种方法是使用 Java 规则。它甚至可以调用 XPath。不同之处在于 Java 是运行时的,因此可以稍后获取值或循环访问一组值。

If you want to be able to use different values in an XPath rule, you'd need to have multiple copies of the rule - one for each value. An alternative using PMD is to use a Java rule. It can even call the XPath. The difference is that Java is runtime so can get values later or loop through a set of values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文