简体   繁体   中英

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

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?

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM