Proposed Rule Name: MissingFunctionalInterface ImplicitFunctionalInterface
Proposed Category: Best Practices
Description:
The @FunctionalInterface
annotation declares that the interface is intended to be a functional interface now and in the future. The programmer creating the interface will face compiler errors if they break this contractual statement. Users of the interface can know they can use the interface in lambdas without having to worry that their code will break in a minor update. Of course, in a major update the creator of the interface could remove the @FunctionalInterface
; however, the removal will hopefully help them realize they are making a breaking change.
This new rule will flag all interfaces that could be annotated with @FunctionalInterface
but are not. If the programmer decides that the interface could have additional abstract methods in the future, then the programmer could add @SuppressWarnings("PMD.MissingFunctionalInterface")
. With either annotation, the users of the interface will know the intent of the interface.
Code Sample: This should include code, that should be flagged by the rule. If possible, the "correct" code
according to this new rule should also be demonstrated.
The following interface could have a @FunctionalInterface
or a @SuppressWarnings("PMD.MissingFunctionalInterface")
to make the intent more clear.
public interface MyInterface
{
void doSomething();
}
This interface is clearly a functional interface.
@FunctionalInterface
public interface MyInterface
{
void doSomething();
}
This interface is clearly not a functional interface even though it could be used as such.
@SuppressWarnings("PMD.MissingFunctionalInterface")
public interface MyInterface
{
void doSomething();
}
Possible Properties:
None that I can think of.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4