recipe: replace Arrays#asList() by List#of()
What's changed?
Adds a new recipe
What's your motivation?
We have lots of usages in our code base and they are reported as warnings by default
Anything in particular you'd like reviewers to focus on?
The second test is failing. I need help here with the implementation to fix it.
Failed to run recipe at Cursor{MethodInvocation->JRightPadded(element=Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), after=Space(comments=<0 comments>, whitespace=<empty>))->Block->MethodDeclaration->JRightPadded(element=MethodDeclaration{A{name=foo,return=void,parameters=[]}}, after=Space(comments=<0 comments>, whitespace=<empty>))->Block->ClassDeclaration->CompilationUnit->root}
Checklist
- [X] I've added unit tests to cover both positive and negative cases
- [X] I've read and applied the recipe conventions and best practices
- [X] I've used the IntelliJ IDEA auto-formatter on affected files
Thanks for kicking this off @kthoms ; we had so far held off on a recipe like this because List.of returns immutable collections, whereas Arrays.asList returns a new mutable array list. That means this is not yet a guaranteed safe replacement, and could lead to runtime issues. What are your thoughts on that?
You are right, Tim. Although most of the time an array would be converted to a iist for the sake to use the values for API that requires lists, it is not always true and might even break code.
Maybe we could think about restricting it to test code? There we are very likely safe and it is where most usages of the method will be.
Also, I forgot that a check/test is needed that List.of() should not be used when Arrays.asList() is used with a single argument that is an array.
Do you think I should extend the code in that sense?
This PR is stale because it has been open for 90 days with no activity. Remove stale label or comment or this will be closed in 7 days.