Skip to content
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

Change mutable public collections to immutable versions: ref issue #2413 #3308

Conversation

MoritzCSchmidt
Copy link

@MoritzCSchmidt MoritzCSchmidt commented Jun 11, 2023

What's changed?

Added new recipe to wrap modifiable lists to be unmodifiable.

What's your motivation?

Closes openrewrite/rewrite-static-analysis#125

Any additional context

Thanks for your work! I hope we can support with our small improvements.

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've added the license header to any new files through ./gradlew licenseFormat
  • I've used the IntelliJ auto-formatter on affected files
  • I've updated the documentation (if applicable)

@MoritzCSchmidt MoritzCSchmidt marked this pull request as ready for review June 11, 2023 10:42
@timtebeek timtebeek added the recipe Requested Recipe label Jun 12, 2023
@timtebeek
Copy link
Contributor

Thank you for starting this implementation! It's looking quite good already.
I've added two small polishing commits, and will add some review comments.

Comment on lines +30 to +31
* This recipe modifies the invocation of a method so that it returns an unmodifiable list, if the
* method returns a modifiable one.
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps this is more of an issue with the original feature request rather than the recipe implementation, but I have some doubts if this recipe could do harm in certain situations. More details here: https://github.com/openrewrite/rewrite/issues/2413#issuecomment-1586767731

import java.util.Arrays;

class A {
public static final List<String> entries = Arrays.asList("A", "B");
Copy link
Collaborator

Choose a reason for hiding this comment

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

IIRC, Arrays.asList already returns a list that is unmodifiable.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, I'm wrong, you can't append to it, but you can replace existing values.

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed; more details discussed in this thread; I like your suggestion of using data flow to make this recipe safe to execute. I've not used data flow myself before, but would be good to explore here.

Copy link
Collaborator

@JLLeitschuh JLLeitschuh Jun 18, 2023

Choose a reason for hiding this comment

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

To use data flow, this recipe would need to be moved to the cleanup package in rewrite-static-analysis repository

Copy link
Contributor

Choose a reason for hiding this comment

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

That's indeed perhaps a better place for this recipe; sorry about that @marynasavchenko . Would you mind targeting https://github.com/openrewrite/rewrite-static-analysis/tree/main/src/main/java/org/openrewrite/staticanalysis for these files?
As seen above that's necessary to use data flow, which we need to be sure we can safely apply this recipe.

@timtebeek
Copy link
Contributor

Closed pending a move to rewrite-static-analysis, which we need for the data flow analysis to determine if it's safe to do this replacement: we can't replace when list elements or order is modified. Great start, looking forward to the full implementation.

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

Successfully merging this pull request may close these issues.

Change mutable public collections to immutable versions
4 participants