Skip to content

HDDS-14918. Enable PMD rules StringToString, UseArraysAsList and UseIndexOfChar#9999

Open
ptlrs wants to merge 4 commits intoapache:masterfrom
ptlrs:HDDS-14918-enable-pmd-rules-StringToString
Open

HDDS-14918. Enable PMD rules StringToString, UseArraysAsList and UseIndexOfChar#9999
ptlrs wants to merge 4 commits intoapache:masterfrom
ptlrs:HDDS-14918-enable-pmd-rules-StringToString

Conversation

@ptlrs
Copy link
Copy Markdown
Contributor

@ptlrs ptlrs commented Mar 29, 2026

What changes were proposed in this pull request?

This PR enabled the following PMD rules:

  • StringToString Avoid calling toString() on objects already known to be string instances
  • UseArraysAsList Use Arrays.asList() instead of copying each element one by one from an array
  • UseIndexOfChar Use String.indexOf(char) when checking for the index of a single character; it executes faster.

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/HDDS-14918

How was this patch tested?

CI: https://github.com/ptlrs/ozone/actions/runs/23707401043

@adoroszlai adoroszlai changed the title HDDS-14918: Enable StringToString, UseArraysAsList and UseIndexOfChar in PMD HDDS-14918. Enable PMD rules StringToString, UseArraysAsList and UseIndexOfChar Mar 29, 2026
Copy link
Copy Markdown
Contributor

@adoroszlai adoroszlai left a comment

Choose a reason for hiding this comment

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

Thanks @ptlrs for the patch.

Comment on lines +46 to +48
<rule ref="category/java/performance.xml/StringToString"/>
<rule ref="category/java/performance.xml/UseArraysAsList"/>
<rule ref="category/java/performance.xml/UseIndexOfChar"/>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: please keep alphabetical order, which makes it easier to spot enabled rules at a glance

String attName = attr.getName();
if (!"modelerType".equals(attName)) {
if (attName.indexOf("=") < 0 && attName.indexOf(":") < 0 && attName.indexOf(" ") < 0) {
if (!attName.contains("=") && !attName.contains(":") && !attName.contains(" ")) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Replace indexOf(String) with indexOf(int), not contains(String), for efficiency.

Comment on lines +234 to +236
cause = cause.substring(cause.indexOf(':') + 2);
remote = new RemoteException(cause.substring(0, cause.indexOf(':')),
cause.substring(cause.indexOf(':') + 1));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: store cause.indexOf(':')

Comment on lines +118 to +120
String trashDirectory = (keyName.contains("/")
? new Path(userTrashCurrent, keyName.substring(0,
keyName.lastIndexOf("/")))
keyName.lastIndexOf('/')))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: store keyName.lastIndexOf('/') to replace contains("/")

Comment on lines +147 to +148
if (host.contains(":")) {
return host.substring(0, host.lastIndexOf(":"));
return host.substring(0, host.lastIndexOf(':'));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: store lastIndexOf and replace contains

int idx = name.indexOf('@');
if (idx != -1) {
pidStr = name.substring(0, name.indexOf("@"));
pidStr = name.substring(0, name.indexOf('@'));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
pidStr = name.substring(0, name.indexOf('@'));
pidStr = name.substring(0, idx);

@adoroszlai adoroszlai added the code-cleanup Changes that aim to make code better, without changing functionality. label Mar 29, 2026
@ptlrs
Copy link
Copy Markdown
Contributor Author

ptlrs commented Mar 29, 2026

Thanks for the review @adoroszlai. I have pushed the fixes.

@adoroszlai adoroszlai marked this pull request as ready for review March 30, 2026 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code-cleanup Changes that aim to make code better, without changing functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants