开发者

XPath find element based on ancestor element

again I have Java AST which is created from

public class Test  {
     String o = new String("hh");
     public void wrong1() {
       synchronized(o) {
             // huhu
      }
   }
}

I try to create a XPath query which finds the synchronized block in which the defined String variable o is used.

As the definition is above it is an ancestor of the SynchronizedStatement, but I dont get it working

//SynchronizedStatement[Expression/PrimaryExpression/PrimaryPrefix/Name[@Image=ancestor::ClassOrInterfaceBody[ClassOrInterfaceBodyDeclaration/FieldVariableDeclaratorId/@Image]]]

I know that /SynchronizedStatement[Expression/PrimaryExpression/PrimaryPrefix/Name[@Image= is correct, my problem is how to address the ancestor ClassOrInterfaceBody part.

Hope its clear what i 开发者_JS百科mean ;-)

Thanks


To test that the two @Image are the same.

//SynchronizedStatement[ Expression/PrimaryExpression/PrimaryPrefix/Name/@Image = ancestor::ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldVariableDeclaratorId/@Image ]

To also test for String type, assuming that FieldDeclaration and FieldVariableDeclaratorId have a common attribute (name).

//SynchronizedStatement[
    Expression/PrimaryExpression/PrimaryPrefix/Name/@Image = 
    ancestor::ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldVariableDeclaratorId[
        @name = ancestor::ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration[Type/ReferenceType/ClassOrInterfaceType/@Image = 'String']/@name
    ]/@Image
]

If the @Image from the original XPath is "String":

//SynchronizedStatement[ Expression/PrimaryExpression/PrimaryPrefix/Name/@Image = ancestor::ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldVariableDeclaratorId/@Image[. = 'String'] ]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜