59 lines
1011 B
Plaintext
59 lines
1011 B
Plaintext
== Why is this an issue?
|
|
|
|
``++static++`` methods can be accessed without an instance of the enclosing class, so there's no reason to instantiate a class that has only ``++static++`` methods.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,text]
|
|
----
|
|
public class TextUtils {
|
|
public static String stripHtml(String source) {
|
|
return source.replaceAll("<[^>]+>", "");
|
|
}
|
|
}
|
|
|
|
public class TextManipulator {
|
|
|
|
// ...
|
|
|
|
public void cleanText(String source) {
|
|
TextUtils textUtils = new TextUtils(); // Noncompliant
|
|
|
|
String stripped = textUtils.stripHtml(source);
|
|
|
|
//...
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,text]
|
|
----
|
|
public class TextUtils {
|
|
public static String stripHtml(String source) {
|
|
return source.replaceAll("<[^>]+>", "");
|
|
}
|
|
}
|
|
|
|
public class TextManipulator {
|
|
|
|
// ...
|
|
|
|
public void cleanText(String source) {
|
|
String stripped = TextUtils.stripHtml(source);
|
|
|
|
//...
|
|
}
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Related rules
|
|
|
|
* S1118 - Utility classes should not have public constructors
|
|
|