SONARJAVA-6446 Refactor merging list of arguments to MethodMatchers#5657
Conversation
849d446 to
2ccd493
Compare
| 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()); | ||
| } | ||
| } |
There was a problem hiding this comment.
💡 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 👍 / 👎
3e6eb59 to
16dd947
Compare
dorian-burihabwa-sonarsource
left a comment
There was a problem hiding this comment.
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
…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>
|
Code Review 👍 Approved with suggestions 0 resolved / 1 findingsSimplifies 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 🤖 Prompt for agentsOptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change:
Was this helpful? React with 👍 / 👎 | Gitar |




Summary by Gitar
StringUtils.flattento concatenateString,String[], andCollection<String>arguments into a singleString[]array.MethodMatchersdefinitions inUnitTestUtilsby replacing complexStream.concatlogic with the newflattenhelper.StringUtilsTest.testFlattento verify argument merging and exception handling for unsupported types.This will update automatically on new commits.