tags:

views:

145

answers:

1

Hi,

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 mean ;-)

Thanks

+2  A: 

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'] ]
Lachlan Roche
thanks for that - that works.But another problem - basically I want to check that the two @Image are the same, BUT only if the variable is a String.so Object o = new Object(); should not match... how can I combine these two conditions a) the @Image check and that the found ancestor element also is of type String.I know how to check if its a String, but not how to combine to searches (that they are working on the SAME node).THANKS
martymcfly
@martymcfly What is the test for String type, and on what node?
Lachlan Roche
basically in the ancestor as above.pseudo likethe image is compared to `ancestor:ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration/VariableDeclarator/VariableDeclaratorId/@Image` and the String comparison is made on that ancestor`ancestor::ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration/Type/ReferenceType/ClassOrInterfaceType[@Image='String']`so basically the `FieldDeclaration` is the node which contains the information about String and the Name. But `ClassOrInterfaceBody` is the common ancestor for this and the Synchron.Statements
martymcfly
thanks for the answer again.But I need in both ancestor calls exactly the SAME ancestor. This XPath also validates true if there is any other FieldDeclaration which is a String, not exclusively the one also in the synchronized block.
martymcfly
@martymcfly We need to be able to find a FieldDeclaration from a FieldVariableDeclaratorId. Is there a FieldDeclaration/@Id ?
Lachlan Roche
the hierarchy isClassOrInterfaceBody -> ClassOrInterfaceBodyDeclaration -> FieldDeclaration -> Type | VariableDeclarator -> VariableDeclaratorId.So no FieldVariableDeclaratorId.on the level of ClassOrInterfaceBody there are several ClassOrInterfaceBodyDeclaration, one contains the Field information and one contains the Sync. information
martymcfly
to your question:no they dont have any attribute in common, they are "only" directly related. as one is the child of the other
martymcfly