Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Class[_] to universe.Type/Symbol

I'm trying to list all classes from a package and extract some metadata information through the new reflection api(2.10).

I may be wrong, but seems that there is no support to list classes from a package on the scala api.

I'm using a java library, but it returns only the Class<?>

It is possible to convert a Class[_] to universe.Type or universe.Symbol?

like image 268
Rodrigo Ribeiro Avatar asked Feb 21 '13 17:02

Rodrigo Ribeiro


1 Answers

You shoud use mirrors of scala.reflect.runtime.universe (JavaUniverse)

Suppose you have a runtime class:

val cls: Class[_] = someObject.getClass

You should get runtimeMmirror (JavaMirror) for your classloader and call its classSymbol method, which converts runtime class to ClassSymbol for this universe:

import scala.reflect.runtime._

val typ: universe.ClassSymbol = universe.runtimeMirror(cls.getClassLoader).classSymbol(cls)
like image 131
alno Avatar answered Oct 17 '22 16:10

alno