I have a mother trait 'Contract' that is extended by a number of case classes, 'scale' being one of them. I am trying to make a generic function such that it takes an object of one of those case classes and does some action on the basis of what kind of object it is. This is the partial code:
def getType[T: TypeTag](obj: T) = typeOf[T]
def makeXMLElem[T <: Contract](contract: T): String = {
println(getType(contract))
val symb = getType(contract).declaration(newTermName(name)).asTerm
val instanceMirror = runtimeMirror(contract.getClass.getClassLoader).reflect(contract)
val symbMirror = instanceMirror.reflectField(symb)
val symbValue = symbMirror.get
.......
Now, I try to pass 'scale' and check its type. The getType function returns its type to be 'Contract' rather than 'scale'. As you can understand, I am trying to access the parameters of the case class 'scale' using the statement:
val symb = getType(contract).declaration(newTermName(name)).asTerm
case class 'scale' has the following signature:
case class scale(amount:Int, currency:String)
Since, the type itself is being extracted wrongly, the value 'symb' does not give any value and I get the following runtime error:
Caused by: scala.ScalaReflectionException: <none> is not a term
How can I make the function makeXMLElem more generic without the loss of information regarding the signature of 'scale' or any class that extends 'Contract' for that matter?