def getField(field: java.lang.reflect.Field, value: Any): AnyRef = {
val c = field.getDeclaringClass
val classMirror = mirror.classSymbol(c)
val method = classMirror.toType.members.collect{case m: MethodSymbol => m}.
find(s => s.name.decoded == field.getName && s.returnType.typeSymbol.asClass.fullName == field.getType.getName)
method.map{m =>
println(m.name)
println(m.returnType)
// m.invoke(value) ???
}.getOrElse(field.get(value))
}
This doesn't work, of course.Hi.I have a method where I want to call the compiler-generated getter (ie. created() for field created) if such a method exists (which is; Having the same name and return-type of the field) for a given java.lang.reflect.Field fof a given instance:def getField(field: java.lang.reflect.Field, value: Any): AnyRef = { val c = field.getDeclaringClass val classMirror = mirror.classSymbol(c) val method = classMirror.toType.members.collect{case m: MethodSymbol => m}. find(s => s.name.decoded == field.getName && s.returnType.typeSymbol.asClass.fullName == field.getType.getName) method.map{m => println(m.name) println(m.returnType) // m.invoke(value) ??? }.getOrElse(field.get(value)) }This doesn't work, of course.What is the best way to do this?Thanks
def getField(field: java.lang.reflect.Field, value: Any): AnyRef = {
val c = field.getDeclaringClass
val classMirror = mirror.classSymbol(c)
val im = mirror.reflect(value)
val method = classMirror.toType.members.collect{case m: MethodSymbol => m}.
find(s => s.name.decoded == field.getName && s.returnType.typeSymbol.asClass.fullName == field.getType.getName)
method.map{m =>
val mm = im.reflectMethod(m)
mm.apply().asInstanceOf[AnyRef]
}.getOrElse(field.get(value))
}
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
2) Repeated lookups of symbols and creations of mirrors are quite slow. However, in 2.11.0-M3 there's some remedy for that in the form of FieldMirror.bind and MethodMirror.bind that you can use to swiftly product mirrors for the same underlying symbols, but for different targets. People have reported significant performance boosts coming from bind. Unfortunately we can't add new APIs in 2.10.x because of binary compatibility constraints, so this will have to wait for 2.11 (or you can probably cast your way around even in 2.10).Sorry for not having time to come up with an end-to-end example, but here's what might help:1) I'd look through { case m: TermSymbol if !m.isMethod && m.isVal }, and then use the getter method on the result.
def getField(field: java.lang.reflect.Field, value: Any): AnyRef = {
val c = field.getDeclaringClass
val classMirror = mirror.classSymbol(c)
val im = mirror.reflect(value)
classMirror.toType.declarations.find(_ match {
case m: TermSymbol if m.name.decoded == (field.getName + " ") && !m.isMethod && m.isVar => true
case _ => false
}).collect{case m: TermSymbol => m}.map{m =>
val getter = m.getter
val mm = im.reflectMethod(getter.asInstanceOf[MethodSymbol])
mm.apply().asInstanceOf[AnyRef]
}.getOrElse(field.get(value))
}
--