GlenKPeterson / Paguro

Generic, Null-safe, Immutable Collections and Functional Transformations for the JVM
Other
312 stars 24 forks source link

toArray(T[]) - ClassCastException #17

Closed BrenoTrancoso closed 7 years ago

BrenoTrancoso commented 7 years ago

String[] array = vec("A", "B", "C").toArray(new String[0]); -> java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

String[] array = vec("A", "B", "C").toArray(new String[3]); -> This works.

UnmodCollection.toArray(T[] as):

if (as.length < size()) {
    as = (T[]) new Object[size()];
}

to

if (as.length < size()) {
    as = (T[]) java.lang.reflect.Array.newInstance(as.getClass().getComponentType(), size());
}
GlenKPeterson commented 7 years ago

Thank you for finding and reporting this! I have written a unit test, seen it fail, and fixed the issue. I'm currently looking for other possible instances of this same bug and hope to have a build in a few hours.

I use arrays in Paguro so we don't have to use them anywhere else. What are you doing that you need an array? Why would you pass a shorter array to this method? You don't need to answer, I just like to understand when people use Paguro in ways I never thought to.

GlenKPeterson commented 7 years ago

This issue is fixed in 2.1.1. Please upgrade at your earliest convenience. I could not find any other place where the issue occurs. Thanks again!

BrenoTrancoso commented 7 years ago

mensagem.setRecipients(RecipientType.TO, destinatario.map(e -> Email.getInternetAddress(e.getKey(), e.getValue())).toImList().stream().toArray(InternetAddress[]::new));

javax.mail.internet.MimeMessage.setRecipients needs a array. A empty array is just to get a typed array.

I've used stream as alternative.