Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toArray(T[]) - ClassCastException #17

Closed
BrenoTrancoso opened this issue May 4, 2017 · 3 comments
Closed

toArray(T[]) - ClassCastException #17

BrenoTrancoso opened this issue May 4, 2017 · 3 comments
Labels

Comments

@BrenoTrancoso
Copy link

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
Copy link
Owner

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
Copy link
Owner

GlenKPeterson commented May 4, 2017

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 participants