[
https://issues.apache.org/jira/browse/COLLECTIONS-747?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17038168#comment-17038168
]
Walter Laan commented on COLLECTIONS-747:
-----------------------------------------
Thanks, works for me.
The only issue is the all-null case, which maybe could have a warning in the
javadoc of getKeys:
@throws ClassCastException if all keys are {@code null}, and an {@link Object}
array was constructed.
MultiKeyMap does not use the method, so should be fine.
Nitpicks:
{code}
// can add @SafeVarargs here
private static <T> Class<? extends T> getComponentType(final T... values) {
assertNotNull(keys);
// bit more comprehensive test would be
assertEquals(new MultiKeyTest[] { key1, key2 }, keys);
{code}
> MultiKey.getKeys class cast exception
> -------------------------------------
>
> Key: COLLECTIONS-747
> URL: https://issues.apache.org/jira/browse/COLLECTIONS-747
> Project: Commons Collections
> Issue Type: Bug
> Components: KeyValue
> Affects Versions: 4.4
> Reporter: Walter Laan
> Assignee: Gary D. Gregory
> Priority: Major
> Fix For: 4.5
>
>
> When using an non-array constructor of MultiKey, an Object[] is created,
> which cannot be cast to a K[]
> {code}
> import org.apache.commons.collections4.keyvalue.MultiKey;
> public class MultiKeyClassCastException {
> public static void main(String[] args) {
> MultiKeyClassCastException key1 = new MultiKeyClassCastException();
> MultiKeyClassCastException key2 = new MultiKeyClassCastException();
> MultiKeyClassCastException[] keys = new MultiKey<>(key1,
> key2).getKeys();
> }
> }
> // running gives (same error if in module):
> Exception in thread "main" java.lang.ClassCastException: class
> [Ljava.lang.Object; cannot be cast to class [LMultiKeyClassCastException;
> ([Ljava.lang.Object; is in module java.base of loader 'bootstrap';
> [LMultiKeyClassCastException; is in unnamed module of loader 'app')
> at MultiKeyClassCastException.main(MultiKeyClassCastException.java:8)
> {code}
> AFAIK, the only way to fix this (with source compatibility), is to only have
> a varargs constructors. but I think this breaks binary compatibility though
> and cannot be used with the existing constructors.
> {code}
> @SafeVarargs
> public MultiKey(K... keys) {
> this.keys = keys;
> }
> {code}
> Workaround is to use array constructor with correct typed array or
> {code}Object[] keys = multiKey.getKeys();{code} explicitly.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)