Invoke java & scala methods with dollar sign ($) in name

300 views
Skip to first unread message

Nic T

unread,
Dec 17, 2017, 7:47:47 AM12/17/17
to Py4J Support and Comments
I had some trouble invoking Java / Scala methods with a dollar sign ($) in their name.
These are methods generated by a pre-compilation step e.g. AVDL code generation.

Other times you may come across them as synthetic methods in Scala bytecode generation.

For example in my case I had a POJO with setUserName$1().
I emailed Barthelemy and he gave me a solution, I'll post here in case others stumble upon this:
You could try py4j.java_gateway.get_method(my_obj, "setUserName$1")

Rgds,
Nic

Imran Rashid

unread,
Aug 16, 2018, 11:05:39 PM8/16/18
to Py4J Support and Comments
old thread, but thought I'd add another useful bit of info here.  I found that if I needed to access a class name with a $ in it, I needed to use __getattr__.  And if its a scala package object, double whammy as that requires extra escaping in the jvm.  eg. to get to to a scala package object defined in org.apache.spark.internal.config, I used

org.apache.spark.internal.config.__getattr__("package$").__getattr__("MODULE$")

Jonah Graham

unread,
Aug 17, 2018, 4:20:13 AM8/17/18
to imran...@gmail.com, Py4J Support and Comments
BTW you probably shouldn't be calling __getattr__ directly and you should use getattr() instead -- https://docs.python.org/3/library/functions.html#getattr. Using __getattr__ directly bypasses some of the mechanisms in Python. So I think this should be your line (untested):

package_dollar = getattr(org.apache.spark.internal.config, "package$")
module_dollar = getattr(package_dollar, "MODULE$")

HTH,
Jonah



~~~
Jonah Graham
Kichwa Coders Ltd.
www.kichwacoders.com


--
You received this message because you are subscribed to the Google Groups "Py4J Support and Comments" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4j+uns...@py4j.org.
To post to this group, send email to py...@py4j.org.
To view this discussion on the web visit https://groups.google.com/a/py4j.org/d/msgid/py4j/beff2c9e-e990-411d-b192-c6766928e00d%40py4j.org.
Reply all
Reply to author
Forward
0 new messages