rspec/rules/S6220/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

63 lines
1.5 KiB
Plaintext

== Why is this an issue?
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++``.
=== Noncompliant code example
[source,java]
----
public sealed interface F permits ... { // Noncompliant
void f();
}
----
=== Compliant solution
[source,java]
----
public interface F { // Compliant
void f();
}
----
== Resources
* 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)
=== Message
Remove this "sealed" keyword if this interface is supposed to be functional
=== Highlighting
interface declaration
'''
== Comments And Links
(visible only on this page)
=== on 22 June 2021, 15:53:00 Alban Auzeill wrote:
For the `java` analyzer we decided to not implement this rule,
see why on https://github.com/SonarSource/sonar-java/pull/3658[*this github PR*]
and https://jira.sonarsource.com/browse/[SONARJAVA-3775]
endif::env-github,rspecator-view[]