Class descriptors
A ClassDescriptor holds everything the compiler knows about a class: its properties, constructors, base types, type parameters, annotations and flags.
Properties
Name, type and access
Each PropertyDescriptor says whether a property can be read and written. A var parameter is writable, a val is not.
Settings, live
Read from the descriptor of the class below.
- getProperty("theme")
- java.lang.String, readable true, writeable true
- getProperty("version")
- scala.Int, readable true, writeable false
scala
case class Settings(var theme: String, version: Int)
val descriptor = ReflectMacros.reflect[Settings]
val theme = descriptor.getProperty("theme").get
println(theme.propertyType.typeName)
Case classes list more than their fields
A case class descriptor also contains the members the compiler generates for Product: productArity, productPrefix, _1, _2 and so on. When you need the fields only, go by the primary constructor's parameterNames.
Structure
Constructors and base types
Constructors come with their parameters, base types as fully qualified names. Both are what the registry uses to create instances and answer subtype questions.
- dog.constructors.head.parameterNames
- name
- dog.baseTypes
- demo.Animal, java.lang.Object, scala.Matchable, scala.Any
- dog.isSubTypeOf("demo.Animal")
- true
- dog.isCaseClass
- false
scala
class Animal(val name: String)
class Dog(name: String) extends Animal(name)
val dog = ReflectMacros.reflect[Dog]