gpt4 book ai didi

java - 如何编写xpath表达式来查找死代码

转载 作者:行者123 更新时间:2023-12-01 21:18:37 27 4
gpt4 key购买 nike

public class IdentifyProblemsInCode {
public void howToDoInJava_method3() {
System.out.println("how to do");
if (true) {
return;
}
System.out.println("in java");
}
}

这是上述 JAVA 代码的 XML:

<Block BeginColumn="41" BeginLine="4" EndColumn="5" EndLine="10" FindBoundary="false" Image="" SingleLine="false" containsComment="false">
<BlockStatement Allocation="false" BeginColumn="9" BeginLine="5" EndColumn="40" EndLine="5" FindBoundary="false" Image="" SingleLine="true">
<Statement BeginColumn="9" BeginLine="5" EndColumn="40" EndLine="5" FindBoundary="false" Image="" SingleLine="true">
<StatementExpression BeginColumn="9" BeginLine="5" EndColumn="39" EndLine="5" FindBoundary="false" Image="" SingleLine="true">
<PrimaryExpression BeginColumn="9" BeginLine="5" EndColumn="39" EndLine="5" FindBoundary="false" Image="" SingleLine="true">
<PrimaryPrefix BeginColumn="9" BeginLine="5" EndColumn="26" EndLine="5" FindBoundary="false" Image="" SingleLine="true" SuperModifier="false" ThisModifier="false">
<Name BeginColumn="9" BeginLine="5" EndColumn="26" EndLine="5" FindBoundary="false" Image="System.out.println" SingleLine="true"/>
</PrimaryPrefix>
<PrimarySuffix ArgumentCount="1" Arguments="true" ArrayDereference="false" BeginColumn="27" BeginLine="5" EndColumn="39" EndLine="5" FindBoundary="false" Image="" SingleLine="true">
<Arguments ArgumentCount="1" BeginColumn="27" BeginLine="5" EndColumn="39" EndLine="5" FindBoundary="false" Image="" SingleLine="true">
<ArgumentList BeginColumn="28" BeginLine="5" EndColumn="38" EndLine="5" FindBoundary="false" Image="" SingleLine="true">
<Expression BeginColumn="28" BeginLine="5" EndColumn="38" EndLine="5" FindBoundary="false" Image="" SingleLine="true">
<PrimaryExpression BeginColumn="28" BeginLine="5" EndColumn="38" EndLine="5" FindBoundary="false" Image="" SingleLine="true">
<PrimaryPrefix BeginColumn="28" BeginLine="5" EndColumn="38" EndLine="5" FindBoundary="false" Image="" SingleLine="true" SuperModifier="false" ThisModifier="false">
<Literal BeginColumn="28" BeginLine="5" CharLiteral="false" EndColumn="38" EndLine="5" FindBoundary="false" FloatLiteral="false" Image="&quot;how to do&quot;" IntLiteral="false" SingleCharacterStringLiteral="false" SingleLine="true" StringLiteral="true"/>
</PrimaryPrefix>
</PrimaryExpression>
</Expression>
</ArgumentList>
</Arguments>
</PrimarySuffix>
</PrimaryExpression>
</StatementExpression>
</Statement>
</BlockStatement>
<BlockStatement Allocation="false" BeginColumn="9" BeginLine="6" EndColumn="9" EndLine="8" FindBoundary="false" Image="" SingleLine="false">
<Statement BeginColumn="9" BeginLine="6" EndColumn="9" EndLine="8" FindBoundary="false" Image="" SingleLine="false">
<IfStatement BeginColumn="9" BeginLine="6" Else="false" EndColumn="9" EndLine="8" FindBoundary="false" Image="" SingleLine="false">
<Expression BeginColumn="13" BeginLine="6" EndColumn="16" EndLine="6" FindBoundary="false" Image="" SingleLine="true">
<PrimaryExpression BeginColumn="13" BeginLine="6" EndColumn="16" EndLine="6" FindBoundary="false" Image="" SingleLine="true">
<PrimaryPrefix BeginColumn="13" BeginLine="6" EndColumn="16" EndLine="6" FindBoundary="false" Image="" SingleLine="true" SuperModifier="false" ThisModifier="false">
<Literal BeginColumn="13" BeginLine="6" CharLiteral="false" EndColumn="16" EndLine="6" FindBoundary="false" FloatLiteral="false" Image="" IntLiteral="false" SingleCharacterStringLiteral="false" SingleLine="true" StringLiteral="false">
<BooleanLiteral BeginColumn="13" BeginLine="6" EndColumn="16" EndLine="6" FindBoundary="false" Image="" SingleLine="true" True="true"/>
</Literal>
</PrimaryPrefix>
</PrimaryExpression>
</Expression>
<Statement BeginColumn="19" BeginLine="6" EndColumn="9" EndLine="8" FindBoundary="false" Image="" SingleLine="false">
<Block BeginColumn="19" BeginLine="6" EndColumn="9" EndLine="8" FindBoundary="false" Image="" SingleLine="false" containsComment="false">
<BlockStatement Allocation="false" BeginColumn="13" BeginLine="7" EndColumn="19" EndLine="7" FindBoundary="false" Image="" SingleLine="true">
<Statement BeginColumn="13" BeginLine="7" EndColumn="19" EndLine="7" FindBoundary="false" Image="" SingleLine="true">
<ReturnStatement BeginColumn="13" BeginLine="7" EndColumn="19" EndLine="7" FindBoundary="false" Image="" SingleLine="true"/>
</Statement>
</BlockStatement>
</Block>
</Statement>
</IfStatement>
</Statement>
</BlockStatement>
<BlockStatement Allocation="false" BeginColumn="9" BeginLine="9" EndColumn="38" EndLine="9" FindBoundary="false" Image="" SingleLine="true">
<Statement BeginColumn="9" BeginLine="9" EndColumn="38" EndLine="9" FindBoundary="false" Image="" SingleLine="true">
<StatementExpression BeginColumn="9" BeginLine="9" EndColumn="37" EndLine="9" FindBoundary="false" Image="" SingleLine="true">
<PrimaryExpression BeginColumn="9" BeginLine="9" EndColumn="37" EndLine="9" FindBoundary="false" Image="" SingleLine="true">
<PrimaryPrefix BeginColumn="9" BeginLine="9" EndColumn="26" EndLine="9" FindBoundary="false" Image="" SingleLine="true" SuperModifier="false" ThisModifier="false">
<Name BeginColumn="9" BeginLine="9" EndColumn="26" EndLine="9" FindBoundary="false" Image="System.out.println" SingleLine="true"/>
</PrimaryPrefix>
<PrimarySuffix ArgumentCount="1" Arguments="true" ArrayDereference="false" BeginColumn="27" BeginLine="9" EndColumn="37" EndLine="9" FindBoundary="false" Image="" SingleLine="true">
<Arguments ArgumentCount="1" BeginColumn="27" BeginLine="9" EndColumn="37" EndLine="9" FindBoundary="false" Image="" SingleLine="true">
<ArgumentList BeginColumn="28" BeginLine="9" EndColumn="36" EndLine="9" FindBoundary="false" Image="" SingleLine="true">
<Expression BeginColumn="28" BeginLine="9" EndColumn="36" EndLine="9" FindBoundary="false" Image="" SingleLine="true">
<PrimaryExpression BeginColumn="28" BeginLine="9" EndColumn="36" EndLine="9" FindBoundary="false" Image="" SingleLine="true">
<PrimaryPrefix BeginColumn="28" BeginLine="9" EndColumn="36" EndLine="9" FindBoundary="false" Image="" SingleLine="true" SuperModifier="false" ThisModifier="false">
<Literal BeginColumn="28" BeginLine="9" CharLiteral="false" EndColumn="36" EndLine="9" FindBoundary="false" FloatLiteral="false" Image="&quot;in java&quot;" IntLiteral="false" SingleCharacterStringLiteral="false" SingleLine="true" StringLiteral="true"/>
</PrimaryPrefix>
</PrimaryExpression>
</Expression>
</ArgumentList>
</Arguments>
</PrimarySuffix>
</PrimaryExpression>
</StatementExpression>
</Statement>
</BlockStatement>
</Block>

Xpath 表达式:

//MethodDeclaration[count(ResultType)=1]/Block[(.//BlockStatement/Statement/IfStatement/Expression/PrimaryExpression/PrimaryPrefix/Literal/BooleanLiteral/@True )='true' and (.//BlockStatement/Statement/IfStatement/Statement/Block/BlockStatement/Statement/ReturnStatement/@SingleLine )='true']/BlockStatement[last()]

如何编写xpath表达式来查找死代码 在上面的代码中 System.out.println("in java"); 这一行是死代码,我如何通过使用 xpath 表达式找到这个 block

最佳答案

XPath 用于选择 XML。用它来解析 Java 或任何其他编程语言是完全错误的工具。此外,死代码确定需要可达性计算,通常被认为是语义分析,而不仅仅是语法分析。您距离在 XPath 中执行此操作还差得很远。

<小时/>

更新:您现在已经发布了 abstract syntax tree来自 XML 格式的 PDM。这比让 XPath 对抗原始 Java 代码要好,但对于完整的可达性分析来说仍然不够。您需要从 PDM 获得的是 control flow graph ,这似乎不是 PDM 的功能。

关于java - 如何编写xpath表达式来查找死代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39496381/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com