How to access (non-exposed) Java-API of exposed (core-)classes from Python code?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

How to access (non-exposed) Java-API of exposed (core-)classes from Python code?

Stefan Richthofer
Hi all,

is there a way to access Java-API of Jython's (core-)classes that are exposed via the expose mechanism (i.e. using @ExposedType, @ExposedGet etc) from Python code? I mean Java methods or fields that are not annotated with @ExposedX.

Accessing Java-API of ordinary Java objects is trivial in Jython, but ironically this seems to be not true for Jython's own Java-API. So far I only found a way where you need to write plain Java-code.
It seems not even to be possible via reflection, because the relevant Class objects are inherently mapped to PyType objects, which don't feature API crucial for reflection like getField, getMethod, etc.

Some things I tried:
(tb is a traceback object)

from org.python.core import Py, PyTraceback, PyObject, PyJavaType

print tb.toString()
print Py.tojava(tb, PyTraceback).toString()
print Py.tojava(tb, PyTraceback.__class__).toString()
print Py.tojava(tb, 'org.python.core.PyTraceback').toString()
print Py.tojava(tb, 'java.lang.Object').toString()
print Py.tojava(tb, PyJavaType).toString()
print Py.tojava(tb, 'org.python.core.PyJavaType').toString()
print Class.forName('org.python.core.PyTraceback').getMethod('toString')

I can use reflection if I write plain Java code, e.g.
public class ReflectHelper {
  public static Method getMethod(Object prototype, String name, Class<?>... parameterTypes) throws NoSuchMethodException {
    return prototype.getClass().getMethod(name, parameterTypes);

Then I can call it like

ReflectHelper.getMethod(tb, 'toString').call(tb)

which would be okay for me, except that I would have to bundle Java-code, eventually class-file as well.
I agree that for exposed objects, Java-level API should be somewhat hidden on Python-level, but I think you should be able to access it if you know what you're doing. Especially given that it is possible anyway via plain Java-code.

If someone knows a proper way how to do it (or any "pure" Python-code level way in Jython at all), please tell me! What option do I overlook?



Check out the vibrant tech community on one of the world's most
engaging tech sites,!
Jython-dev mailing list
[hidden email]