Skip to content

Conversation

@timtebeek
Copy link
Member

Fixes #230

What's changed

In this proposal we look at the method parameter type, and if that is also an array we do not produce a change.

Anything in particular you'd like reviewers to focus on?

Not sure I actually like this change yet; It seems before we were too eager to make changes, even where we changed to logic. The recipe was intended not to show the result of arr.toString(), that that wasn't what happened on some of the previous cases. Take for example

jshell> String[] args = new String[] {"a","b","c"}
args ==> String[3] { "a", "b", "c" }

jshell> String.format("s=%s", args)
|  Warning:
|  non-varargs call of varargs method with inexact argument type for last parameter;
|    cast to java.lang.Object for a varargs call
|    cast to java.lang.Object[] for a non-varargs call and to suppress this warning
|  String.format("s=%s", args)
|                        ^--^
$6 ==> "s=a"

Note

How we print s=a, not s=[Ljava.lang.String;@73a8dfcc as would be the case for args.toString()

That means that the recipe might inadvertently in the old situation changes the above to start producing
s=[a, b, c] for String.format("s=%s", Arrays.toString(args))

@timtebeek timtebeek added the bug Something isn't working label Jan 19, 2024
@timtebeek timtebeek self-assigned this Jan 19, 2024
@timtebeek
Copy link
Member Author

@philippe-granet
Copy link

@philippe-granet do these changes align with what you expected after logging

@timtebeek yes it's OK for me, thanks!

@timtebeek timtebeek merged commit 093655f into main Jan 31, 2024
@timtebeek timtebeek deleted the no-arrays.tostring-on-varargs branch January 31, 2024 20:12
if (methodType == null) {
return mi;
}
List<JavaType> parameterTypes = methodType.getParameterTypes();
Copy link
Contributor

Choose a reason for hiding this comment

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

An alternative would be to rely on Flag.Varargs which when set on the JavaType.Method means that it is a varargs method.

Copy link
Member Author

Choose a reason for hiding this comment

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

Didn't know that flag, thanks! But then that only applies to the entire method right? Yet we'd still want to do replacements on String.format and friends when we can. Say String.format gets called with 6 arguments, one of them is an array. The current implementation would then still wrap that one array in Arrays.toString. Or how would you apply that flag here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure. I would probably just use the flag to check what the recipe should do with the arguments corresponding to the (last) varargs parameter, if any. Probably no huge improvement...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

RemoveToStringCallsFromArrayInstances should not use Arrays.toString on String/PrintStream/PrintWriter.format methods

4 participants