<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
Hi,<br>
<br>
I agree the exception message could be added to facilitate the
understanding and the resolution of UnsupportedOperationExceptions
from List implementations.<br>
<br>
New issue: <a class="moz-txt-link-freetext" href="https://bugs.openjdk.org/browse/JDK-8297283">https://bugs.openjdk.org/browse/JDK-8297283</a><br>
<br>
As to what should be included in the message, that will need more
discussion.<br>
<br>
To the original point, in the case where the list should have
particular features or abilities, such as mutability or
extensibility, the developer should construct the list themselves or
only use factory methods such as Arrays.asList() that are specified
to meet their requirements. With all kinds of lists and other
collections, there are many different implementation features to
choose from. Immutable lists are very common and well supported by
the libraries.<br>
<br>
$.02, Roger<br>
<br>
<br>
<div class="moz-cite-prefix">On 11/18/22 12:23 PM, Ethan McCue
wrote:<br>
</div>
<blockquote type="cite" cite="mid:CA+NR86ibGtGFxd7uHh23AWvKkq=0BhCrMriCNHQqbYVzMm3aeA@mail.gmail.com">
<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" moz-do-not-send="true" class="moz-txt-link-freetext">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" target="_blank" moz-do-not-send="true">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" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">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" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">ethan@mccue.dev</a>
<br>
> <mailto:<a href="mailto:ethan@mccue.dev" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">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" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">andreas.rosdal@gmail.com</a>
<mailto:<a href="mailto:andreas.rosdal@gmail.com" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">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" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">https://github.com/fciv-net/fciv-net</a><br>
> <<a href="https://github.com/fciv-net/fciv-net" rel="noreferrer" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">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>
<br>
</body>
</html>