[英]Obtain Java Class from Scala TypeTag in Macros
According to this question/answer Any way to obtain a Java class from a Scala (2.10) type tag or symbol? 根据此问题/答案,是否可以从Scala(2.10)类型的标记或符号中获取Java类?
I should use runtimeClass
to obtain the Class
from a type. 我应该使用runtimeClass
从类型获取Class
。
But if I do reflection in Macros I don't have access to runtimeClass
. 但是,如果我在宏中进行反射,则无法访问runtimeClass
。 How can I do? 我能怎么做?
In macros, you can have a TypeTag
for a class which isn't actually compiled yet and thus doesn't have a Class
object, that's why there is no runtimeClass
. 在宏中,您可以为尚未真正编译的类提供一个TypeTag
,因此还没有Class
对象,这就是为什么没有runtimeClass
的原因 。 If you want to try anyway (eg because you know the class is from a dependency, not being compiled), I don't know any way other than Class.forName
, as in the question. 如果您仍然想尝试(例如,因为您知道该类来自某个依赖项,而不是正在编译),那么除了Class.forName
之外,我不知道其他方法。 Of course, you can use runtimeClass
in the generated code. 当然,您可以在生成的代码中使用runtimeClass
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.