vendredi 28 juillet 2017

Scala reflection. Is there a convenient way to verify that reflection class is module class (companion object)?

I'm trying to find out if the class is an implementation of a companion object, or it's a regular class that corresponds to some type of data.

def checkIsCompanionClass(universe:scala.reflect.api.Universe, class:Class[_]):Boolean = {
    val classSymbol = universe.rootMirror.staticClass(cls.getName)
    classSymbol.isModule //or classSymbol.isModuleClass
}

I tried this, but isModule and isModuleClass return false for "companion" classs (class name ends with $)





Aucun commentaire:

Enregistrer un commentaire