1

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.

4
  • 1
    Why is this "vital information pertaining to an Exception" not passed as part of the Exception itself?
    – Phill W.
    Jun 25, 2021 at 15:46
  • @PhillW I couldn't say really. Essentially the situation is that something dies at some point, the error is not logged anywhere or sent anywhere, but is put on some internal list which you can get via some method. When you try to contact the now dead thing, it tells you it is dead but doesn't tell you why. If anyone is to be at fault I would say it was elasticsearch github.com/elastic/elasticsearch/issues/… Jun 25, 2021 at 16:21
  • You should prioritize getting the original module fixed, even if it requires forking the module and is cumbersome. The compiler may make assumptions about finals that may bite you later. Jun 27, 2021 at 18:04
  • @ThorbjørnRavnAndersen noted, I hate and am wary of any sort of reflection so don't think I did this lightly. The initial discussion with the powers that be deemed it not practical, as it would involve forking 3 modules to do nicely, and would be painful as we are flexible on versions of 2 of these modules, so we would need to maintain several fork versions to keep up with this, but I have scheduled a review of this decision. Jun 28, 2021 at 22:31

1 Answer 1

4

You don't explain why you are unable to fork or modify code so I will accept that as a given.

While using reflection is a little ugly, I think your best bet is to build a wrapper API for retrieving this information using your proven approach. The idea would be to provide an easy way to get read-only access to this and hide the nasty details about how it was done. I don't think you intend to modify anything and I would strongly recommend you not attempt to update any state with this approach. You might also want to make sure calling this doesn't produce any side effects.

8
  • 2
    And this approach obviously insulates the bulk of your app (whatever it is that uses this information) when the libraries eventually (if ever) get updated - this wrapper could even determine dynamically if it was working against updated libraries or not ...
    – davidbak
    Jun 25, 2021 at 15:21
  • @davidbak Yes good points.
    – JimmyJames
    Jun 25, 2021 at 15:23
  • 1
    You should prioritize getting the original module fixed, even if it requires forking the module. The compiler may make assumptions about finals that may bite you later. Jun 25, 2021 at 16:13
  • @ThorbjørnRavnAndersen I agree but the OP makes the assertion: "It is not practical to fork or modify the libraries in any way." as noted in my answer.
    – JimmyJames
    Jun 25, 2021 at 17:17
  • @jimmy this was for OP. “Not practical” can be many things and I think It is necessary to emphasize that this approach (which is what I would do too) may have subtle pitfalls. Jun 25, 2021 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.