Skip to content

HDDS-14923. Enable UselessStringValueOf, UseStringBufferForStringAppends in PMD#10007

Draft
ptlrs wants to merge 4 commits intoapache:masterfrom
ptlrs:HDDS-14923-UseStringBufferForStringAppends
Draft

HDDS-14923. Enable UselessStringValueOf, UseStringBufferForStringAppends in PMD#10007
ptlrs wants to merge 4 commits intoapache:masterfrom
ptlrs:HDDS-14923-UseStringBufferForStringAppends

Conversation

@ptlrs
Copy link
Copy Markdown
Contributor

@ptlrs ptlrs commented Mar 30, 2026

What changes were proposed in this pull request?

This PR enables new PMD rules:

  1. UselessStringValueOf No need to call String.valueOf to append to a string where the type can be inferred
  2. UseStringBufferForStringAppends Use StringBuffer for concatenating strings

What is the link to the Apache JIRA

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

How was this patch tested?

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

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

@Gargi-jais11 Gargi-jais11 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. Please resolve the conflicts in your branch and find the inline comments.

* @param nextLevel
* @return subpath
*/
@SuppressWarnings("PMD.UseStringBufferForStringAppends")
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.

Why to do suppression over here? If we are applying this as a rule we should refactor this as well as the change is not very complex.

public static String buildSubpath(String path, String nextLevel) {
    StringBuilder sb = new StringBuilder();
    if (!path.startsWith(OM_KEY_PREFIX)) {
      sb.append(OM_KEY_PREFIX);
    }
    sb.append(path);
    String normalized = removeTrailingSlashIfNeeded(sb.toString());
    if (nextLevel == null) {
      return normalized;
    }
    return new StringBuilder(normalized)
        .append(OM_KEY_PREFIX)
        .append(nextLevel)
        .toString();
  }

Copy link
Copy Markdown
Contributor

@adoroszlai adoroszlai Apr 1, 2026

Choose a reason for hiding this comment

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

The first StringBuilder is not needed.

String subpath = !path.startsWith(OM_KEY_PREFIX)
    ? OM_KEY_PREFIX + path
    : path;

private static final int PATH_INDENT = 27;

@Override
@SuppressWarnings(value = "PMD.UseStringBufferForStringAppends")
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.

I think no need of suppression here as well. Just need to change these below lines 166 - 170 in this method:

String subPath = subPathDU.path("path").asText("");
          // differentiate key from other types
          if (!subPathDU.path("isKey").asBoolean(false)) {
            subPath += OM_KEY_PREFIX;
          }

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 +69 to +72
if (name == null) {
name = "_" + RDB_CHECKPOINT_DIR_PREFIX + currentTime;
}
checkpointDir += name;
checkpointDir.append(name);
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.

String concatenation can be changed to use the outer StringBuilder:

      if (name == null) {
        checkpointDir.append('_')
            .append(RDB_CHECKPOINT_DIR_PREFIX)
            .append(currentTime);
      } else {
        checkpointDir.append(name);
      }

boolean topologyAware) {
String key = pipeline.getId().getId().toString() + pipeline.getType();
StringBuilder key = new StringBuilder()
.append(pipeline.getId().getId().toString())
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: unnecessary toString()

Suggested change
.append(pipeline.getId().getId().toString())
.append(pipeline.getId().getId())

ptlrs added 2 commits April 2, 2026 18:01
…-14923-UseStringBufferForStringAppends

# Conflicts:
#	dev-support/pmd/pmd-ruleset.xml
@ptlrs
Copy link
Copy Markdown
Contributor Author

ptlrs commented Apr 3, 2026

Thanks for the reviews @adoroszlai @Gargi-jais11. I have updated the PR. Can you please take another look?

@ptlrs ptlrs requested review from Gargi-jais11 and adoroszlai April 3, 2026 02:20
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 updatinig the patch.

Comment on lines +107 to +108
StringBuilder sb = new StringBuilder(path)
.append(removeTrailingSlashIfNeeded(path));
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.

  • This duplicates path.
  • StringBuilder is unnecessary if nextLevel == null, and we can use concatenation in the other case
  • Better avoid reassigning parameter path.

So logic can be simplified:

String subpath = !subpath.startsWith(OM_KEY_PREFIX)
    ? OM_KEY_PREFIX + path
    : path;
subpath = removeTrailingSlashIfNeeded(subpath);
return nextLevel != null
    ? subpath + OM_KEY_PREFIX + nextLevel
    : subpath;

Comment on lines +165 to 169
StringBuilder sb = new StringBuilder(subPathDU.path("path").asText(""));
// differentiate key from other types
if (!subPathDU.path("isKey").asBoolean(false)) {
subPath += OM_KEY_PREFIX;
sb.append(OM_KEY_PREFIX);
}
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.

I think PMD is really wrong here: previously the (internal) StringBuilder was created only conditionally, but now it is eagerly created. We can avoid that.

String pathValue = subPathDU.path("path").asText("");
boolean isDir = !subPathDU.path("isKey").asBoolean(false);
String subPath = isDir ? (pathValue + OM_KEY_PREFIX) : pathValue;

Copy link
Copy Markdown
Contributor

@Gargi-jais11 Gargi-jais11 left a comment

Choose a reason for hiding this comment

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

Overall LGTM! Let's update the patch with above comments then its good to go.

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.

3 participants