Failed to find constructor when there are multiple(overloaded) constructors
See original GitHub issueFound a bug with pyjnius when dealing with a Java class with multiple constructors.
When I executed the below Java code, the main method will invoke the 2nd constructor and finish gracefully.
When I execute the python code below, it will report the constructor is not found.
(jnius.JavaException: No constructor matching your arguments, available: ['(ILjava/lang/String;)V', '(ILjava/lang/String;Ljava/lang/Object;[I)V'])
But when I removed the 1st constructor in java code, my python code with pyjnius can correctly find the only constructor and finish gracefully.
Java Class
public class SampleJavaClass {
public SampleJavaClass( int arg1, String arg2, int arg3, Object arg4, int... arg5 ) {
System.out.println("arg1: " + Integer.toString(arg1));
System.out.println("arg2: " + arg2);
System.out.println("arg3: " + arg3);
System.out.println("arg3: " + arg4);
System.out.println("arg4: " + arg5.toString());
}
// the constructor plans to use
public SampleJavaClass (int arg1, String arg2, Object arg3, int... arg4) {
System.out.println("arg1: " + Integer.toString(arg1));
System.out.println("arg2: " + arg2);
System.out.println("arg3: " + arg3);
System.out.println("arg4: " + arg4.toString());
}
public static void main(String[] args) {
SampleJavaClass test = new SampleJavaClass(1, "var2", null, 4);
}
}
Python code with pyjnius
import os
currentPath = os.getcwd()
classpath = currentPath + "/SampleJavaClass.class"
import jnius_config
jnius_config.set_classpath('.', classpath)
from jnius import autoclass
def main():
SampleJavaClass = autoclass("SampleJavaClass")
SampleJavaClass(1, "var2", None, 4)
print("Execution finished!")
if __name__ == "__main__":
main()
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (7 by maintainers)
Top Results From Across the Web
Constructor Overloading Error - java
This program is used to find the area of a circle and a rectangle // through constructor overloading concept. class area { float...
Read more >Removing repetitive code (overloading methods and ...
The technique of having two (or more) constructors in a class is known as constructor overloading. A class can have multiple constructors that...
Read more >Error using overloaded constructors (with classes as ...
Basically I'm trying to print out the names of weapons I put into an array of Infantry object's "hands". I print out two...
Read more >Constructor Overloading in Java
Overloaded constructor is called based upon the parameters ... Sometimes there is a need of initializing an object in different ways.
Read more >Constructors (C++)
A constructor has the same name as the class and no return value. You can define as many overloaded constructors as needed to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
When pyjnius calls a method or constructor it first checks if there are 0, 1, or multiple constructors or methods with the given name. If there is only 1 it always makes the call, hoping for the best. If there are multiple, it does this scoring process to try to decide if each is acceptable or not. Hopefully one and only one is acceptable; if not, it throws an error.
In this case, it is rejecting both as constructors are unacceptable. Using a signature hint gets around the problem by causing it to skip going through the scoring process. The signature hint is a good workaround.
For this issue, I took a closer look at the code and found there is in fact a bug that is causing this to fail. And happily it’s an easy fix. I’ll make a PR with a unit test in a moment.
Never mind, I figured it out and have a fix for this. I’ll add it to the PR.