RFR(XS): 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character

Claes Redestad claes.redestad at oracle.com
Tue Nov 17 22:08:55 UTC 2015


Hi Remi,

On 2015-11-17 22:13, Remi Forax wrote:
> Hi Claes,
> I fail to see how this code will not throw a CCE at runtime
>    if (con instanceof Integer || con instanceof Byte || con instanceof Short || con instanceof Character) {
>       emitIconstInsn((int) con);
>       ...
>
> (int)con is translated by the compiler to ((Integer)con).intValue()
>
> you have to write something like that
>    (con instanceof Character)? (char)con: ((Number)con).intValue()

Well, this is embarrassing. I was fooled by my own sanity testing since 
javac makes the unboxing+cast work when type is known:

Character c = 'a';
System.out.println((int)c);

but not when going through an Object reference:

Object o = 'a';
System.out.println((int)o);

This works better:

         if (con instanceof Integer) {
             emitIconstInsn((int) con);
             return;
         }
         if (con instanceof Byte) {
             emitIconstInsn((int)(byte)con);
             return;
         }
         if (con instanceof Short) {
             emitIconstInsn((int)(short)con);
             return;
         }
         if (con instanceof Character) {
             emitIconstInsn((int)(char)con);
             return;
         }

Updated in-place.

/Claes



More information about the core-libs-dev mailing list