Checkstyle “Method Not Designed For Extension” error being incorrectly issued?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 23:26:12

It looks to be caused by the DesignForExtension rule. According to the documentation:

Checks that classes are designed for extension. More specifically, it enforces a programming style where superclasses provide empty "hooks" that can be implemented by subclasses.

The exact rule is that nonprivate, nonstatic methods of classes that can be subclassed must either be

abstract or
final or
have an empty implementation

Rationale: This API design style protects superclasses against beeing broken by subclasses. The downside is that subclasses are limited in their flexibility, in particular they cannot prevent execution of code in the superclass, but that also means that subclasses cannot corrupt the state of the superclass by forgetting to call the super method.

Source: http://sonar.15.n6.nabble.com/design-for-extension-rule-tp3200037p3200043.html

But since your method has a final modifier, I'd say you found a bug and might want to log a bug report. https://github.com/checkstyle/checkstyle/issues

At first glance it looks like what on the earth kind of programming style is this... This just checks whether you are planing methods to be inherited or not...and then you can declare them final,abstract or empty implementation. Then you declare it final... ;) Either the class can be final or the methods individual depending on the requirement scenario.

I think this check is useful and most of the times the warning is justified. Sometimes it is not appropriate and then I ignore it.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!