对该包中的类不进行验证
使用PMD 7.22.0
需要在 TooManyFields 检查中排除来自某些包及其嵌套包的类。示例:com.example.dto.Dto
这个回答中的解决方案不起作用:
https://stackoverflow.com/a/68502378/17965846
看起来像是这样的:
<rule ref="category/java/design.xml/TooManyFields">
<properties>
<property name="violationSuppressXPath"
vvalue="//ClassOrInterfaceDeclaration['.*/dto/.*']"/>
</properties>
</rule>
更新:由于我的问题已被关闭,我在此给出一个回答。感谢 @somil-jain的回答,以下解决方案可行:
<rule ref="category/java/design.xml/TooManyFields">
<properties>
<property name="violationSuppressXPath"
value="//ClassDeclaration[matches(@BinaryName, '\.(dto|entities)\.')]"/>
</properties>
</rule>
解决方案
你提供的代码片段中有 两个问题:
- 有一个打字错误:
vvalue→ 应该是value。 - ClassOrInterfaceDeclaration在 PMD 7中被移除了。这个节点现在只是ClassDeclaration。
- PMD 7的 XPath语法无效:使用
ClassOrInterfaceDeclaration['.*/dto/.*']是无效的。在PMD 7中,violationSuppressXPath将直接在带有违规的节点上进行求值。你应该使用.对当前类节点的引用,并检查它的@BinaryName属性(其中包含完整的包路径)或它的@SimpleName属性。
纠正如下:
<rule ref="category/java/design.xml/TooManyFields">
<properties>
<property name="violationSuppressXPath"
value=".[matches(@BinaryName, '\.dto\.') or matches(@SimpleName, 'DTO$')]"/>
</properties>
</rule>
即便这个版本失败,也可以尝试;
<rule ref="category/java/design.xml/TooManyFields">
<properties>
<property name="violationSuppressXPath">
<value>
<![CDATA[
./ancestor::CompilationUnit[contains(concat('.', @PackageName, '.'), '.dto.')]
]]>
</value>
</property>
</properties>
</rule>
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。