XPath examples – WebService

By , last updated December 11, 2019

In this article we are going to look at some complex PMD XPath examples that include parsing WebServices.

WebServices

Any WebService has a main class and we want that top-level logging happens there. We create our own LoggUtil and now we need to be sure that all our programmers use it while creating WebServices.

@javax.jws.WebService
public class Foo {
     private LoggUtil log = new LoggUtil(Foo.class);
}

In order to do that we need:
a) find all classes in the project with annotation @javax.jws.WebService:

//Annotation/NormalAnnotation[(Name/@Image='javax.jws.WebService')]

b) look if these classes have LoggUtil instances:

//VariableDeclarator[( contains(../Type/ReferenceType/ClassOrInterfaceType/attribute::Image,'LoggUtil'))]

To set it all together we look again through part 1 and produce the following code:

![CDATA[
  //Annotation/NormalAnnotation
  [
    (
       Name/@Image='javax.jws.WebService'
    )
    and
    not
    (
       //VariableDeclarator
       [
         (
            contains (../Type/ReferenceType/ClassOrInterfaceType/attribute::Image,'LoggUtil')
         )
       ]
    )
   ]
]]

WebService with a static log-instance

Now let’s take our class Foo and add a static identifier to the LoggUtil:

@javax.jws.WebService
public class Foo {
     private static LoggUtil log = new LoggUtil(Foo.class);
}
![CDATA[
	//Annotation/NormalAnnotation
	[
		(
			Name/@Image='javax.jws.WebService'
		)
		and
		(
			//VariableDeclarator
			[parent::FieldDeclaration]
			[
				(../Type/ReferenceType/ClassOrInterfaceType[@Image='LoggUtil'])
				and
				(..[@Static = 'false'] )
			]
			)
	]
]]

Senior Software Engineer developing all kinds of stuff.