allwpilib icon indicating copy to clipboard operation
allwpilib copied to clipboard

[epilogue] Use reflection to access non-public superclass fields

Open Ornamus opened this issue 5 months ago • 0 comments
trafficstars

A sort of patch/continuation to #7993. @SamCarlberg and I overlooked that VarHandler does not allow you to violate some Java access permissions, such as accessing a private variable of a superclass or accessing a protected or package-private variable of a superclass if you're not in that package. As a result, some of the superclass-logging features of #7993 didn't work as intended:

  • A java.lang.IllegalAccessException would be raised on startup if a logged class' superclass had a @Logged private field
  • A java.lang.IllegalAccessException would be raised on startup if a logged class' superclass had a @Logged protected or package-private field and the child class wasn't in the same package

This PR fixes this by switching to using reflection to handle non-public superclass fields. This lets me revert the change from 7993 that made all non-public fields get accessed with VarHandler's - this can go back to being used for private-non-superclass fields only like it was before, which simplifies a lot of loggers and cleans up a lot of the unit test outputs :)

I pulled this set of changes into the robot project that helped me catch the issues and all seems to be working well - cross-package superclass fields are logging as intended.

Ornamus avatar May 31 '25 17:05 Ornamus