<div dir="auto"><div>The problem is that would necessitate a new public class that is *only* used as a return value. Since it would then come up with auto complete for people searching for ArrayList, that's non trivial.</div><div dir="auto"><br></div><div dir="auto">Deprecated also isn't the right concept. Mechanically it puts a line through a method invocation, but this is a more specific sort of disclaimer. Like a @Unsupported.</div><div dir="auto"><br></div><div dir="auto">There is a whole separate universe of pitches for more standard annotations, but efforts like that would probably have to start in the community like jspecify is attempting for nullness annotations.</div><div dir="auto"><br><div class="gmail_quote" dir="auto"><div dir="ltr" class="gmail_attr">On Fri, Nov 18, 2022, 12:47 PM Andreas Røsdal <<a href="mailto:andreas.rosdal@gmail.com" target="_blank" rel="noreferrer">andreas.rosdal@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">How about this as a proposed solution to avoid UnsupportedOperationException:<br>1. Renamed the internal ArrayList to ArrayWrappingList<br><div>2. Override add() in ArrayWrappingList and make it deprecated to warn people from using it.</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Nov 18, 2022 at 6:23 PM Ethan McCue <<a href="mailto:ethan@mccue.dev" rel="noreferrer noreferrer" target="_blank">ethan@mccue.dev</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">I think there is potentially actionable feedback in that the exception thrown when users bump into this limitation kinda sucks<br><br>jshell> Arrays.asList(1, 2, 3).add(4);<br>|  Exception java.lang.UnsupportedOperationException<br>|        at AbstractList.add (AbstractList.java:155)<br>|        at AbstractList.add (AbstractList.java:113)<br>|        at (#8:1)<br><br>jshell> try { Arrays.asList(1, 2, 3).add(4);} catch (Exception e) { System.out.println(e.getMessage());}<br>null<br><br>I think there would be value in overriding "add" and other such operations to provide enough context for users to *understand* that<br>* they did an unsupported operation <br>* the list given by `Arrays.asList` was the cause<br><br>If I had to guess, that's the core of the frustration. The process to get from "not understanding what went wrong" -> "understanding what went wrong" -> "knowing how to fix it" is high.<br><br>The design problem is how much context can be conveyed in an exception message/stack trace.<br><br>There is a similar conversation to be had for the collections returned by List.of() and similar.<br><br>jshell> List.of().add(1)<br>|  Exception java.lang.UnsupportedOperationException<br>|        at ImmutableCollections.uoe (ImmutableCollections.java:142)<br>|        at ImmutableCollections$AbstractImmutableCollection.add (ImmutableCollections.java:147)<br>|        at (#6:1)<br><br>jshell> try { List.of(1, 2, 3).add(4);} catch (Exception e) { System.out.println(e.getMessage());}<br>null<br><br>There is a clue in the stack trace here though for List.of() with the "ImmutableCollections" calls. Maybe if we took two moves<br><br>1. Renamed the internal ArrayList to something like ArrayWrappingList<br>2. Overrode add<br><br>then the stack trace could be enough (or better than the status quo)<br><br>jshell> Arrays.asList(1, 2, 3).add(4);<br>|  Exception java.lang.UnsupportedOperationException<br>|        at ArrayWrappingList.add (ArrayWrappingList.java:155)<br>|        at ArrayWrappingList.add (ArrayWrappingList.java:113)<br>|        at (#8:1)<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Nov 18, 2022 at 12:14 PM Andreas Røsdal <<a href="mailto:andreas.rosdal@gmail.com" rel="noreferrer noreferrer" target="_blank">andreas.rosdal@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>`new ArrayList<>(Arrays.asList(array))`  is quite complex syntax to convert an array to an java.util.ArrayList,<br></div><div>so a suggestion could be to add a new method to Arrays to convert an array to a normal java.util.ArrayList which is modifiable.<br></div><div><br></div><div>Are there any low-hanging-fruit issues in core-libs-dev in <a href="http://bugs.openjdk.org" rel="noreferrer noreferrer" target="_blank">bugs.openjdk.org</a> that you are aware of that <br>you would like me to help you implement?<br></div><div><br></div><div>Thanks for considering my request.</div><div><br></div><div>Regards,<br></div><div>Andreas</div><div><br></div><div><br></div><div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Nov 18, 2022 at 5:51 PM Daniel Fuchs <<a href="mailto:daniel.fuchs@oracle.com" rel="noreferrer noreferrer" target="_blank">daniel.fuchs@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Andreas,<br>
<br>
First of all, congratulations for seeking advice before working on<br>
a PR. This is exactly how first contributions should start. Thank<br>
you for that!<br>
<br>
Given the area in which you intended to work however, `core-libs-dev`<br>
might have been a better list than `discuss` to start from.<br>
<br>
With regard to the meat of the issue however, and as noted by Ethan,<br>
Arrays.asList() behaves as intended, and changing that would be a<br>
major incompatible change, as many users of the API expect the list<br>
returned by Arrays.asList to be immutable (and depend on it).<br>
It is not possible nor desirable to change that.<br>
<br>
As for your observation, I believe that:<br>
<br>
   `new ArrayList<>(Arrays.asList(array))`<br>
<br>
will get you what you want.<br>
<br>
best regards,<br>
<br>
-- daniel<br>
<br>
On 18/11/2022 16:29, Andreas Røsdal wrote:<br>
> Yes, the exception comes when adding objects to the returned list. So I <br>
> would like a convenient way to use Arrays to convert an array to a <br>
> normal modifiable java.util.ArrayList, instead of this AbstractList.<br>
> <br>
> <br>
> On Fri, Nov 18, 2022 at 5:23 PM Ethan McCue <<a href="mailto:ethan@mccue.dev" rel="noreferrer noreferrer" target="_blank">ethan@mccue.dev</a> <br>
> <mailto:<a href="mailto:ethan@mccue.dev" rel="noreferrer noreferrer" target="_blank">ethan@mccue.dev</a>>> wrote:<br>
> <br>
>     What situation were you encountering the exception? Was it when<br>
>     trying to add to the returned list?<br>
> <br>
>     If so, that's expected. Arrays.asList only wraps an underlying<br>
>     array, it can't grow it. By that measure List.of() is even more<br>
>     unintuitive because you can't set anything.<br>
> <br>
>     On Fri, Nov 18, 2022, 11:06 AM Andreas Røsdal<br>
>     <<a href="mailto:andreas.rosdal@gmail.com" rel="noreferrer noreferrer" target="_blank">andreas.rosdal@gmail.com</a> <mailto:<a href="mailto:andreas.rosdal@gmail.com" rel="noreferrer noreferrer" target="_blank">andreas.rosdal@gmail.com</a>>> wrote:<br>
> <br>
>         Hello!<br>
> <br>
>         I am an aspiring JDK contributor, having used Java in my work as<br>
>         a developer.<br>
> <br>
>         I was recently surprised by an Exception thrown when using the<br>
>         java.util.Arrays.asList() method<br>
>         so I would like to propose some improvements to this API. In<br>
>         particular:<br>
>         - When using java.util.Arrays.asList() then AbstractList is<br>
>         throwing UnsupportedOperationException which is not<br>
>         user-friendly or intuitive.<br>
>         - java.util.Arrays.asList() returning a private class called<br>
>         ArrayList, which is not the usual java.util.ArrayList, so it's<br>
>         not user-friendly or intuitive.<br>
> <br>
>         Since this would be my first contribution to the JDK, and the<br>
>         goal is to complete the contribution with accepted pull request<br>
>         and get the Arrays API improved, what would the first step to<br>
>         making this first improvement to the JDK be?<br>
> <br>
>         I would also like to share a link to an open source project I've<br>
>         been working on:<br>
>         <a href="https://github.com/fciv-net/fciv-net" rel="noreferrer noreferrer noreferrer" target="_blank">https://github.com/fciv-net/fciv-net</a><br>
>         <<a href="https://github.com/fciv-net/fciv-net" rel="noreferrer noreferrer noreferrer" target="_blank">https://github.com/fciv-net/fciv-net</a>><br>
> <br>
>         Thank you!<br>
> <br>
>         Regards,<br>
>         Andreas R.<br>
> <br>
<br>
</blockquote></div></div>
</blockquote></div>
</blockquote></div>
</blockquote></div></div></div>