PMD XPath: example

Now we are going to look at some complex examples.

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'] )
       ]
    )
  ]
]]>

Popularity: 3% [?]

Leave a Reply

Leave a Reply
  • (required)
  • (required) (will not be published)