Skip to content
Advertisement

Is there a way to typecast interface using Jpype?

I am trying to call Java code from Python using Jpype and trying to implement Interface using JProxy for callbacks. It is giving me error that “TypeError: Cannot create Java interface instances” If i try to cast it e.g.

proxy = jpype.JProxy("PutCallback", dict=d, convert=False) 
javaPackage.TestClient.put(jLang.String("Pi"), 3, expiryType, jLang.String(" "), amazon.proxy)

But if i pass it directly It gives error that no method found as my method is expecting PuCallback while i am passing is jproxy object

proxy = jpype.JProxy("PutResultCallback", dict=d, convert=False)
javaPackage.TestClient.put(jLang.String("Pi"), 3, expiryType, jLang.String(" "), proxy)```
  javaPackage.TestClient.put("Pi", 3, expiryType, " ", proxy)

TypeError: No matching overloads found for 
TestClient.put(java.lang.String,int,client.Dat
aExpiryType,java.lang.String,_jpype._JProxy), options are:
        
public void TestClient.put(java.lang.String,boolean,client.DataExpiryType,java.la
ng.String,client.PutResultCallback)```

Advertisement

Answer

This will be achieved by removing the static reference from Dev package, It automatically captures the proxy object.

No need to typecast it to interface.

Note: If your method is static then you will need to call it via static reference.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement