Skip to content

ReplaceCollectToListWithToList #558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/main/resources/META-INF/rewrite/examples.yml
    • lines 660-659
    • lines 705-721
    • lines 1254-1273
    • lines 1306-1305
    • lines 2337-2336
    • lines 2352-2364
    • lines 2801-2800
    • lines 2820-2831

Copy link

@gnodet gnodet left a comment

Choose a reason for hiding this comment

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

The problem is that this code change is not semantically the same. The stream.collect(Collectors.toList()) returns a mutable list, while stream.toList() returns an immutable collection.

So this cannot be applied blindly using a recipe imho.

@Pankraz76
Copy link
Contributor Author

So this cannot be applied blindly using a recipe imho.

on greenfield yes, to ensure https://adabeat.com/fp/immutability-in-functional-programming/

for legacy, or valid exceptions, like maven we need suppression.

Copy link
Contributor

@iddeepak iddeepak left a comment

Choose a reason for hiding this comment

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

LGTM!

@Pankraz76
Copy link
Contributor Author

Pankraz76 commented May 16, 2025

The problem is that this code change is not semantically the same. The stream.collect(Collectors.toList()) returns a mutable list, while stream.toList() returns an immutable collection.

So this cannot be applied blindly using a recipe imho.

yes we can: make change and wrap in new ArrayList(). Then it will return new operational list. Other side effects would be sus relying on pass-by-value, manipulation SSOT violating getter/setter convention.

Then we have same API, but no new violations following: https://kyleshevlin.com/just-enough-fp-immutability/

return method.getArguments().get(0) instanceof J.MethodInvocation &&
((J.MethodInvocation) method.getArguments().get(0)).getSimpleName().equals("toList") &&
((J.MethodInvocation) method.getArguments().get(0)).getSelect() != null &&
((J.MethodInvocation) method.getArguments().get(0)).getSelect().toString().equals("Collectors");
Copy link
Member

Choose a reason for hiding this comment

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

This would fail to pick up static imports of toList. Wherever possible prefer to use MethodMatchers as opposed to looking at simple names, as that avoids these pitfalls.

@Pankraz76 Pankraz76 closed this May 17, 2025
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite May 17, 2025
@timtebeek
Copy link
Member

I've spoken at length with the author linked above at Code Remix Summit last week, helped him understand and analyze the changes, and got him set up to continue his work. Despite this he came out with a deeply flawed analysis and false statements, which unfortunately are being taken at face value.

Figured I'd provide some more context about the changes made from our perspective:

You're all welcome to use and contribute to OpenRewrite for as much as you'd like; we see no reason to be hesitant at all, but understand opinions may differ.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants