Skip to content

Instantly share code, notes, and snippets.

@maxaf
Created July 8, 2011 12:24
Show Gist options
  • Save maxaf/1071721 to your computer and use it in GitHub Desktop.
Save maxaf/1071721 to your computer and use it in GitHub Desktop.
come smell my desperation
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