checkstyle Method is not designed for extension -

2019-06-22 03:33发布

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.

2条回答
Lonely孤独者°
2楼-- · 2019-06-22 04:11

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.

查看更多
劳资没心,怎么记你
3楼-- · 2019-06-22 04:17

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.

查看更多
登录 后发表回答