Created
July 8, 2011 12:24
-
-
Save maxaf/1071721 to your computer and use it in GitHub Desktop.
come smell my desperation
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 UglyHacks { | |
implicit def screwEncapsulation(o: AnyRef) = new { | |
private def field(fieldName: String) = { | |
val field = o.getClass.getDeclaredField(fieldName) | |
field.setAccessible(true) | |
field | |
} | |
def shoeHorn(fieldName: String, fieldValue: Any) = field(fieldName).set(o, fieldValue) | |
def squeezeOut[T: Manifest](fieldName: String): Option[T] = { | |
try { Some(manifest[T].erasure.cast(field(fieldName).get(o)).asInstanceOf[T]) } | |
catch { case _ => None } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment