Question

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.

Was it helpful?

OTHER TIPS

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top