commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Dipanjan Laha (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (COLLECTIONS-531) Generic Wildcards specified in CollectionUtils#isEqualCollection(Collection, Collection, Equator) may throw ClassCastException in certain cases
Date Tue, 27 May 2014 10:15:02 GMT

     [ https://issues.apache.org/jira/browse/COLLECTIONS-531?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Dipanjan Laha updated COLLECTIONS-531:
--------------------------------------

    Summary: Generic Wildcards specified in CollectionUtils#isEqualCollection(Collection,
Collection, Equator) may throw ClassCastException in certain cases  (was: Generic Wildcards
specified in CollectionUtils#isEqualCollection(Collection, Collection, Equator) may cause
ClassCastException in certain cases)

> Generic Wildcards specified in CollectionUtils#isEqualCollection(Collection, Collection,
Equator) may throw ClassCastException in certain cases
> -----------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: COLLECTIONS-531
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-531
>             Project: Commons Collections
>          Issue Type: Bug
>          Components: Collection
>    Affects Versions: 4.0
>            Reporter: Dipanjan Laha
>            Priority: Minor
>         Attachments: IsEqualCollectionTest.java
>
>
> CollectionUtils#isEqualCollection(Collection, Collection, Equator) is defined as
> {code}
> public static boolean isEqualCollection(final Collection<?> a, final Collection<?>
b, final Equator<?> equator) {
> ...
> }
> {code}
> This makes it possible to invoke it with a code like
> {code}
> public static class IntegerEquator implements Equator<Integer> {
>         public boolean equate(Integer o1, Integer o2) {
>             return o1.intValue() == o2.intValue();
>         }
>         public int hash(Integer o) {
>             return o.intValue();
>         }
>     }
>     @Test
>     public void test() {
>         List<Long> longList = Arrays.asList(1L, 2L);
>         List<Integer> intList = Arrays.asList(1, 2);
>         assertTrue(CollectionUtils.isEqualCollection(longList, intList, new IntegerEquator()));
>     }
> {code}
> which compiles perfectly but throws a ClassCastException as Long cannot be cast to an
Integer. However, the generics should be defined such that this is stopped at compile time
itself.
> If we modify the method to something like
> {code}
> public static <E> boolean isEqualCollection(final Collection<? extends E>
a, final Collection<? extends E> b, final Equator<? super E> equator) {
> ...
> }
> {code}
> the above example would give a compile time error. imho we should modify this method
with bounded wildcards. I don't think this change would break any existing binaries if the
method is being used correctly, otherwise it is probably throwing ClassCastExceptions anyway.
> Test case attached



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Mime
View raw message