Use StandardCharsets instead
Proposed Category: Best Practices | Performance (both could apply)
Description:
Starting with Java 7, StandardCharsets provides constants for common Charset objects, such as UTF-8.
Using the constants is less error prone, and can provide a small performance advantage compared to Charset.forName(...)
since no scan across the internal Charset caches is needed.
Code Sample:
This example:
try (OutputStreamWriter osw = new OutputStreamWriter(out, Charset.forName("UTF-8"))) {
can be replaced with:
try (OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
However, there is a number of Java API where an overloaded method takes the Charset defition as a string, e.g. the following should also be flagged and reported:
try (OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8")) {
Possible Properties:
Maybe a list of common constructors/methods that have an overload with possibility of using both a string/Charset?
Trivial implementation
I have written this trivial implementation looking just for Charset.forName cases:
<rule name="StandardCharsets"
language="java"
minimumLanguageVersion="1.8"
message="Please use StandardCharsets constants"
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
StandardCharsets has constants for various common character
sets.
</description>
<priority>3</priority>
<properties>
<property name="version" value="2.0"/>
<property name="xpath">
<value><![CDATA[
//PrimaryExpression[PrimaryPrefix/Name/@Image = 'Charset.forName']/PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix/Literal[@Image = '"US-ASCII"' or @Image = '"ISO-8859-1"' or @Image = '"UTF-8"' or @Image = '"UTF-16BE"' or @Image = '"UTF-16LE"' or @Image = '"UTF-16"']
]]></value>
</property>
</properties>
</rule>
Use StandardCharsets instead
Proposed Category: Best Practices | Performance (both could apply)
Description:
Starting with Java 7, StandardCharsets provides constants for common Charset objects, such as UTF-8.
Using the constants is less error prone, and can provide a small performance advantage compared to
Charset.forName(...)since no scan across the internal Charset caches is needed.
Code Sample:
This example:
can be replaced with:
However, there is a number of Java API where an overloaded method takes the Charset defition as a string, e.g. the following should also be flagged and reported:
Possible Properties:
Maybe a list of common constructors/methods that have an overload with possibility of using both a string/Charset?
Trivial implementation
I have written this trivial implementation looking just for
Charset.forNamecases: