-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Modernize codebase with Java improvements - Use modern Java collections API (toList()
instead of Collectors.toList()
)
#2287
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
Conversation
…StreamToListWithCollect
a4ba4de
to
e2f212a
Compare
…StreamToListWithCollect
e2f212a
to
4728546
Compare
Modernize codebase with Java improvements: ReplaceStreamToListWithCollect
…rn Java collections API (toList() instead of collect(Collectors.toList()))
4728546
to
1ef4091
Compare
…rn Java collections API (toList() instead of collect(Collectors.toList()))
1ef4091
to
8e20db7
Compare
Modernize codebase with Java improvements: ReplaceStreamToListWithCollect
Modernize codebase with Java improvements: Use modern Java collections API (toList() instead of collect(Collectors.toList()))
…rn Java collections API (toList() instead of collect(Collectors.toList()))
8e20db7
to
bd04034
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please only apply to [root]/api
, [root]/impl
, but not [api]/compat
.
…rn Java collections API (toList() instead of collect(Collectors.toList()))
bd04034
to
15cdf3f
Compare
…rn Java collections API (toList() instead of collect(Collectors.toList()))
15cdf3f
to
b3c9f06
Compare
Modernize codebase with Java improvements: Use modern Java collections API (toList() instead of collect(Collectors.toList()))
Modernize codebase with Java improvements - Use modern Java collections API (toList() instead of collect(Collectors.toList()))
…nalize DefaultModelProcessor
…nalize DefaultModelProcessor
…nal DefaultModelProcessor#read
…nal DefaultModelProcessor#read
…nal DefaultModelProcessor#read
…nal DefaultModelProcessor#read
…nal DefaultModelProcessor#read
…nal DefaultModelProcessor#read
impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultChecksumAlgorithmService.java
Outdated
Show resolved
Hide resolved
…ern Java collections API (toList() instead of collect(Collectors.toList()))
b3c9f06
to
9958737
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a drop in replacement. This changes a lot of modifiable lists to immutable lists. In some cases here this is clearly fine — e.g. when all that''s done next is findFirst — but others aren't so obvious. This is really, really hard to debug when it goes wrong. I would limit this PR to cases where the lsist does not escape the method or perhaps the class.
Immutable Objects and Thread SafetyIf someone wants to deviate from the happy path, they should work with a copy of final and secure state - and by happy with that. Core Principles:
Important Considerations:
Reference:Immutable Objects - Thread Safety Implementation Strategy:For production systems:
|
in Checkstyle |
You're not working in a green field. You can't change the behavior of APIs and assume existing clients will adapt. |
Yes. Which clients are affected?When will they get in touch? Do they live inside our JVM — at runtime or even compile-time — like within this code repo's scope? If not — for example, clients using the Maven wrapper — they simply execute the The only case I can imagine is another method using the current API and directly manipulating the result. Whether we return a mutable list or they create one themselves doesn’t really matter. The scenario where code relies on mutating a shared reference doesn’t apply here, since The core change here is introducing immutability — which is a best practice and supports defensive programming. So overall, the change is meaningful in terms of thread safety and data integrity — it's justified. See also: Create mutable list from array - Stack Overflow Ideally, failing tests that throw |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, no, I have been burned too badly by this change before. It is not OK to change an existing public method from returning a mutable list to an immutable list. It does break things. I have wasted too many hours of my life tracking down problems caused by an unexpected change from mutable to immutable to be OK with this.
of course agree on this. Then need to make this step by step. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extract internal.
impl/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java
Show resolved
Hide resolved
.profiles(request.getProfiles().stream() | ||
.map(Profile::getDelegate) | ||
.map(SettingsUtilsV4::convertToSettingsProfile) | ||
.collect(Collectors.toList())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is api
@@ -312,7 +312,7 @@ public XmlNode doMerge(XmlNode dominant, XmlNode recessive, Boolean childMergeOv | |||
Iterator<XmlNode> it = | |||
commonChildren.computeIfAbsent(name, n1 -> Stream.of(dominant.children().stream() | |||
.filter(n2 -> n2.name().equals(n1)) | |||
.collect(Collectors.toList())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, internal.
Modernize codebase with Java improvements - Use modern Java collections API (toList() instead of collect(Collectors.toList()))
toList()
instead of Collectors.toList()
)
Only doing the migration step by step to ensure quick fix, its possible. As mentioned any change could unpleasant have side effects. |
Modernize codebase with Java improvements - Use modern Java collections API (
toList()
instead ofCollectors.toList()
)Objects.requireNonNull
#2290AvoidOutdatedUsage: Use modern Java collections API (toList() instead of collect(Collectors.toList()))
checkstyle/checkstyle#17000