问题
I have this piece of code and PMD reports two rule violations:
AbstractExceptionHandler has no constructor (AtLeastOneConstructor)
And the field uriInfo is a unused private field (UnusedPrivateField)
@NoArgsConstructor
public class AbstractExceptionHandler { // PMD AtLeastOneConstructor warning here
/** the uriInfo injection. */
@Getter
@Context
private UriInfo uriInfo; // PMD UnusedPrivateField warning here
both warnings are okay, but we use annotations to generate code. So the warning is useless for us.
We have created following suppressions:
AtLeastOneConstructor
<rule ref="rulesets/java/controversial.xml/AtLeastOneConstructor">
<properties>
<property name="violationSuppressXPath"
value="//ClassOrInterfaceDeclaration[//ImportDeclaration//Name[@Image='lombok.NoArgsConstructor'] | //TypeDeclaration//MarkerAnnotation//Name[@Image='NoArgsConstructor']]" />
</properties>
</rule>
UnusedPrivateField
<rule ref="rulesets/java/unusedcode.xml/UnusedPrivateField">
<properties>
<property name="violationSuppressXPath"
value="//ClassOrInterfaceBodyDeclaration//FieldDeclaration[//TypeDeclaration//MarkerAnnotation//Name[@Image='Getter'] | //TypeDeclaration//MarkerAnnotation//Name[@Image='Setter']]"/>
</properties>
</rule>
And the PMD xpath Designer tells us it is the exact used lines with the violation, but still PMD reports a error. Can someone help me out of the dark?
来源:https://stackoverflow.com/questions/28604905/pmd-xpath-to-suppress-atleastoneconstructor-and-unusedprivatefield