Reflection Library for the Scala Programming Language providing runtime reflection capabilities and metaprogramming support
—
Symbol representation for all definitions including classes, methods, fields, and modules with complete metadata access. Symbols represent named entities in Scala programs and provide access to their properties, relationships, and type information.
Core symbol operations and properties available on all symbol types.
/**
* Base symbol trait providing fundamental symbol operations
*/
trait SymbolApi { this: Symbol =>
/** Name of this symbol */
def name: Name
/** Full name including package path */
def fullName: String
/** Type signature of this symbol */
def info: Type
/** Type signature in the context of a specific type */
def infoIn(site: Type): Type
/** Owner symbol (enclosing class, method, or package) */
def owner: Symbol
/** Check if this symbol is private */
def isPrivate: Boolean
/** Check if this symbol is protected */
def isProtected: Boolean
/** Check if this symbol is public */
def isPublic: Boolean
/** Check if this symbol is a package */
def isPackage: Boolean
/** Check if this symbol is a class */
def isClass: Boolean
/** Check if this symbol is a module (object) */
def isModule: Boolean
/** Check if this symbol is a method */
def isMethod: Boolean
/** Check if this symbol is a term (value, variable, method, object) */
def isTerm: Boolean
/** Check if this symbol is a type (class, trait, type alias) */
def isType: Boolean
/** Check if this symbol is abstract */
def isAbstract: Boolean
/** Check if this symbol is final */
def isFinal: Boolean
/** Check if this symbol is static */
def isStatic: Boolean
/** Check if this symbol is synthetic (compiler-generated) */
def isSynthetic: Boolean
/** Get annotations on this symbol */
def annotations: List[Annotation]
/** Convert to term symbol (if applicable) */
def asTerm: TermSymbol
/** Convert to type symbol (if applicable) */
def asType: TypeSymbol
/** Convert to class symbol (if applicable) */
def asClass: ClassSymbol
/** Convert to method symbol (if applicable) */
def asMethod: MethodSymbol
/** Convert to module symbol (if applicable) */
def asModule: ModuleSymbol
}Symbols representing terms (values, variables, methods, objects).
/**
* Term symbol representing values, variables, methods, and objects
*/
trait TermSymbolApi extends SymbolApi { this: TermSymbol =>
/** Check if this is a val */
def isVal: Boolean
/** Check if this is a var */
def isVar: Boolean
/** Check if this is a lazy val */
def isLazy: Boolean
/** Check if this is stable (val or object) */
def isStable: Boolean
/** Check if this is an accessor method */
def isAccessor: Boolean
/** Check if this is a getter */
def isGetter: Boolean
/** Check if this is a setter */
def isSetter: Boolean
/** Check if this is a case accessor */
def isCaseAccessor: Boolean
/** Check if this is a parameter */
def isParameter: Boolean
/** Check if this has a default value */
def isParamWithDefault: Boolean
/** Check if this is implicit */
def isImplicit: Boolean
/** Associated getter for var/val */
def getter: Symbol
/** Associated setter for var */
def setter: Symbol
/** Accessed field for accessor methods */
def accessed: Symbol
}Symbols representing types (classes, traits, type aliases).
/**
* Type symbol representing classes, traits, and type aliases
*/
trait TypeSymbolApi extends SymbolApi { this: TypeSymbol =>
/** Check if this is a type alias */
def isAliasType: Boolean
/** Check if this is an abstract type */
def isAbstractType: Boolean
/** Check if this type is covariant */
def isCovariant: Boolean
/** Check if this type is contravariant */
def isContravariant: Boolean
/** Get type parameters */
def typeParams: List[Symbol]
/** Convert to class symbol if this is a class */
def asClass: ClassSymbol
/** Get the aliased type for type aliases */
def aliasedType: Type
/** Get type bounds for abstract types */
def bounds: TypeBounds
}Symbols representing classes and traits.
/**
* Class symbol representing classes and traits
*/
trait ClassSymbolApi extends TypeSymbolApi { this: ClassSymbol =>
/** Check if this is a trait */
def isTrait: Boolean
/** Check if this is a case class */
def isCaseClass: Boolean
/** Check if this is a module class (object implementation) */
def isModuleClass: Boolean
/** Check if this is a primitive class */
def isPrimitive: Boolean
/** Check if this is a numeric class */
def isNumeric: Boolean
/** Check if this is derived from a value class */
def isDerivedValueClass: Boolean
/** Get primary constructor */
def primaryConstructor: Symbol
/** Get all constructors */
def constructors: List[Symbol]
/** Get base classes in linearization order */
def baseClasses: List[Symbol]
/** Get companion module (object) */
def companion: Symbol
/** Get this type (C.this.type) */
def thisType: Type
/** Get self type */
def selfType: Type
/** Get type signature as ClassInfoType */
def toType: Type
/** Check if this class is derived from another class */
def isSubClass(that: Symbol): Boolean
}Symbols representing methods (def).
/**
* Method symbol representing method definitions
*/
trait MethodSymbolApi extends TermSymbolApi { this: MethodSymbol =>
/** Check if this is a constructor */
def isConstructor: Boolean
/** Parameter lists (for multiple parameter lists) */
def paramLists: List[List[Symbol]]
/** Return type */
def returnType: Type
/** Check if this method is varargs */
def isVarargs: Boolean
/** Check if this is a macro */
def isMacro: Boolean
/** Type parameters */
def typeParams: List[Symbol]
/** Check if method has multiple parameter lists */
def isMultipleParameterLists: Boolean
}Symbols representing objects and packages.
/**
* Module symbol representing objects and packages
*/
trait ModuleSymbolApi extends TermSymbolApi { this: ModuleSymbol =>
/** Check if this is a package object */
def isPackageObject: Boolean
/** Get the module class (implementation class) */
def moduleClass: Symbol
/** Get companion class */
def companion: Symbol
}Methods for navigating symbol relationships and lookups.
/**
* Symbol lookup and navigation operations
*/
trait SymbolNavigation {
/** Find member by name */
def member(name: Name): Symbol
/** Find declaration by name */
def decl(name: Name): Symbol
/** Get all members */
def members: MemberScope
/** Get all declarations */
def decls: MemberScope
/** Get owner chain */
def ownerChain: List[Symbol]
/** Check if symbol is accessible from given context */
def isAccessibleFrom(site: Type): Boolean
/** Get all overridden symbols */
def allOverriddenSymbols: List[Symbol]
/** Get directly overridden symbol */
def overriddenSymbol: Symbol
/** Get all symbols that override this one */
def overridingSymbol: Symbol
}Usage Examples:
import scala.reflect.runtime.universe._
import scala.reflect.runtime.{currentMirror => cm}
// Basic symbol inspection
case class Person(name: String, age: Int) {
def greet(other: String): String = s"Hello $other, I'm $name"
var mood: String = "happy"
}
val personType = typeOf[Person]
val personClass = personType.typeSymbol.asClass
println(s"Class name: ${personClass.name}")
println(s"Full name: ${personClass.fullName}")
println(s"Is case class: ${personClass.isCaseClass}")
println(s"Is final: ${personClass.isFinal}")
// Constructor inspection
val constructor = personClass.primaryConstructor.asMethod
println(s"Constructor: ${constructor.name}")
println(s"Parameter lists: ${constructor.paramLists}")
constructor.paramLists.head.foreach { param =>
println(s" Parameter: ${param.name} -> ${param.info}")
println(s" Is implicit: ${param.asMethod.isImplicit}")
}
// Method inspection
val greetMethod = personType.decl(TermName("greet")).asMethod
println(s"Greet method: ${greetMethod.name}")
println(s"Return type: ${greetMethod.returnType}")
println(s"Is public: ${greetMethod.isPublic}")
greetMethod.paramLists.head.foreach { param =>
println(s" Parameter: ${param.name} -> ${param.info}")
}
// Field inspection
val nameField = personType.decl(TermName("name")).asTerm
println(s"Name field: ${nameField.name}")
println(s"Is val: ${nameField.isVal}")
println(s"Is stable: ${nameField.isStable}")
println(s"Is case accessor: ${nameField.isCaseAccessor}")
val moodField = personType.decl(TermName("mood")).asTerm
println(s"Mood field: ${moodField.name}")
println(s"Is var: ${moodField.isVar}")
println(s"Getter: ${moodField.getter}")
println(s"Setter: ${moodField.setter}")/**
* Utility methods for filtering and analyzing symbols
*/
object SymbolUtils {
/** Get all methods from a type */
def methods(tpe: Type): List[MethodSymbol] =
tpe.members.filter(_.isMethod).map(_.asMethod).toList
/** Get all fields from a type */
def fields(tpe: Type): List[TermSymbol] =
tpe.members.filter(m => m.isTerm && !m.isMethod).map(_.asTerm).toList
/** Get all nested types from a type */
def nestedTypes(tpe: Type): List[TypeSymbol] =
tpe.members.filter(_.isType).map(_.asType).toList
/** Get public members only */
def publicMembers(tpe: Type): List[Symbol] =
tpe.members.filter(_.isPublic).toList
/** Get constructors */
def constructors(cls: ClassSymbol): List[MethodSymbol] =
cls.info.members.filter(_.isConstructor).map(_.asMethod).toList
}Complete Symbol Analysis Example:
import scala.reflect.runtime.universe._
def analyzeSymbol(sym: Symbol): Unit = {
println(s"Symbol: ${sym.name} (${sym.getClass.getSimpleName})")
println(s"Full name: ${sym.fullName}")
println(s"Owner: ${sym.owner.name}")
println(s"Type: ${sym.info}")
// Visibility
val visibility = if (sym.isPublic) "public"
else if (sym.isProtected) "protected"
else "private"
println(s"Visibility: $visibility")
// Modifiers
val modifiers = List(
if (sym.isFinal) Some("final") else None,
if (sym.isAbstract) Some("abstract") else None,
if (sym.isStatic) Some("static") else None,
if (sym.isSynthetic) Some("synthetic") else None
).flatten
if (modifiers.nonEmpty) {
println(s"Modifiers: ${modifiers.mkString(", ")}")
}
// Annotations
if (sym.annotations.nonEmpty) {
println("Annotations:")
sym.annotations.foreach { ann =>
println(s" @${ann.tree}")
}
}
// Type-specific analysis
if (sym.isClass) {
val cls = sym.asClass
println(s"Is trait: ${cls.isTrait}")
println(s"Is case class: ${cls.isCaseClass}")
println(s"Base classes: ${cls.baseClasses.map(_.name).mkString(", ")}")
}
if (sym.isMethod) {
val method = sym.asMethod
println(s"Return type: ${method.returnType}")
println(s"Parameter lists: ${method.paramLists.size}")
method.paramLists.zipWithIndex.foreach { case (params, i) =>
println(s" Param list $i: ${params.map(p => s"${p.name}: ${p.info}").mkString(", ")}")
}
}
println()
}
// Analyze various symbols
val stringType = typeOf[String]
analyzeSymbol(stringType.typeSymbol)
val listType = typeOf[List[Int]]
analyzeSymbol(listType.typeSymbol)
val optionType = typeOf[Option[String]]
analyzeSymbol(optionType.typeSymbol)Install with Tessl CLI
npx tessl i tessl/maven-org-scala-lang--scala-reflect