getting Class object for a Java class

530 views
Skip to first unread message

Jonah Graham

unread,
May 18, 2016, 3:31:04 PM5/18/16
to Py4J Support and Comments
What is the way to get the class object of a JavaClass?

i.e. the py4j equivalent to: java.lang.String.class

I need it to be able to call methods such as OSGi like "getService" that takes an interface as the parameter.

In Jython, I can do something like getService(java.lang.String).

Thanks,
Jonah

Barthelemy Dagenais

unread,
May 18, 2016, 4:06:04 PM5/18/16
to Jonah Graham, Py4J Support and Comments
Hi Jonah,

There is no direct method to get the java.lang.Class from a
py4j.java_gateway.JavaClass. I used to just call Class.forName(fqn)
when I needed a java.lang.Class on the python side. I'm not opposed to
a feature request to support .class, but in the meantime, here are a
few workarounds that are more robust than Class.forName:

1. If you have an instance, you can always use .getClass()
ArrayList = gateway.jvm.java.util.ArrayList()
ArrayListClass = ArrayList.getClass()

This won't work for java.lang.String or primitives though.

2. You can use Py4J's ReflectionUtil, since the .class attribute would
likely end up using it:

ReflectionUtil = gateway.jvm.py4j.reflection.ReflectionUtil
JavaString = ReflectionUtil.classForName("java.lang.String")

If you have a py4j.java_gateway.JavaClass instance, you would do:

String = gateway.jvm.py4j.java.lang.String
ReflectionUtil = gateway.jvm.py4j.reflection.ReflectionUtil
JavaString = ReflectionUtil.classForName(String._fqn)


Since 0.9.2, the class loading strategy can be modified globally and
everything goes through ReflectionUtil.


Regarding the implementation of an hypothetical .class attribute: the
Java side of Py4j does not keep references to
py4j.java_gateway.JavaClass: it refetches the class through reflection
every time a static method, static field, or constructor is called.
That's why ultimately, a call to .class would end up calling
ReflectionUtil.

HTH,
Barthelemy
> --
> 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/5aa621ea-242f-4fd7-9eee-97bcf82eb3da%40py4j.org.

Jonah Graham

unread,
May 18, 2016, 4:33:57 PM5/18/16
to Barthelemy Dagenais, Py4J Support and Comments
Thanks, That does help.

I am trying to port
https://gist.github.com/tracymiranda/e9588d0976c46a987463 from using
the Jython engine in EASE to the Py4J engine.

Thanks
Jonah

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

Barthelemy Dagenais

unread,
May 18, 2016, 4:38:33 PM5/18/16
to Jonah Graham, Py4J Support and Comments
I just thought about this : you could monkey patch the .class attribute and once it's released you can just remove the monkey patching.

Jonah Graham

unread,
May 18, 2016, 4:56:03 PM5/18/16
to Barthelemy Dagenais, Py4J Support and Comments
I have the autosave from the gist working now, thanks for your help.

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


Reply all
Reply to author
Forward
0 new messages