HDDS-14923. Enable UselessStringValueOf, UseStringBufferForStringAppends in PMD#10007
HDDS-14923. Enable UselessStringValueOf, UseStringBufferForStringAppends in PMD#10007ptlrs wants to merge 4 commits intoapache:masterfrom
Conversation
Gargi-jais11
left a comment
There was a problem hiding this comment.
Thanks @ptlrs for the patch. Please resolve the conflicts in your branch and find the inline comments.
| * @param nextLevel | ||
| * @return subpath | ||
| */ | ||
| @SuppressWarnings("PMD.UseStringBufferForStringAppends") |
There was a problem hiding this comment.
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();
}
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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;
}
adoroszlai
left a comment
There was a problem hiding this comment.
Thanks @ptlrs for the patch.
| if (name == null) { | ||
| name = "_" + RDB_CHECKPOINT_DIR_PREFIX + currentTime; | ||
| } | ||
| checkpointDir += name; | ||
| checkpointDir.append(name); |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
nit: unnecessary toString()
| .append(pipeline.getId().getId().toString()) | |
| .append(pipeline.getId().getId()) |
…-14923-UseStringBufferForStringAppends # Conflicts: # dev-support/pmd/pmd-ruleset.xml
|
Thanks for the reviews @adoroszlai @Gargi-jais11. I have updated the PR. Can you please take another look? |
adoroszlai
left a comment
There was a problem hiding this comment.
Thanks @ptlrs for updatinig the patch.
| StringBuilder sb = new StringBuilder(path) | ||
| .append(removeTrailingSlashIfNeeded(path)); |
There was a problem hiding this comment.
- This duplicates
path. StringBuilderis unnecessary ifnextLevel == 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;| 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); | ||
| } |
There was a problem hiding this comment.
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;
What changes were proposed in this pull request?
This PR enables new PMD rules:
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