rspec/rules/S6220/java/rule.adoc

51 lines
1.2 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
Java 15 introduced feature of ``++sealed++`` classes. With ``++sealed++`` classes and interfaces you can specify a strict hierarchy of types and restrict possible inheritance.
Although this feature can help to make code safer, it is not applicable everywhere. Functional interfaces can not be sealed. This means that if an interface with a single abstract method is declared with a ``++sealed++`` keyword, its implementation can't be replaced with a lambda.
This rule reports an issue when an interface with a single abstract method is marked ``++sealed++``.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
public sealed interface F permits ... { // Noncompliant
void f();
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
public interface F { // Compliant
void f();
}
----
2021-04-28 16:49:39 +02:00
== See
* https://docs.oracle.com/javase/specs/jls/se16/preview/specs/sealed-classes-jls.html#jls-8.1.1.2[Sealed classes specification]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
2021-06-22 16:01:14 +02:00
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]