2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2023-10-18 10:16:10 +02:00
|
|
|
include::../description.adoc[]
|
2021-02-02 15:02:10 +01:00
|
|
|
|
2023-10-18 10:16:10 +02:00
|
|
|
=== Exceptions
|
|
|
|
|
|
|
|
When a class contains `public static void main(String[] args)` method it is not considered as a utility class and will be ignored by this rule.
|
|
|
|
|
|
|
|
include::../howtofix.adoc[]
|
2020-06-30 12:47:33 +02:00
|
|
|
|
2023-10-18 10:16:10 +02:00
|
|
|
=== Code examples
|
|
|
|
|
|
|
|
==== Noncompliant code example
|
2020-06-30 12:47:33 +02:00
|
|
|
|
2023-06-15 15:32:44 +02:00
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
2020-06-30 12:47:33 +02:00
|
|
|
----
|
|
|
|
class StringUtils { // Noncompliant
|
|
|
|
|
|
|
|
public static String concatenate(String s1, String s2) {
|
|
|
|
return s1 + s2;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2023-10-18 10:16:10 +02:00
|
|
|
==== Compliant solution
|
2020-06-30 12:47:33 +02:00
|
|
|
|
2023-06-15 15:32:44 +02:00
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
2020-06-30 12:47:33 +02:00
|
|
|
----
|
|
|
|
class StringUtils { // Compliant
|
|
|
|
|
|
|
|
private StringUtils() {
|
|
|
|
throw new IllegalStateException("Utility class");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String concatenate(String s1, String s2) {
|
|
|
|
return s1 + s2;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
Hide this public constructor.
|
|
|
|
|
|
|
|
Add a private constructor to hide the implicit public one.
|
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2023-06-22 10:38:01 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|