Skip to content

[java] Fix #3212: Enhance UseStandardCharsets#6597

Merged
adangel merged 7 commits into
pmd:mainfrom
UncleOwen:issue-3212-Enhance-UseStandardCharsets-to-flag-some-constructors-of-IO-related-classes
May 7, 2026
Merged

[java] Fix #3212: Enhance UseStandardCharsets#6597
adangel merged 7 commits into
pmd:mainfrom
UncleOwen:issue-3212-Enhance-UseStandardCharsets-to-flag-some-constructors-of-IO-related-classes

Conversation

@UncleOwen
Copy link
Copy Markdown
Member

Describe the PR

This PR changes UseStandardCharsets to not only violate use of Charset.forName, but also looks for method calls where a charset is named by String while a similar method that takes a Charset exists.

I thought about using a long list of InvocationMatchers (like in RelianceOnDefaultCharsetRule), but decided to try a different approach: The code looks for a method of the same name, where the String argument is replaced with a Charset argument. This way this should automatically work for non-JCL (like apache commons-io mentioned in the ticket) as well.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@codacy-production
Copy link
Copy Markdown

codacy-production Bot commented Apr 15, 2026

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 18 complexity · 0 duplication

Metric Results
Complexity 18
Duplication 0

View in Codacy

TIP This summary will be updated as you push new changes. Give us feedback

@pmd-actions-helper
Copy link
Copy Markdown
Contributor

pmd-actions-helper Bot commented Apr 15, 2026

Documentation Preview

No relevant source code has been changed, pmdtester skipped.

(comment created at 2026-05-07 14:03:29+00:00 for 545f8bb)

@UncleOwen UncleOwen marked this pull request as draft April 15, 2026 12:03
@UncleOwen
Copy link
Copy Markdown
Member Author

Fixed the "new errors" from the regression report. I suspect the logic is still not quite 100% correct (e.g. static vs non-static). But I think these cases should be rare enough that we can ignore them until we see them in real life.

@UncleOwen UncleOwen marked this pull request as ready for review April 15, 2026 13:38
@UncleOwen UncleOwen force-pushed the issue-3212-Enhance-UseStandardCharsets-to-flag-some-constructors-of-IO-related-classes branch from 3a572a3 to 501c641 Compare April 17, 2026 18:42
@UncleOwen UncleOwen force-pushed the issue-3212-Enhance-UseStandardCharsets-to-flag-some-constructors-of-IO-related-classes branch from 18a6a8c to 402d9be Compare April 24, 2026 14:11
@adangel adangel changed the title [java] Fix #3212: enhance UseStandardCharsets [java] Fix #3212: Enhance UseStandardCharsets May 7, 2026
@adangel adangel added this to the 7.25.0 milestone May 7, 2026
Copy link
Copy Markdown
Member

@adangel adangel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

adangel added a commit that referenced this pull request May 7, 2026
@adangel adangel merged commit 545f8bb into pmd:main May 7, 2026
2 checks passed
adangel added a commit that referenced this pull request May 7, 2026
@UncleOwen UncleOwen deleted the issue-3212-Enhance-UseStandardCharsets-to-flag-some-constructors-of-IO-related-classes branch May 7, 2026 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants