
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.
62 lines
1.7 KiB
Plaintext
62 lines
1.7 KiB
Plaintext
== Why is this an issue?
|
|
|
|
The underlying implementation of ``++String::replaceAll++`` calls the ``++java.util.regex.Pattern.compile()++`` method each time it is called even if the first argument is not a regular expression. This has a significant performance cost and therefore should be used with care.
|
|
|
|
|
|
When ``++String::replaceAll++`` is used, the first argument should be a real regular expression. If it's not the case, ``++String::replace++`` does exactly the same thing as ``++String::replaceAll++`` without the performance drawback of the regex.
|
|
|
|
|
|
This rule raises an issue for each ``++String::replaceAll++`` used with a ``++String++`` as first parameter which doesn't contains special regex character or pattern.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
String init = "Bob is a Bird... Bob is a Plane... Bob is Superman!";
|
|
String changed = init.replaceAll("Bob is", "It's"); // Noncompliant
|
|
changed = changed.replaceAll("\\.\\.\\.", ";"); // Noncompliant
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
String init = "Bob is a Bird... Bob is a Plane... Bob is Superman!";
|
|
String changed = init.replace("Bob is", "It's");
|
|
changed = changed.replace("...", ";");
|
|
----
|
|
Or, with a regex:
|
|
|
|
[source,java]
|
|
----
|
|
String init = "Bob is a Bird... Bob is a Plane... Bob is Superman!";
|
|
String changed = init.replaceAll("\\w*\\sis", "It's");
|
|
changed = changed.replaceAll("\\.{3}", ";");
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
* S4248 - Regex patterns should not be created needlessly
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Replace this call to "replaceAll" by a call to the "replace" method
|
|
|
|
|
|
=== Highlighting
|
|
|
|
the method name
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|