jpype-project / jpype

JPype is cross language bridge to allow Python programs full access to Java class libraries.
http://www.jpype.org
Apache License 2.0
1.12k stars 183 forks source link

Lost in the java & python type system #1187

Closed dingiso closed 6 months ago

dingiso commented 6 months ago

Hi everyone,

I am using jpype to call java functions. However, the function signature is not matched. I am trying to fix but it still wrong. Can someone help me to figure out?

The java function is as below: ( sorry about it, this function is too long and many self-defined types, but I have carefully handled them)

public FidPopulateResult createNewLibraryFromPrograms(FidDB fidDb, String libraryFamilyName,
            String libraryVersion, String libraryVariant, List<DomainFile> programDomainFiles,
            Predicate<Pair<Function, FidHashQuad>> functionFilter, LanguageID languageId,
            List<LibraryRecord> linkLibraries, List<String> commonSymbols, TaskMonitor monitor)
            throws MemoryAccessException, VersionException, CancelledException,
            IllegalStateException, IOException {

The wrong message output by python is as below:

TypeError: No matching overloads found for 
ghidra.feature.fid.service.FidService.createNewLibraryFromPrograms(ghidra.feature.fid.db.FidDB,
str, str, str, list, NoneType, ghidra.program.model.lang.LanguageID,
NoneType, list, ghidra.util.task.ConsoleTaskMonitor), 

options are:
public ghidra.feature.fid.service.FidPopulateResult 
ghidra.feature.fid.service.FidService.createNewLibraryFromPrograms(ghidra.feature.fid.db.FidDB,
java.lang.String, java.lang.String, java.lang.String, java.util.List, java.util.function.Predicate, ghidra.program.model.lang.LanguageID,
java.util.List, java.util.List, ghidra.util.task.TaskMonitor)
throws 
ghidra.program.model.mem.MemoryAccessException,ghidra.util.exception.VersionException,ghidra.util.exception.CancelledException,java.lang.IllegalStateException,java.io.IOException

The code I used to call the function

# fidDb: ghidra.feature.fid.db.FidDB
# name: str 
# file: DomainFile 
# langID: ghidra.program.model.lang.LanguageID
result_ = service.createNewLibraryFromPrograms(fidDb, 
                name, "1.0", "", [file], None, langID, None, [], monitor)

The type error comes out, as we can see there are two parameters do not have same type:

If everyone could help me or give me some advice, I will fully appreciate it.

Best Regards, Dingisoul

Thrameos commented 6 months ago

That is a very complex call. Your best bet would be to force casting on each argument and determine which casting operator failed.

To use a cast apply the "@" operator.

Ie. java.lang.String@"hello" would force the type of the argument to be java.lang.String rather than a Python string.

My guess is some automatic conversion that you are assuming doesn't work. It may be fixed by casting, or it may tell you the conversion is not possible. But either way it should move you towards a solutions.

dingiso commented 6 months ago

Thanks, I have found the solution, jpype will not convert python list to java list in this situation, after I change the original [file] to java.util.ArrayList([file]) , it goes OK. Best Regards, Dingisoul