使用 PMD 自定义规则 - 在运行时将值传递给规则
我的要求是解析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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望能够在 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.