0

I have a Bean class User I create object dynamically during run time.I can do set the value in setter method but that setter method have multiple arguments.

This My User Bean Class

 public class User {
 private String name1;
 private String name2;
 private String name3;
 private int age1;
 private int age2;
 public String getName1() {
 return name1;
 }
 public void setName1(String name1,String name2,String name3) {
 this.name1 = name1;
 this.name2 = name2;
 this.name3 = name3;
 }
 public int getAge1() {
 return age1;
 }
 public void setAge1(int age1,int age2) {
 this.age1 = age1;
 this.age2 = age2;
 }
 @Override
 public String toString() {
 return "StudentUser [name1=" + name1 + ", name2=" + name2 + ", name3=" + name3 + ", age1=" + age1 + ", age2="
 + age2 + "]";
 }

I want to invoke this setter method using reflection.I can find anything during runtime like method Name method Parameter Type and also have order of Parameter Type.

for my case I have some set of default values for setter method like primitive type and non primitive type and find the method arguments type during run time and invoke setter method and set default values for them

My Main Method :-

public static Object getBean(String beanClassName) throws Exception
 {
 Class klass = Class.forName(beanClassName); //->Get Class Name By Path
 Object obj = klass.newInstance(); //->Create The Object of Class 
 Method[] b = klass.getDeclaredMethods(); //->Get Declared Method in Class
 for(Method m : b)
 { 
 Type[] pType = m.getGenericParameterTypes();
 for(int i=0;i<pType.length; i++)
 {
 System.out.println("The Arguments :"+pType[i]+" Arguments Order :"+i);
 if(pType[i].equals(String.class))
 {
 m.setAccessible(true);
 m.invoke(obj,"Hello");
 }
 else if(pType[i].equals(int.class))
 {
 System.out.println("Machted int");
 m.setAccessible(true);
 m.invoke(obj,21);
 }
 }
 }
 return obj;
 }

I can do that i have execpetion ArgumentMissMacth.I want to set Every String Type to "Hello" and every Int Type to 23 And Object To Null How can identify dynamically which order to set.

My case I would know Method Parameter type but have to set the deafult value according to method parameter type.

asked Jan 16, 2019 at 8:13
1
  • But I want to set Value According to Method Argument Type problem is invoking a right setter method Commented Jan 16, 2019 at 8:32

2 Answers 2

2

Method takes 3 argument but you call method with only 1 argument. You should collect all parameters value then invoke method.

Here is sample code.

public static Object getBean(String beanClassName) throws Exception {
 Class klass = Class.forName(beanClassName); // ->Get Class Name By Path
 Object obj = klass.newInstance(); // ->Create The Object of Class
 Method[] b = klass.getDeclaredMethods(); // ->Get Declared Method in
 // Class
 for (Method m : b) {
 Type[] pType = m.getGenericParameterTypes();
 if(pType.length==0){
 continue;
 }
 /**
 * Create new array to hold value of parameters
 */
 Object[] params = new Object[pType.length];
 for (int i = 0; i < pType.length; i++) {
 System.out.println("The Arguments :" + pType[i] + " Arguments Order :" + i);
 if (pType[i].equals(String.class)) {
 params[i] = "Hello";
 } else if (pType[i].equals(int.class)) {
 params[i] = 21; 
 }
 }
 m.setAccessible(true);
 /**
 * Invoke method with all paramtters.
 */
 m.invoke(obj, params);
 }
 return obj;
}
answered Jan 16, 2019 at 8:57
1
  • I can't use Object Array is it possible Commented Jan 16, 2019 at 9:22
0

You have a setter for name1 that takes 3 strings as parameters, but in your getBean method, you are invoking it dynmaically using m.invoke(obj,"Hello");, that means, that you are trying to invoke a method named setName1 with only one parameter, and this method does not exist.

For explanation purposes, I edited it to m.invoke(obj, "Hello","Hello","Hello"); and it works.

The same goes for the setAge1 : m.invoke(obj, 21,21);

The goal is that, you have to provide as more objects to the invoke method as the number of the parameters you have declared in the method

answered Jan 16, 2019 at 9:01
2
  • it is HardCode m.invoke(obj, "Hello","Hello","Hello"); Commented Jan 16, 2019 at 9:30
  • yeah, that's why I said it is only for explanation purposes Commented Jan 16, 2019 at 10:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.