DaveAKing / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

TypeToken.equals() is incorrect for some array type combinations #1590

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The following sample code displays the issue,

TypeToken<?> b0 = new TypeToken<byte[]>() {};
TypeToken<?> b1 = TypeToken.of(byte[].class);

System.out.println(b0.equals(b1));
System.out.println(b0.isAssignableFrom(b1));
System.out.println(b1.isAssignableFrom(b0));

I would expect all of these comparisons to return true, but the first instead 
prints false. In the first case (b0) the byte[] type is represented internally 
as an instance of GenericArrayTypeImpl. The equals() on GenericArrayTypeImpl 
will only attempt a comparison to other GenericArrayType subclasses. I believe 
this equals method should also handle comparison to non-generic array types 
when appropriate.

Original issue reported on code.google.com by chris.th...@bronto.com on 25 Nov 2013 at 8:17

GoogleCodeExporter commented 9 years ago
There's something weird about what the JDK does with arrays across JDK 
versions. I forget the details. Ben will know.

Original comment by cpov...@google.com on 25 Nov 2013 at 8:20

GoogleCodeExporter commented 9 years ago
I found a related JDK issue,

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5041784

It looks like a fix was applied for 7 that makes an explicit check for this 
scenario. It looks like the same fix could be applied to Guava.

Original comment by chris.th...@bronto.com on 25 Nov 2013 at 9:01

GoogleCodeExporter commented 9 years ago
So I see now that this isn't a problem for Guava running on top of JDK 7, but 
it would be nice for Guava to work around this issue for older JDKs.

Original comment by chris.th...@bronto.com on 25 Nov 2013 at 9:58

GoogleCodeExporter commented 9 years ago
This was intentional.

Currently TypeToken stays consistent with whatever JDK it runs in.

In Java 7, getGenericReturnType() for the follow method includes int[].class:

List<int[]> foo();

In Java 6, the same getGenericReturnType() includes GenericArrayType.

The rationale is:
  * If it's broken, it's already broken. By staying consistent with JDK, we aren't making things more surprising.
  * Migrating from Java 6 to Java 7 does have slight behavior difference. But presumably other reflection code already has to deal with this behavior difference.

Original comment by be...@google.com on 26 Nov 2013 at 1:53

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:10

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08