我想识别不在条件语句内的打印语句(即检查打印语句是否有任何 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 (x > 10) {
System.out.println("x is greater than 10");
}
System.out.println("This is the end of the program");
我已经尝试过下面的 Xpath 表达式,但没有运气,有人可以帮助我吗?
//Name[@Image='System']/following-sibling::Name[@Image='out']/following-sibling::Name[@Image='println'][not(ancestor::IfStatement)]
//Name[@Image='System']/descendant-or-self::Name[@Image='println'][not(ancestor::IfStatement)]
//Name[@Image='System']/child::Name[@Image='println'][not(ancestor::IfStatement)]
//attribute::Image[.='System']/parent::Name/following-sibling::Name[attribute::Image='out']/following-sibling::Name[attribute::Image='println'][not(ancestor::IfStatement)]
//Name[@Image='System']/preceding-sibling::Name[@Image='out']/preceding-sibling::Name[@Image='println'][not(ancestor::IfStatement)]
请注意,我正在使用 PMD 的 PMD 规则设计器来测试 XPath 表达式。
提前致谢。
我对PMD一无所知,也没有成功设置它,所以这里是一个猜测:
//Name[@Image='System']/Name[@Image='out']/Name[@Image='println'][not(ancestor::IfStatement)]