Skip to content

SONARJAVA-6446 Refactor merging list of arguments to MethodMatchers#5657

Merged
tomasz-tylenda-sonarsource merged 5 commits into
masterfrom
tt/string-varargs
Jun 5, 2026
Merged

SONARJAVA-6446 Refactor merging list of arguments to MethodMatchers#5657
tomasz-tylenda-sonarsource merged 5 commits into
masterfrom
tt/string-varargs

Conversation

@tomasz-tylenda-sonarsource
Copy link
Copy Markdown
Contributor

@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource commented Jun 5, 2026


Summary by Gitar

  • New utility method:
    • Added StringUtils.flatten to concatenate String, String[], and Collection<String> arguments into a single String[] array.
  • Refactoring:
    • Simplified MethodMatchers definitions in UnitTestUtils by replacing complex Stream.concat logic with the new flatten helper.
  • Testing:
    • Added StringUtilsTest.testFlatten to verify argument merging and exception handling for unsupported types.

This will update automatically on new commits.

@hashicorp-vault-sonar-prod hashicorp-vault-sonar-prod Bot changed the title Refactor merging list of arguments to MethodMatchers SONARJAVA-6446 Refactor merging list of arguments to MethodMatchers Jun 5, 2026
@hashicorp-vault-sonar-prod
Copy link
Copy Markdown
Contributor

hashicorp-vault-sonar-prod Bot commented Jun 5, 2026

SONARJAVA-6446

Comment on lines +53 to +67
public static String[] stringArgs(Object ... args) {
List<String> result = new ArrayList<>();
for (Object arg : args) {
if (arg instanceof String s) {
result.add(s);
} else if (arg instanceof String[] arr) {
Collections.addAll(result, arr);
} else if (arg instanceof Collection<?> col) {
for (Object o : col) {
if (o instanceof String s) {
result.add(s);
} else {
throw new IllegalArgumentException("Unsupported collection element type: " + o.getClass());
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Edge Case: stringArgs throws NPE on null argument instead of IllegalArgumentException

In StringUtils.stringArgs(Object... args), a null element in args (or a null element inside a passed Collection) does not match any instanceof branch and falls through to throw new IllegalArgumentException("Unsupported argument type: " + arg.getClass()). Since arg is null, arg.getClass() throws a NullPointerException rather than the intended IllegalArgumentException, and the message is never produced. All current call sites pass non-null string literals and constants, so this is not triggered today, but the method is public and the failure mode is misleading. Consider handling null explicitly, e.g. if (arg == null) throw new IllegalArgumentException("Unsupported argument type: null"); (and similarly for collection elements).

Was this helpful? React with 👍 / 👎

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.

The change looks OK to me but I would rather we keep the code simpler and make it evolve if we feel a need for a dedicated type of exception message when we encounter the issue in a collection or a regular argument

Comment thread java-checks/src/main/java/org/sonar/java/checks/helpers/StringUtils.java Outdated
Comment thread java-checks/src/main/java/org/sonar/java/checks/helpers/StringUtils.java Outdated
Comment thread java-checks/src/main/java/org/sonar/java/checks/helpers/StringUtils.java Outdated
…Utils.java

Co-authored-by: Dorian Burihabwa <75226315+dorian-burihabwa-sonarsource@users.noreply.github.com>
…Utils.java

Co-authored-by: Dorian Burihabwa <75226315+dorian-burihabwa-sonarsource@users.noreply.github.com>
@sonarqube-next
Copy link
Copy Markdown

sonarqube-next Bot commented Jun 5, 2026

@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource enabled auto-merge (squash) June 5, 2026 15:35
@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource merged commit 7d839d4 into master Jun 5, 2026
15 checks passed
@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource deleted the tt/string-varargs branch June 5, 2026 15:44
@gitar-bot
Copy link
Copy Markdown

gitar-bot Bot commented Jun 5, 2026

Code Review 👍 Approved with suggestions 0 resolved / 1 findings

Simplifies MethodMatchers argument list merging by introducing a new StringUtils helper method. Note that stringArgs currently throws an NPE on null inputs instead of the expected IllegalArgumentException.

💡 Edge Case: stringArgs throws NPE on null argument instead of IllegalArgumentException

📄 java-checks/src/main/java/org/sonar/java/checks/helpers/StringUtils.java:53-67

In StringUtils.stringArgs(Object... args), a null element in args (or a null element inside a passed Collection) does not match any instanceof branch and falls through to throw new IllegalArgumentException("Unsupported argument type: " + arg.getClass()). Since arg is null, arg.getClass() throws a NullPointerException rather than the intended IllegalArgumentException, and the message is never produced. All current call sites pass non-null string literals and constants, so this is not triggered today, but the method is public and the failure mode is misleading. Consider handling null explicitly, e.g. if (arg == null) throw new IllegalArgumentException("Unsupported argument type: null"); (and similarly for collection elements).

🤖 Prompt for agents
Code Review: Simplifies MethodMatchers argument list merging by introducing a new StringUtils helper method. Note that stringArgs currently throws an NPE on null inputs instead of the expected IllegalArgumentException.

1. 💡 Edge Case: stringArgs throws NPE on null argument instead of IllegalArgumentException
   Files: java-checks/src/main/java/org/sonar/java/checks/helpers/StringUtils.java:53-67

   In `StringUtils.stringArgs(Object... args)`, a `null` element in `args` (or a `null` element inside a passed `Collection`) does not match any `instanceof` branch and falls through to `throw new IllegalArgumentException("Unsupported argument type: " + arg.getClass())`. Since `arg` is null, `arg.getClass()` throws a `NullPointerException` rather than the intended `IllegalArgumentException`, and the message is never produced. All current call sites pass non-null string literals and constants, so this is not triggered today, but the method is `public` and the failure mode is misleading. Consider handling null explicitly, e.g. `if (arg == null) throw new IllegalArgumentException("Unsupported argument type: null");` (and similarly for collection elements).

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing less information.

Comment with these commands to change:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

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.

2 participants