I have an object that looks like this:
object Settings { final val Host = "host" final val Protocol = "protocol" object User { final val Name = "username" final val Password = "password" } object Subject { final val Query = "query" final val Predicate = "predicate" } }
What I would like to do is something like membersAsHash(classOf[CollectionSettings]) and get the hash) of all the vals that I declared in the object:
[ Host => "host", Protocol => "protocol", Name => "username", Password => "password", Query => "query", Predicate => "predicate" ]
It would be nice if the key was a string, even the full name of the package (for example, com.example.Settings.User). I really need values, so if I can only get this, it's still acceptable.
This gave me the name of the subobjects, but I cannot figure out how to get the shafts that are internal to each:
val optionsToCheck = { import scala.reflect.runtime.{universe => ru} val mirror = ru.runtimeMirror(getClass.getClassLoader) val subObjects = ru.typeOf[CollectionSettings.type].declarations.filter(_.isModule) subobjects.map(o => mirror.reflectModule(o.asModule).instance.asInstanceOf[Object].toString).toList }
source share