简体   繁体   中英

How to write xpath expression to find dead code

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

THIS IS XML FOR ABOVE JAVA CODE:

<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 expression :

//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()]

How to write xpath expression to find dead code in above code System.out.println("in java"); this line is dead code how can i find this block by using xpath expression

XPath is for selecting XML. It is completely the wrong tool to use to parse Java or any other programming language. Further, dead code determination requires a reachability calculation, typically considered to be semantic analysis, not just a syntactic parse. You're not even close to being able to do this in XPath.


Update: You've now posted an abstract syntax tree from PDM in XML. That's better than having XPath go against raw Java code, but it's still insufficient for full reachability analysis. What you need from PDM is a control flow graph , and that doesn't appear to be a feature of PDM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM