Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid call, number of argument mismatch, got 2 need 1, found definitions ('Ljava/lang/String,[Ljava/lang/Class;',) in class b'java/lang/Class' method b'getMethod' #738

Closed
Sahil-pixel opened this issue Dec 6, 2024 · 3 comments

Comments

@Sahil-pixel
Copy link

I want to Get the Method class and retrieve the 'printInt' method using reflection
Running on Ubuntu Pyjnius (1.6.1)

python file

from jnius import autoclass

# Step 1: Load the Java Class class
Class = autoclass('java.lang.Class')
JInteger=autoclass('java.lang.Integer')
Array = autoclass('java.lang.reflect.Array')
String = autoclass('java.lang.String')
parameter_types=Array.newInstance(Class, 1)


# Step 2: Create a list to represent the parameter types array
#parameter_types = [None]  # Create a list with a size of 1 (similar to new Class<?>[1] in Java)

# Step 3: Assign the int.class (primitive type representation in Java) to the first element
parameter_types[0] = Class.forName('java.lang.Integer')

# Step 4: Use the array (e.g., pass it to a reflective method)
Example = autoclass('org.Example')  # Load your Example class
example_instance = Example()  # Create an instance of the Example class

# Get the Method class and retrieve the 'printInt' method using reflection
#Method = autoclass('java.lang.reflect.Method')
method = example_instance.getClass().getMethod(String("printInt"), parameter_types)

# Invoke the method with the appropriate arguments
#method.invoke(example_instance, 42)

Example.java

package org;
public class Example {
    public void printInt(int number) {
        System.out.println("Number: "+number);
    }
}

i got some Error
method = example_instance.getClass().getMethod(String("printInt"), parameter_types)
File "jnius/jnius_export_class.pxi", line 846, in jnius.JavaMethod.call
jnius.JavaException: Invalid call, number of argument mismatch, got 2 need 1, found definitions ('Ljava/lang/String,[Ljava/lang/Class;',) in class b'java/lang/Class' method b'getMethod'

@cmacdonald
Copy link
Contributor

this should be sufficient

jnius.autoclass("org.Example")().printInt(5)

@Sahil-pixel
Copy link
Author

this should be sufficient

jnius.autoclass("org.Example")().printInt(5)

I know that but i want to do that test for another application. Please let me know how we use Pyjnius for reflection
To implement
socket = (BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device, 1);
Line using pyjnius
edufolly/flutter_bluetooth_serial#154 (comment)

@cmacdonald
Copy link
Contributor

All methods exposed in the underlying class should be available using getattr(), e.g.

getattr(getattr(jnius.autoclass("java.lang.System"), "out"), "println")("hello")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants