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

时间:2016-09-14 17:27:01

标签: java xpath xsd pmd

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)=&#39; true&#39;和   (.// BlockStatement/Statement/IfStatement/Statement/Block/BlockStatement/Statement/ReturnStatement/@SingleLine)=&#39; true&#39;] / BlockStatement [last()]

如何编写xpath表达式来查找死代码     在上面的代码System.out.println("in java");中,此行是死代码,如何使用xpath表达式找到此块

1 个答案:

答案 0 :(得分:3)

XPath用于选择XML。这是用于解析Java或任何其他编程语言的错误工具。此外,死代码确定需要可达性计算,通常被认为是语义分析,而不仅仅是语法分析。你甚至不能在XPath中做到这一点。

更新:您现在已在PDM中以XML格式发布了abstract syntax tree。这比让XPath违反原始Java代码更好,但它仍然不足以进行完全可达性分析。您需要PDM的内容是control flow graph,这似乎不是PDM的功能。