java – Good approaches when one needs to access private variables in production systems

At my company we needed access to some variable to call a method on it, this variable is hidden about 6 layers down of different libraries code and each time it is behind a private variables (that this is so is a definite oversight, the method needed provides vital information pertaining to an exception that has occurred, there is no way other than calling this method to see these details. There is an open issue about this, but it does not look like it will be getting resolved any time soon).

It is not practical to fork or modify the libraries in any way.

We are using Scala, so I wrote some reflection to just remove all the private modifiers and call the method. This was fine for the context we needed to do it, to debug some production issue, but it was such a success that now we would like to have access to this method forever more.

What is a good approach here?

My thinking is, add some unit test to make sure the variable is actually there to protect against dependency changes, put big signposting around the relevant code explaining what is going on, maybe provide the facility to turn this code off if users want to use our code against a version of one of the relevant dependencies which we’ve not tested.

I suppose my anxiety is just in the knowing introduction of a potential for a runtime error.