Registry
ReflectRegistry is one global map from type names to descriptors. Register a type once, then look it up, create it and ask about subtypes by name.
Register
With accessors and a factory
register reflects the type with accessors, binds its runtime class and the factory, and stores it under its full and its simple name.
- ReflectRegistry.loadClass("demo.Query").map(_.simpleName)
- Some(Query)
- ReflectRegistry.loadClassBySimpleName("Query").isDefined
- true
- ReflectRegistry.createInstance("demo.Query")
- Some(Query(select * from dual))
- ReflectRegistry.getPropertyAccessor("demo.Query", "sql").isDefined
- true
scala
import reflect.ReflectRegistry
ReflectRegistry.register(() => Query("select * from dual"))
ReflectRegistry.loadClass("demo.Query")
ReflectRegistry.createInstance("demo.Query")
ReflectRegistry.getPropertyAccessor("demo.Query", "sql")
Other entries
Descriptors you built yourself
registerByTypeName stores any descriptor, for example one with a bound runtime class and factory.
scala
val descriptor = ReflectMacros.reflect[Query]
ReflectRegistry.registerByTypeName(
descriptor.typeName,
descriptor.bindRuntimeClass(classOf[Query]).bindFactory(() => Query("select 1"))
)
Global state
The registry lives as long as the program. Tests that register types should call ReflectRegistry.clear(); code that needs isolation should use a ReflectClassLoader.