ClassCastException on collection generics for jdk8 working code

Emanuel emanuele.tagliaferri at gmail.com
Sat Jan 17 16:58:37 UTC 2015


I am at the Adopt OpenJdk hack day organized by the LJC, I am running
tests cases of OrientDB (open source java based NoSql) on JDK9 (Build
b45 ), and i got some issue with the use of generics.

basically this code:
        List<Date> list = new ArrayList<>();
        list.add(new Date());

        List<Foo> cList = (List<Foo>) (List<?>) list;
        Date date = (Date) cList.get(0);                        // it
fail here

on jdk8 it work fine, on jdk9 the code fail in the marked line.

the jdk8 bytecode for the failing line is:
      invokeinterface #7,  2            // InterfaceMethod
java/util/List.get:(I)Ljava/lang/Object;
      checkcast     #4                  // class java/util/Date

instead of  on jdk9 is:
      invokeinterface #7,  2            // InterfaceMethod
java/util/List.get:(I)Ljava/lang/Object;
      checkcast     #8                  // class ListFail$Foo
      checkcast     #4                  // class java/util/Date


and is clear that there's a additional cast that is causing the exception.

I understand that this code is not the exactly clean and correct
conceptually, but it may be a lot of this staff out there that can be
break by jdk9.

In attachment a couple of simple example for reproduce this issue.


Regards

Emanuel




More information about the jdk9-dev mailing list