wpilibsuite / shuffleboard

A modern dashboard for FRC
Other
79 stars 84 forks source link

CameraServerWidget: Explicitly unbox Number objects #780

Closed Starlight220 closed 1 year ago

Starlight220 commented 1 year ago

Fixes #773. This fixes a CCE caused by trying to implicitly cast a Gson internal subclass of Number to an int.

PeterJohnson commented 1 year ago

I don’t understand how this changes behavior. It’s getting stored into an int, so intValue() is implicitly being called.

Starlight220 commented 1 year ago

The logs show otherwise. I think it gets cast into an Integer, which is then unboxed. This calls intValue directly.

Starlight220 commented 1 year ago

Bytecode before (notice the CHECKCAST java/lang/Integer):

   L6
    LINENUMBER 167 L6
    ALOAD 0
    GETFIELD edu/wpi/first/shuffleboard/plugin/cameraserver/widget/CameraServerWidget.height : Ledu/wpi/first/shuffleboard/api/components/IntegerField;
    INVOKEVIRTUAL edu/wpi/first/shuffleboard/api/components/IntegerField.getNumber ()Ljava/lang/Number;
    CHECKCAST java/lang/Integer
    INVOKEVIRTUAL java/lang/Integer.intValue ()I
    ISTORE 5

Bytecode after:

   L6
    LINENUMBER 170 L6
    ALOAD 0
    GETFIELD edu/wpi/first/shuffleboard/plugin/cameraserver/widget/CameraServerWidget.height : Ledu/wpi/first/shuffleboard/api/components/IntegerField;
    INVOKEVIRTUAL edu/wpi/first/shuffleboard/api/components/IntegerField.getNumber ()Ljava/lang/Number;
    INVOKEVIRTUAL java/lang/Number.intValue ()I
    ISTORE 5

Thanks for challenging this so I actually look at bytecode -- apparently Java really likes implicitly casting to Integer, and an explicit cast to Number is needed.