2019年7月1日 14点热度 0条评论

当使用CodeNarc对Groovy&Grails代码进行静态代码分析时,它无法找出类层次结构。在一个服务类(称为BaseService)中有一个私有(private)方法。另一个服务类说ChildService扩展了BaseService类。 ChildService中的方法之一调用在BaseService中定义的私有(private)方法。但是BaseService类在其自己的类中的任何地方都不使用私有(private)方法。因此,当单独分析BaseService类时,私有(private)方法将显示为未使用。但是,当我们查看类层次结构时,我们可以理解其子类之一正在调用它。


就像documentation for codenarc中所说的:

UnusedPrivateMethod Rule

Checks for private methods that are not referenced within the same class. Note that the private modifier is not currently "respected" by Groovy code (i.e., Groovy can access private members within other classes).

Known limitations:

  • Does not recognize method reference through property access (e.g. getName() accessed as
  • Does not recognize method invocations when method name is a GString (e.g. this."$methodName"())
  • Does not recognize invoking private method of another instance (i.e. other than this)
  • Does not differentiate between multiple private methods with the same name but different parameters (i.e., overloaded)
  • Does not check for unused constructors

have to turn this rule off