lundi 15 février 2016

Java to Scala type conversion - collection of reflected classes

I use org.reflections to scan a package in my project and get all classes that extend a certain trait, like so:

val reflections = new Reflections("classpath")<br>
val classSet = reflections.getSubTypesOf(classOf[Trait])

I end up with a util.Set[Class[_ : Trait]]
If i do classSet.toSet.map(x => x.getName -> x).toMap using JavaConversions i get a 'missing parameter type' error.

If i do classSet.toSet.map(x => Map(x.getName -> x)).reduce(_ ++ _) i get the same error.
If i replace the toSet to toList i get a 'type mismatch' error.

I want to convert this set to a map of the class name as key and the reflected class as value.
Note - when i build an external map like var classMap = Map[String, Class[_ <: Trait]]() and then pushing every element into this map, by iterating over the Set. I can't access the class's methods i implemented from the Trait.





Aucun commentaire:

Enregistrer un commentaire