开发者

PMD - check for too many public methods (but exclude constructors and getters/setters)

开发者 https://www.devze.com 2022-12-09 00:41 出处:网络
I wish to add a PMD check to ensure that a class does not have too many public methods, but I do not want constructors and getters/setters to be included in the check.

I wish to add a PMD check to ensure that a class does not have too many public methods, but I do not want constructors and getters/setters to be included in the check.

The ExcessivePublicCount 开发者_高级运维check includes constructors, getters/setters and public variables, and I can't see a way to customise it.

The TooManyMethods check excludes getters/setters, but includes everything else (including private methods). The XPath code for the check is as follows.

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

Can anyone help me out with modifying this to achieve what I want, or suggest another way to do this with 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
]

You are counting MethodDeclarator so ctors should not be included.
..[@Public='true']
Go back one from the MethodDeclarator to the MethodDeclaration, and then check if it is public.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号