rspec/rules/S6217/java/rule.adoc

37 lines
1.2 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
In Java 16 ``++sealed++`` classes reached their second preview stage. This feature is very useful if there is a need to define a strict hierarchy and restrict the possibility of extending classes. In order to mention all the allowed subclasses, there is a keyword ``++permits++``, which should be followed by subclasses' names.
This notation is quite useful if subclasses of a given ``++sealed++`` class can be found in different files, packages, or even modules. In case when all subclasses are declared in the same file there is no need to mention the explicitly and ``++permits++`` part of a declaration can be omitted.
This rule reports an issue if all subclasses of a ``++sealed++`` class are declared in the same file as their superclass.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
sealed class A permits B, C, D, E {} // Noncopliant
final class B extends A {}
final class C extends A {}
final class D extends A {}
final class E extends A {}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
sealed class A {} // Compliant
final class B extends A {}
final class C extends A {}
final class D extends A {}
final class E extends A {}
----
2021-04-28 16:49:39 +02:00
== See
* https://docs.oracle.com/javase/specs/jls/se16/html/jls-8.html#jls-8.10[Records specification]