checkstyle Method is not designed for extension - needs to be abstract, final or empty [duplicate]

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

I have an interface

public interface LdapConnectionFactory {     LdapConnectionWrapper getConnectionWrapper() throws LDAPException; }

and the implementation class LdapConnectionFactoryImpl which implements the getConnectionWrapper() method.

public class LdapConnectionFactoryImpl implements LdapConnectionFactory {     ...     public LdapConnectionWrapper getConnectionWrapper() throws LDAPException {         ...     } }

When I ran checkstyle, it flags the getConnectionWrapper() as error - Method 'getConnectionWrapper' is not designed for extension - needs to be abstract, final or empty. Any advice? Thanks.

回答1:

Checkstyle is saying that either the class or method should be marked as final since it is non-empty and therefore not designed for being overridden.

This is one of the rules of Checkstyle that I don't personally agree with and tend to disable.



回答2:

Checkstyle is throwing this message because you have provided an implementation but have not marked the method as final. You can disable this check via Eclipse Preferences and editing your Checkstyle configuration. The specific rule is Class Design > Design for Extension.

The description for the check is pretty decent, and is as follows:

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.



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