Question

When using CodeNarc for static code analysis on Groovy & Grails code, it is not able to figure out class hierarchies. There is a private method in one service class say BaseService. Another service class say ChildService extends the BaseService class. One of the methods in the ChildService calls a private method defined in the BaseService. But the BaseService class does not use the private method anywhere in its own class. So when analyzing the BaseService class in isolation, the private method will appear as unused. But when we look at the class hierarchy, we can understand one of its subclass is calling it.
So, my question is, how does CodeNarc miss that kind of dependency? Doesn't static code analyzers build parse tree and analyze on it? If not how does it analyze code? Any insight into the internals of static code analysis is highly appreciated.
Thanks.

Was it helpful?

Solution

As it says in the 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

So you're going to have to turn this rule off.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top