scalajs-reflection is a combination of an sbt plugin and a run-time API enabling a controlled amount of run-time reflection in Scala.js projects.
Add the following line to your project/plugins.sbt
file:
addSbtPlugin("be.doeraene" % "sbt-scalajs-reflection" % "0.1.1")
and the following settings to your build.sbt
file:
enablePlugins(ScalaJSReflectionPlugin)
libraryDependencies += "be.doeraene" %%% "scalajs-reflection" % "0.1.1"
In addition, you need to specify what classes will be reflectible, and what
reflective operations will be enabled on them.
To do so, use the scalaJSReflectSelectors
setting.
For example, to enable Reflect.getClassForName
on all subclasses of some
class or trait foo.Bar
, use:
scalaJSReflectSelectors ++= Seq(
selectDescendentClasses("foo.Bar") -> reflectClassByName()
)
Each element of the Seq
must be a pair of an entity selector and a
reflective operation. The operation will be enabled on all classes matched
by the entity selector.
Available selectors are:
selectSingleClass("foo.Bar")
: select exclusivelyfoo.Bar
selectDescendentClasses("foo.Bar")
: selectfoo.Bar
and all its descendants
Note that, to select the class of a Scala object
, you need to append a $
at the end of its full name, e.g., "foo.Bar$"
.
Available operations are:
reflectClassByName()
: enablesReflect.getClassForName
reflectEnumerateClass()
enablesReflect.enumerateClasses
reflectDeclaredConstructors()
: enablesReflect.getDeclaredConstructors
reflectModuleAccessor()
: enablesReflect.loadModule
be.doeraene.sjsreflect.Reflect
Reflect.getClassForName(fqcn: String): Class[_]
: find a class by its nameReflect.enumerateClasses: Array[Class[_]]
: an array of the specified classesReflect.getDeclaredConstructors[T](clazz: Class[T]): Array[Constructor[T]]
: lists the declared constructors of the given classReflect.loadModule[T](clazz: Class[T]): T
: loads the module instance of the given module class (a Scalaobject
)
be.doeraene.sjsreflect.Constructor[T]
ctor.getParameterTypes(): Array[Class[_]]
: returns the list of the parameter types of the constructorctor.newInstance(actualArgs: Any*): T
: invokes the constructor with the given arguments, and returns the created instance