Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CameraServerWidget: Explicitly unbox Number objects #780

Merged
merged 3 commits into from
Mar 28, 2023

Conversation

Starlight220
Copy link
Member

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

@PeterJohnson
Copy link
Member

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

@Starlight220
Copy link
Member Author

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

@Starlight220
Copy link
Member Author

Starlight220 commented Mar 28, 2023

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.

@PeterJohnson PeterJohnson merged commit 42dcd98 into wpilibsuite:main Mar 28, 2023
@Starlight220 Starlight220 deleted the unbox branch March 28, 2023 20:02
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CCE in loading CameraServer settings from saved layout JSON
2 participants