How to read the value of a private field from a different class in Java?

前端 未结 14 1895
梦毁少年i
梦毁少年i 2020-11-21 11:28

I have a poorly designed class in a 3rd-party JAR and I need to access one of its private fields. For example, why should I need to choose priv

14条回答
  •  萌比男神i
    2020-11-21 12:01

    One other option that hasn't been mentioned yet: use Groovy. Groovy allows you to access private instance variables as a side effect of the design of the language. Whether or not you have a getter for the field, you can just use

    def obj = new IWasDesignedPoorly()
    def hashTable = obj.getStuffIWant()
    

提交回复
热议问题