Skip to content

[java] Enhance UseStandardCharsets to flag some constructors of IO-related classes  #3212

@oowekyala

Description

@oowekyala

Rule: UseStandardCharsets

This is extracted from #3190, fyi @aaime

Is your feature request related to a problem? Please describe. The rule UseStandardCharsets (#3193) flags explicit use of Charset::forName.

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")) { }

Describe the solution you'd like In pmd 7 this is easy to solve. We can use invocation matchers in XPath: ConstructorCall[pmd-java:matchesSig('java.io.OutputStreamWriter#new(_,java.lang.String)')]. Perhaps writing the rule in java would be better though, if there are many ctors to match.

Describe alternatives you've considered Doing this on master. But this would expose us to false positives I think, as these constructors have many overloads and I don't think the overload resolver is very reliable. Since this rule is part of quickstart.xml, I don't want to make concessions about precision.

Metadata

Metadata

Assignees

No one assigned

    Labels

    an:enhancementAn improvement on existing features / rules

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions