rspec/rules/S4719/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

64 lines
1.5 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

== Why is this an issue?
JDK7 introduced the class ``++java.nio.charset.StandardCharsets++``. It provides constants for all charsets that are guaranteed to be available on every implementation of the Java platform.
* ISO_8859_1
* US_ASCII
* UTF_16
* UTF_16BE
* UTF_16LE
* UTF_8
These constants should be preferred to:
* the use of a String such as "UTF-8" which has the drawback of requiring the ``++catch++``/``++throw++`` of an ``++UnsupportedEncodingException++`` that will never actually happen
* the use of Guavas ``++Charsets++`` class, which has been obsolete since JDK7
=== Noncompliant code example
[source,java]
----
try {
byte[] bytes = string.getBytes("UTF-8"); // Noncompliant; use a String instead of StandardCharsets.UTF_8
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
// ...
byte[] bytes = string.getBytes(Charsets.UTF_8); // Noncompliant; Guava way obsolete since JDK7
----
=== Compliant solution
[source,java]
----
byte[] bytes = string.getBytes(StandardCharsets.UTF_8)
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Replace XXX with StandardCharsets.XXX
'''
== Comments And Links
(visible only on this page)
=== on 10 Jul 2018, 21:46:38 Ann Campbell wrote:
\[~alexandre.gigleux] I suggest you drop the phrase "unless the JVM violates the JDK specifications"
=== on 11 Jul 2018, 08:20:29 Alexandre Gigleux wrote:
\[~ann.campbell.2]: Done
endif::env-github,rspecator-view[]