Does anyone have insights on why this code works on java 8 but not on java 9
String[] strings = (String[]) Arrays.asList(\"foo\", \"bar\").toArray();
for (St
The implementation of Arrays.ArrayList.toArray seems to have been changed. The old implementation was to just clone the backing array:
private final E[] a;
ArrayList(E[] array) {
a = Objects.requireNonNull(array);
}
@Override
public Object[] toArray() {
return a.clone();
}
The new implementation forces the returned array to be an Object[]:
@Override
public Object[] toArray() {
return Arrays.copyOf(a, a.length, Object[].class);
}
To be clear, though, in Java 8 the cast only worked because the backing array was originally a String[], created by the asList varargs. Implicitly all that was happening was new String[] {"foo", "bar"}.clone(), but the array was passed through the asList List implementation.
As for fixing the broken dependency, I don't think there's a way besides either using a Java 8 run-time environment or rewriting what was introduced in that commit. Filing a bug report seems like the right thing to do.