I am trying to reflect a class with implicit param, how can I get the actual implicit param at runtime to obtain a instance of this class?
object Validator extends App {
val between = Between(1, 3)
println(getAnotherInstance(between))
def getAnotherInstance[T: TypeTag](obj: T)(implicit tag: ClassTag[T]) = {
val tpe = typeOf[T]
val lowerTerm = tpe.member(TermName("lower")).asTerm
val upperTerm = tpe.member(TermName("upper")).asTerm
val lower = mirror.reflect(obj).reflectField(lowerTerm).get
val upper = mirror.reflect(obj).reflectField(upperTerm).get
val constructorSymbol = typeOf[T].member(termNames.CONSTRUCTOR).alternatives.head.asMethod
mirror.reflectClass(tpe.typeSymbol.asClass).reflectConstructor(constructorSymbol).apply(lower, upper, Ordering[Int]) // problem is the Ordering[Int]
}
}
case class Between[T](lower: T, upper: T)(implicit order: Ordering[T]) extends ValidateAnnotation {
def validate(name: String, value: T): Option[String] = {
if(order.lteq(value, lower) || order.gteq(value, upper)) Some(s"$name is not between $lower and $upper") else None
}
}
The actual type of implicit param Ordering[Int] is confirmed at runtime, is there a way to get the implicit parameter(maybe Ordering[String], Ordering[Date]) at runtime? So that I can pass it to apply method then obtain an instance.