forked from phoedos/pmd
@ -14,11 +14,23 @@ This is a {{ site.pmd.release_type }} release.
|
||||
|
||||
### New and noteworthy
|
||||
|
||||
#### New rules
|
||||
|
||||
* The new Java rule {% rule "java/bestpractices/UseStandardCharsets" %} finds usages of `Charset.forName`,
|
||||
where `StandardCharsets` can be used instead.
|
||||
|
||||
This rule is also part of the Quickstart Ruleset (`rulesets/java/quickstart.xml`) for Java.
|
||||
|
||||
### Fixed Issues
|
||||
|
||||
* java-bestpractices
|
||||
* [#3190](https://github.com/pmd/pmd/issues/3190): \[java] Use StandardCharsets instead of Charset.forName
|
||||
|
||||
### API Changes
|
||||
|
||||
### External Contributions
|
||||
|
||||
* [#3193](https://github.com/pmd/pmd/pull/3193): \[java] New rule: UseStandardCharsets - [Andrea Aime](https://github.com/aaime)
|
||||
|
||||
{% endtocmaker %}
|
||||
|
||||
|
13
pmd-core/src/main/resources/rulesets/releases/6340.xml
Normal file
13
pmd-core/src/main/resources/rulesets/releases/6340.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0"?>
|
||||
|
||||
<ruleset name="6340"
|
||||
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
|
||||
<description>
|
||||
This ruleset contains links to rules that are new in PMD v6.34.0
|
||||
</description>
|
||||
|
||||
<rule ref="category/java/bestpractices.xml/UseStandardCharsets" />
|
||||
|
||||
</ruleset>
|
@ -1831,34 +1831,36 @@ public class Foo {
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<rule name="UseStandardCharsets"
|
||||
language="java"
|
||||
since="6.34"
|
||||
minimumLanguageVersion="1.7"
|
||||
message="Please use StandardCharsets constants"
|
||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#usestandardcharsets">
|
||||
<description>
|
||||
<rule name="UseStandardCharsets"
|
||||
language="java"
|
||||
since="6.34.0"
|
||||
minimumLanguageVersion="1.7"
|
||||
message="Please use StandardCharsets constants"
|
||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#usestandardcharsets">
|
||||
<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.
|
||||
</description>
|
||||
<priority>3</priority>
|
||||
<properties>
|
||||
<property name="version" value="2.0"/>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
</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"', '"ISO-8859-1"', '"UTF-8"', '"UTF-16BE"', '"UTF-16LE"', '"UTF-16"')]
|
||||
//PrimaryExpression[PrimaryPrefix/Name/@Image = 'Charset.forName']
|
||||
/PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix
|
||||
/Literal[@Image = ('"US-ASCII"', '"ISO-8859-1"', '"UTF-8"', '"UTF-16BE"', '"UTF-16LE"', '"UTF-16"')]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<example>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<example>
|
||||
<![CDATA[
|
||||
public class UseStandardCharsets {
|
||||
public void run() {
|
||||
|
||||
|
||||
// looking up the charset dynamically
|
||||
try (OutputStreamWriter osw = new OutputStreamWriter(out, Charset.forName("UTF-8"))) {
|
||||
osw.write("test");
|
||||
@ -1871,10 +1873,10 @@ public class UseStandardCharsets {
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<rule name="UseTryWithResources"
|
||||
<rule name="UseTryWithResources"
|
||||
language="java"
|
||||
minimumLanguageVersion="1.7"
|
||||
since="6.12.0"
|
||||
|
@ -53,6 +53,7 @@
|
||||
<rule ref="category/java/bestpractices.xml/UseAssertSameInsteadOfAssertTrue"/>
|
||||
<rule ref="category/java/bestpractices.xml/UseAssertTrueInsteadOfAssertEquals"/>
|
||||
<rule ref="category/java/bestpractices.xml/UseCollectionIsEmpty"/>
|
||||
<rule ref="category/java/bestpractices.xml/UseStandardCharsets" />
|
||||
<!-- <rule ref="category/java/bestpractices.xml/UseTryWithResources" /> -->
|
||||
<!-- <rule ref="category/java/bestpractices.xml/UseVarargs" /> -->
|
||||
<!-- <rule ref="category/java/bestpractices.xml/WhileLoopWithLiteralBoolean" /> -->
|
||||
|
Reference in New Issue
Block a user