2

How can I access a Java method named next() when the same class has a getNext() method?

JPype has the feature to give you access to bean properties (get-Methods without a parameter) using just the property name. So if you have a class with a method getNext() you can access that bean property from within python using instance.next which is nice in 99,9% of the cases. But how can I access the instance.next()? If I call instance.next() I'll get an exception saying that the return type of the bean property is not callable.

bastian
  • 1,122
  • 11
  • 23

2 Answers2

1

You'll probably have to resort to using reflection on the underlying java class:

# iterate through all methods
for method in instance.__javaclass__.getMethods():
    # find the method matching the correct signature
    if method.name == u'next' and len(method.parameterTypes) == 0:
        # create a function that invokes the method on the given object
        # without additional arguments
        next_instance = lambda o: method.invoke(o, [])
        break
else:
    raise Exception('method was not found')

nxt = next_instance(instance)
mata
  • 67,110
  • 10
  • 163
  • 162
  • Thank you, it works. But I prefer to use the fix that is now in originell's jpype fork (see my own answer). – bastian Feb 12 '14 at 14:08
  • Yes, that's of course better if it's fixed. My solution is just a workaround for versions that don't support this yet. – mata Feb 13 '14 at 07:11
0

It is fixed in originell's jpype fork https://github.com/originell/jpype/commit/0e6067f2eec78f6c697b3714f467142fa9b58243

bastian
  • 1,122
  • 11
  • 23