Issue with Comparator.comparing

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Mon Dec 17 15:26:43 UTC 2018


yep - you ended up using raw types; something I thought about, but note 
that this will generate an unchecked warning:

warning: [unchecked] unchecked call to compareTo(T) as a member of the 
raw type Comparable
                 return f1.t().compareTo(f2.t());
                                        ^
   where T is a type-variable:
     T extends Object declared in interface Comparable

So, strictly speaking the class cast exception comes at no surprise here.

But there's no warning in the original code!!! (not suggesting we should 
add warnings here or there, just noting that we have a case of heap 
pollution that goes under the radar).

Maurizio


On 17/12/2018 14:53, B. Blaser wrote:
> On Mon, 17 Dec 2018 at 13:49, Maurizio Cimadamore
> <maurizio.cimadamore at oracle.com> wrote:
>> [...]
>> Challenge: can you find an example which ends up with same behavior
>> (ClassCastException) that does _not_ involve method references and/or
>> lambdas?
>>
>> Maurizio
> I like challenges, but I agree this one is hard as it involves a raw
> 'Foo3' to simulate the capture side-effect as here under.
>
> Thoughts?
> Bernard
>
> import java.util.*;
> import java.util.stream.*;
> import java.util.function.*;
>
> class Foo3<T extends Comparable<T>> {
>
>      static Foo3<String> S = new Foo3<>("");
>      static Foo3<Integer> I = new Foo3<>(42);
>
>      private T t;
>
>      Foo3(T t) {
>          this.t = t;
>      }
>
>      T t() {
>          return t;
>      }
>
>      public static void main(String[] args) {
>          List<Foo3<?>> l = new ArrayList<>();
>          l.add(S);
>          l.add(I);
>
>          Comparator<? super Foo3<?>> comp = new Comparator<Foo3>() {
>              public int compare(Foo3 f1, Foo3 f2) {
>                  return f1.t().compareTo(f2.t());
>              }
>          };
>          Collections.sort(l, comp);
>      }
> }


More information about the compiler-dev mailing list