RFR(XS): 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
forax at univ-mlv.fr
forax at univ-mlv.fr
Tue Nov 17 22:30:04 UTC 2015
----- Mail original -----
> De: "Claes Redestad" <claes.redestad at oracle.com>
> À: "Remi Forax" <forax at univ-mlv.fr>
> Cc: "core-libs-dev at openjdk.java.net Libs" <core-libs-dev at openjdk.java.net>
> Envoyé: Mardi 17 Novembre 2015 23:08:55
> Objet: Re: RFR(XS): 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
>
> 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
>
I agree, far more readable :)
Rémi
More information about the core-libs-dev
mailing list