0

My problem

I want to create a custom ruleset for PMD that finds all declared variables that use a specific package. And it should be a XPath ruleset, because that is easier to maintain for me.

What I have so far

I am able to find import statements and variables. I'm using this XPath to find the declarations:

//VariableDeclarator[../Type
                          /ReferenceType
                             /ClassOrInterfaceType
                                [@Image = 'ClassA']]

However, this is only matching the exact class, but I want to check if this class comes from package my.package and I don't know how to get that.

Any hints?

4

1 回答 1

1

自从我使用 PMD 以来已经有一段时间了,我在这里从记忆中开始,但是尝试在您的 XPath 之前使用:

//ClassOrInterfaceDeclaration[preceding::PackageDeclaration/Name/@Image = 'my.package']//VariableDeclarator[etc..

基本上,它会在 XML 中查找声明适当包的相关节点。就像我说的,这是来自相当古老的记忆,但希望它至少应该为您指明正确的方向。

于 2011-01-31T14:51:29.180 回答