gpt4 book ai didi

java - PMD - 检查过多的公共(public)方法(但不包括构造函数和 getters/setters)

转载 作者:行者123 更新时间:2023-11-29 07:22:50 25 4
gpt4 key购买 nike

我希望添加一个 PMD 检查以确保一个类没有太多的公共(public)方法,但我不希望检查中包含构造函数和 getters/setters。

ExcessivePublicCount 检查包括构造函数、getter/setter 和公共(public)变量,我看不到自定义它的方法。

TooManyMethods 检查不包括 getter/setter,但包括其他所有内容(包括私有(private)方法)。校验的XPath代码如下。

//ClassOrInterfaceDeclaration/ClassOrInterfaceBody
[
count(descendant::MethodDeclarator[
not
(
starts-with(@Image,'get')
or
starts-with(@Image,'set')
)
]) > $maxmethods
]

任何人都可以帮助我修改它以实现我想要的,或者建议使用 PMD 的另一种方法吗?

最佳答案

//ClassOrInterfaceDeclaration/ClassOrInterfaceBody [
count(descendant::MethodDeclarator[
..[@Public='true']
and
not
(
starts-with(@Image,'get')
or
starts-with(@Image,'set')
or
starts-with(@Image,'is')
)
] ) > $maxmethods
]

您计算的是 MethodDeclarator,因此不应包含 ctors。
..[@Public='真']
从 MethodDeclarator 返回一个到 MethodDeclaration,然后检查它是否是 public。

关于java - PMD - 检查过多的公共(public)方法(但不包括构造函数和 getters/setters),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1565366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com