结合两个PMD检查 [英] combine two PMD checks
问题描述
在使用PMD检查我的代码错误时遇到问题.我不知道如何同时满足两个要求.例如,如果我要检查文件扩展名BCD中不存在的名为ABC的方法.我知道如何使用PMD检查ABC是否存在或是否从BCD扩展出来.
赞:
//PrimaryExpression/PrimaryPrefix/Name [@Image = "ABC"];
//ExtendsList/ClassOrInterfaceType [@Image != "BCD"];
现在,无论如何,我可以一起检查这两个.例如,我希望班级中没有ABC扩展BCD.看来我不能简单地使用和来连接这两个Xpath查询.另外,我注意到我可以使用|与他们建立联系,但是|作为或工作.我需要一个and在这里而不是or.
我尝试过这样的事情:
//PrimaryExpression/PrimaryPrefix/Name[@Image = "ABC"]
[//ancestor::ClassOrInterfaceDeclaration/ExtendsList/
ClassOrInterfaceType[@Image != "BCD"]]
似乎至少对我有用.但是,自从我尝试过这种方法以来,我仍然不确定100%是否是正确的方法.
您的编辑应该可以工作,但是请注意,ancestor
轴将递归所有父节点,因此不应在'//'处引入它.
另一种替代方法是基于公共祖先的Xpath(例如ClassOrInterfaceDeclaration
),然后使用and
确保满足两个条件.由于似乎您只是在测试是否满足这两个条件的节点,所以我想结果表达式/节点集实际返回的内容并不重要:
//ClassOrInterfaceDeclaration[ExtendsList/ClassOrInterfaceType[@Image != "BCD"]
and descendant::PrimaryExpression/PrimaryPrefix/Name[@Image = "ABC"]]
但是,如果您确实需要在成功后选择特定的节点,则只需添加相对于ClassOrInterfaceDeclaration
的节点路径即可:
//ClassOrInterfaceDeclaration[... predicate ...]/Some/Path/Here
您还可以应用类似count()
的函数来确定满足条件的节点数:
count(//ClassOrInterfaceDeclaration[... predicate ...])
然后计算表达式.
(注意-我不熟悉PMD布局)
我举了一些例子,展示了//ancestor::
与ancestor::
的影响以及我的选择. /p>
I met a problem while using PMD to check my code errors. I do not know how to satisfy two requirements at the same time. For example, if I want to check a method named ABC not existing in file extends from BCD. I know how to check whether ABC exists or whether it extends from BCD separately using PMD.
Like this:
//PrimaryExpression/PrimaryPrefix/Name [@Image = "ABC"];
//ExtendsList/ClassOrInterfaceType [@Image != "BCD"];
now, is there anyway that I can check these two together. For example, I want it no ABC in class extends BCD. It seems I cannot simply use things like and to connect this two Xpath queries. Also, I noticed I can use | to get connected with them, but | works as or. I need an and here instead of or.
Edit:
I tried something like this:
//PrimaryExpression/PrimaryPrefix/Name[@Image = "ABC"]
[//ancestor::ClassOrInterfaceDeclaration/ExtendsList/
ClassOrInterfaceType[@Image != "BCD"]]
Which seems like it works for me at least. But I am still not 100% sure if this is the correct way since I just tried this out.
Your edit should work, although note that ancestor
axis will recurse all parent nodes, so it should not be introduced with '//'.
Another alternative is to base the Xpath from a common ancestor (e.g. ClassOrInterfaceDeclaration
) and then use and
to ensure the two criteria are met. Since it seems that you are merely testing for the presence of a node meeting both criteria, I guess it doesn't really matter what the resultant expression / node-set actually returns:
//ClassOrInterfaceDeclaration[ExtendsList/ClassOrInterfaceType[@Image != "BCD"]
and descendant::PrimaryExpression/PrimaryPrefix/Name[@Image = "ABC"]]
If however you do need to select a specific node upon success, just append the node's path, relative to ClassOrInterfaceDeclaration
:
//ClassOrInterfaceDeclaration[... predicate ...]/Some/Path/Here
You could also apply a function like count()
to determine the number of nodes meeting the criteria:
count(//ClassOrInterfaceDeclaration[... predicate ...])
And then evaluate the expression.
(Caveat - I'm not familiar with the PMD layout)
I've put some examples showing the effects of //ancestor::
vs ancestor::
and my alternative.
这篇关于结合两个PMD检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!