Xpath 表达式无法使用 eclipse-PMD 识别 Java 代码中的打印语句

问题描述 投票:0回答:1

我想识别不在条件语句内的打印语句(即检查打印语句是否有任何 if 语句作为其祖先)

几个测试用例如下:

  • 识别下面代码片段中的所有打印语句
int x = 5;
int y = 10;
System.out.println("The value of x is " + x);
System.out.println("The value of y is " + y);
  • 忽略第一个打印语句并识别第二个打印语句,因为第二个打印语句不在 if 语句内。
if (x > 10) {
    System.out.println("x is greater than 10");
}
System.out.println("This is the end of the program");

我已经尝试过下面的 Xpath 表达式,但没有运气,有人可以帮助我吗?

  • 表达式1
//Name[@Image='System']/following-sibling::Name[@Image='out']/following-sibling::Name[@Image='println'][not(ancestor::IfStatement)]
  • 表达式2
//Name[@Image='System']/descendant-or-self::Name[@Image='println'][not(ancestor::IfStatement)]
  • 表达3
//Name[@Image='System']/child::Name[@Image='println'][not(ancestor::IfStatement)]
  • 表达4
//attribute::Image[.='System']/parent::Name/following-sibling::Name[attribute::Image='out']/following-sibling::Name[attribute::Image='println'][not(ancestor::IfStatement)]
  • 表达5
//Name[@Image='System']/preceding-sibling::Name[@Image='out']/preceding-sibling::Name[@Image='println'][not(ancestor::IfStatement)]

请注意,我正在使用 PMD 的 PMD 规则设计器来测试 XPath 表达式。

提前致谢。

java xpath pmd
1个回答
0
投票

我对PMD一无所知,也没有成功设置它,所以这里是一个猜测:

//Name[@Image='System']/Name[@Image='out']/Name[@Image='println'][not(ancestor::IfStatement)]
© www.soinside.com 2019 - 2024. All rights reserved.