Skip to content

Scala library for when you need to get or set that dang private field in some friggin 3rd party lib.

Notifications You must be signed in to change notification settings

mrobakowski/the-dang-field

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Scala library for when you need to get or set that dang private field in some friggin 3rd party lib.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages