Last active
August 29, 2015 14:17
-
-
Save ScalaWilliam/561c2f2dcd73ca2a894c to your computer and use it in GitHub Desktop.
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
myfine.domain.user.actor.handlers.BetterAnonymousClassApp$$anon$1(mutingStuff = let's see, otherStuff = List(works, too), stuff = works, moreLazyStuff = myfine.domain.user.actor.handlers.BetterAnonymousClassApp$$anon$1$$anon$2(extensionData = This is just some extension data)) | |
Disconnected from the target VM, address: '127.0.0.1:55780', transport: 'socket' | |
{ | |
"mutingStuff" : "let's see", | |
"otherStuff" : [ "works, too" ], | |
"stuff" : "works", | |
"moreLazyStuff" : { | |
"extensionData" : "This is just some extension data" | |
} | |
} |
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
trait BetterAnonymousClass { self => | |
def myName: String = getClass.getName | |
def fieldValues: List[(String, Any)] = { | |
import scala.reflect.runtime.universe._ | |
implicit val mirror = runtimeMirror(self.getClass.getClassLoader).classSymbol(self.getClass).toType | |
val publicVals = mirror.members.filter(_.isTerm).filter(_.asTerm.isVal).flatMap( | |
value => mirror.members.filter(_.isTerm).find(member => member.asTerm.name.toString == value.name.toString && !member.asTerm.isPrivate && member.isMethod) | |
) | |
val publicVars = mirror.members.filter(_.isTerm).filter(_.asTerm.isVar) | |
.filterNot(x => mirror.members.exists(m => m.name.toString == s"${x.name}$$lzycompute")) | |
.flatMap(value => mirror.members.filter(_.isTerm).find(member => member.asTerm.name.toString == value.name.toString && !member.asTerm.isPrivate && member.isMethod)) | |
val publicLazyVals = { | |
val extractName = """(.*)\$lzycompute""".r | |
mirror.members.filter(_.isTerm).map(_.name.toString).collect{case extractName(name) => name}.flatMap( | |
theName => | |
mirror.members.filter(_.isTerm).filter(_.name.toString == theName).filter(_.asTerm.isPublic).filter(_.isMethod).filterNot(_.asTerm.isPrivate) | |
) | |
} | |
val wantedFields = publicVals ++ publicLazyVals ++ publicVars | |
this.getClass.getMethods.collect { | |
case method if wantedFields.exists(_.name.decodedName.toString == method.getName) => | |
method.getName -> method.invoke(this) | |
}.toList | |
} | |
override def toString = { | |
val fields = fieldValues.map{case (k, v) => s"$k = $v"}.mkString(", ") | |
s"""$myName($fields)""" | |
} | |
} | |
object BetterAnonymousClass { | |
val serializer = new CustomSerializer[BetterAnonymousClass]({implicit fmts => (PartialFunction.empty, | |
{case bac: BetterAnonymousClass => Extraction.decompose(bac.fieldValues.toMap)})}) | |
} | |
object BetterAnonymousClassApp extends App { | |
val thingy = new BetterAnonymousClass { | |
val stuff = "works" | |
lazy val otherStuff = List("works, too") | |
var mutingStuff = "let's see" | |
def moreStuff = Option("does not work") | |
private val dontShow = "me" | |
lazy val moreLazyStuff = new BetterAnonymousClass { | |
val extensionData = "This is just some extension data" | |
} | |
} | |
println(thingy) | |
implicit val fmts = DefaultFormats + BetterAnonymousClass.serializer | |
println(org.json4s.jackson.Serialization.writePretty(thingy)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment