How do I import a java classname with a dollar sign ($), or refer to a field or method with one, in Jython?

杀马特。学长 韩版系。学妹 提交于 2019-12-12 17:30:52

问题


Naive attempts fail: e.g.

from mypackage import SomeClass$

gives

javax.script.ScriptException: SyntaxError: no viable alternative at character '$' in at line number 1

This question is unrelated to java nested classes, which are easily importable in Jython.

Such a class is for example the scala.Predef$ class, which one can then access with Predef$.foobar.

How can I achieve this import?


回答1:


I found a solution using Java Reflection like this:

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.



来源:https://stackoverflow.com/questions/44623470/how-do-i-import-a-java-classname-with-a-dollar-sign-or-refer-to-a-field-or

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!