Created
June 1, 2016 13:30
-
-
Save lhohan/b9c466a2def1e42acc201cb8172a031a to your computer and use it in GitHub Desktop.
Set any private field on any Scala or Java class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Reflect { | |
implicit class Reflect(ref: AnyRef) { | |
/** | |
* Set the value of a (private) field. | |
*/ | |
def setV(name: String, value: Any): Unit = { | |
val declaredField = ref.getClass.getDeclaredField(name) | |
declaredField.setAccessible(true) | |
declaredField.set(ref, value) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment