Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

java - Checkstyle "Method Not Designed For Extension" error being incorrectly issued?

I'm using Checkstyle and am getting an error about this method:

public final String getAdmitCodeStatus() {
    return admitCodeStatus;
}

Here's the error I get:

Method 'getAdmitCodeStatus' is not designed for extension - needs to be abstract, final, or empty.

How is that method not compliant? Is there something I'm doing wrong that Checkstyle would bark at me about this method?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...