CodeNarc的静态代码分析无法找到正确的类依赖关系

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

当使用CodeNarc对Groovy&Grails代码进行静态代码分析时,它无法找出类层次结构。在一个服务类(称为BaseService)中有一个私有(private)方法。另一个服务类说ChildService扩展了BaseService类。 ChildService中的方法之一调用在BaseService中定义的私有(private)方法。但是BaseService类在其自己的类中的任何地方都不使用私有(private)方法。因此,当单独分析BaseService类时,私有(private)方法将显示为未使用。但是,当我们查看类层次结构时,我们可以理解其子类之一正在调用它。
所以,我的问题是,CodeNarc如何错过这种依赖关系?静态代码分析器是否不构建分析树并对其进行分析?如果不是,它将如何分析代码?任何对静态代码分析内部的见解都将受到高度赞赏。
谢谢。

解决方案如下:

就像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 x.name)
  • 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