2
我的需求是解析java文件并查找实现特定接口的类或接口。因此我开始在PMD中实现自定义规则。我能够编写一个XPath表达式来搜索接口类&,但无法找出正确的方式将要进行搜索的接口名称传递给XPath规则。一种方法是在调用PMD之前定义属性并更新xml文件。使用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>
到上述方法的问题是,PMD不能在线程中调用,因为XML将被共享。
有没有人遇到过这样的PMD问题,其中值要在运行时传递给规则?
我找到了解决办法!虽然不使用PMD。 Checkstyle是另一种静态分析工具,它允许定义自定义规则并将运行时值传递给这些规则。 – Amit 2010-08-02 05:22:57