Assuming I have a general superclass:
class GenericExample[T]( a: String, b: T ) { def fn(i: T): T = b }
and specific subclass:
case class Example( a: String, b: Int ) extends GenericExample[Int](a, b)
I want to get a parameter of the type of the function "fn" when scala is reflected, so I select and filter through its elements:
import ScalaReflection.universe._ val baseType = typeTag[Example] val member = baseType .tpe .member(methodName: TermName) .asTerm .alternatives .map(_.asMethod) .head val paramss = member.paramss val actualTypess: List[List[Type]] = paramss.map { params => params.map { param => param.typeSignature } }
I expected scala to give me the correct result, which is List(List(Int)) , instead I only got a generic List(List(T))
Having folded the document, I found that the Signature type is the culprit:
* This method always returns signatures in the most generic way possible, even if the underlying symbol is obtained from an * instantiation of a generic type.
And he offers me to use an alternative:
def typeSignatureIn(site: Type): Type
However, since the Example class is no longer generic, I cannot get the site from typeTag [Example], can anyone suggest me how to get typeOf [Int] only for typeTag [Example]? Or is there no way to do this, and should I return to Java reflection?
Many thanks for your help.
UPDATE: After some quick test, I found that even MethodSymbol.returnType is not working properly, the following code:
member.returnType
also gives T , annd cannot be fixed asSeenFrom, since the following code does not change the result:
member.returnType.asSeenFrom(baseType.tpe, baseType.tpe.typeSymbol.asClass)