I have a class:
package org.apache.project
class Foo {
def bar: List[Bar] = ...
}
is it a way in scala reflection that allows me to get typeOf[List[Bar]] from the className "org.apache.project.Foo" and the method name "bar"?
Thanks a lot for your help!
Yes, if you have Foo
as a Type
, you can find the return type of the method bar
as a Type
as well.
import scala.reflect.runtime.universe._
typeOf[Foo] // Get the type of Foo
.decls // Get Foo's declared members
.find(_.name.toString == "bar") // Find the member named "bar" as a symbol
.head // Unsafely access it for now, use Option and map under normal conditions
.asMethod // Coerce to a method symbol
.returnType // Access the return type
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.