JPype的属性使用属性名称访问shadows方法

时间:2013-10-01 17:03:28

标签: python jpype

当同一个类有next()方法时,如何访问名为getNext()的Java方法?

JPype具有仅使用属性名称即可访问bean属性(不带参数的get-Methods)的功能。因此,如果你有一个方法getNext()的类,你可以使用instance.next从python中访问该bean属性,这在99.9%的情况下是很好的。但是如何访问instance.next()?如果我调用instance.next(),我将得到一个异常,说bean属性的返回类型不可调用。

2 个答案:

答案 0 :(得分:1)

你可能不得不求助于在底层java类上使用反射:

# 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)

答案 1 :(得分:0)

相关问题