Skip to content

Latest commit

 

History

History
52 lines (44 loc) · 1.76 KB

README.md

File metadata and controls

52 lines (44 loc) · 1.76 KB

The Dang Field

We've all been in a situation where the lib wasn't flexible enough and didn't allow us to customize some aspect of it. So you went and contributed to the library to add the missing functionality or add some more setters to let you customize the behaviour that way. Yay, Open Source! 🎉

Who am I kidding. Nobody's got time for that. Just lemme set the dang field!

Ha! I'll use reflection!

val instance = SomeLibrary.getInstance()
val f = instance.getClass.getDeclaredField("privateEngine")
f.setAccessible(true)
f.set(instance, new MyAwesomeEngine())

... but then it turns out that the privateEngine field wasn't in the instance's class, but its superclass. And you just need to replace a piece of the engine, not the whole engine.

It gets ugly really quickly. This library aims to help with that:

import TheDangFieldImplicits._
val lib = SomeLibrary.getInstance()
// any of `privateEngine`, `valveFactory`, and `meaningOfLife` may be private, final, 
// and located arbitrarily deep in the class hierarchy
val i: Int = lib getMe TheDangField.privateEngine.valveFactory.meaningOfLife
lib set TheDangField.privateEngine.valveFactory.meaningOfLife to (i * 10)

You can see more examples in the tests.

But seriously. Go contribute to that dang library.

How to add as a dependency

maven

<repositories>
    <repository>
       <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
<dependency>
    <groupId>com.github.mrobakowski</groupId>
    <artifactId>the-dang-field</artifactId>
    <version>master</version>
</dependency>

other

Go to https://jitpack.io/ and figure it out.