Bug 473748

Summary: ClassCastException when using types parameterized with primitive arrays and varargs
Product: [Tools] AspectJ Reporter: Krzysztof Szafranski <k.p.szafranski>
Component: CompilerAssignee: aspectj inbox <aspectj-inbox>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3 CC: aclement
Version: 1.8.5   
Target Milestone: ---   
Hardware: Macintosh   
OS: Mac OS X   
Whiteboard:
Attachments:
Description Flags
Examples reproducing the issue none

Description Krzysztof Szafranski CLA 2015-07-28 08:39:03 EDT
Created attachment 255478 [details]
Examples reproducing the issue

This issue appears when we are passing a result of a type-parameterized method directly to a varargs method. The behavior is different when compiling with javac and with ajc.

When I compile and run the attached file with javac (1.8.0_40) everything works fine. But when I compile it with ajc:
java -jar /path/to/aspectjtools-1.8.5.jar GenericVarargsBug.java -1.8

I'm getting ClassCastException:
Exception in thread "main" java.lang.ClassCastException: [B cannot be cast to [Ljava.lang.Object;
	at GenericVarargsBug.main(GenericVarargsBug.java:9)

It seems that while javac treats the byte array as a single Object argument to the varargs method, ajc tries to cast it to Object array.

The issue does not appear when the result of sth.get(0) is saved to an intermediary variable, e.g.:
byte[] first = sth.get(0);
isItWorking(first);

I'm also attaching two more examples of how to reproduce this issue.
Comment 1 Andrew Clement CLA 2015-08-05 15:53:30 EDT
I just tried your snippet in pure Eclipse and it seems to be a bug there (The AspectJ compiler piece is based on the eclipse compiler). I've raised bug 474355 to track it in  JDT. Possibly it is already fixed in JDT master but I haven't checked that, I only checked the mars (4.5.0) release.

Once there is a fix and we know where it is I will pick it up when I fold the new compiler version from eclipse into AspectJ. Thanks for the small code snippet, they are so useful.