Stefan Strooves
Stefan Strooves

Reputation: 634

Get the method output parameters parameters

I using the following code to get the method input parameter but I getting the wrong params for example I for set salary I want to get the type (double )and the name (salary) . what i miss here ?

public void setSalery(double salery) {
    this.salery = salery;
  }

this is the code

for (Method method : classHandle.getMethods()) {


            Class<?>[] parameterTypes = method.getParameterTypes();

            for (Class<?> class1 : parameterTypes) {

                Field[] declaredFields = class1.getDeclaredFields();
                for (Field field : declaredFields) {
                    System.out.println(field.getName());


            }

Upvotes: 2

Views: 236

Answers (1)

Jim Garrison
Jim Garrison

Reputation: 86754

You can retrieve the parameter types but not the parameter names. They are of no significance except within the method, which at this point is opaque to you. getDeclaredFields() returns the fields in the types, not the parameter names.

To invoke such a method (using your example), assume

MyBean b = new MyBean(); // contains method setSalary(double salary)
Method m = ...           // a reference to a Method object for MyBean#setSalary(double salary)
double newSalary = ...;

Then do

m.invoke(b, new Double(newSalary));

Upvotes: 3

Related Questions