[英]How do I import a java classname with a dollar sign ($), or refer to a field or method with one, in Jython?
Naive attempts fail: eg 天真的尝试失败:例如
from mypackage import SomeClass$
gives 给
javax.script.ScriptException: SyntaxError: no viable alternative at character '$' in at line number 1
javax.script.ScriptException:SyntaxError:在第1行的字符“ $”处没有可行的替代方法
This question is unrelated to java nested classes, which are easily importable in Jython. 这个问题与Java嵌套类无关,后者可以在Jython中轻松导入。
Such a class is for example the scala.Predef$
class, which one can then access with Predef$.foobar
. 例如,此类就是
scala.Predef$
类,然后可以使用Predef$.foobar
进行访问。
How can I achieve this import? 如何实现此导入?
I found a solution using Java Reflection like this: 我找到了使用Java Reflection的解决方案,如下所示:
from java.lang import Class
person = Class.forName("full.path.to.Person$").getField("MODULE$").get(None)
It's not pretty but it works. 它不是很漂亮,但是可以。 It might be nicer to use the Scala reflection API if that's available.
如果可用的话,最好使用Scala反射API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.