For example, I have a Class A with a constructor A(List list) Class A {public A(List list) {}}Now, I want to use Java reflect to create A class's instance.My problem is, as I have a parameter Object, I can know this object type. It's a concrete class not an interface, eg: I have a ArrayList object obj eg: Class paramType[] = new Class[]{obj.getClass()}; constructor.getConstructor(paramType, new Object[]{obj});When I do these, it will throw NoSuchMethodException.Seams I couldn't know the relationship between its class and interface.
Perhaps u should code in this way.4 the Method getConstructor in class Class metions as follow: public Constructor<T> getConstructor(Class... parameterTypes) The parameterTypes parameter is an array of Class objects that identify the constructor's formal parameter types, in declared order. so,code list this. Class A {public A(List list) {}} and u got a object a. Class classA=a.getClass(); Constructor con=classA.getConstructor(new Object[]{List.getClass}); con is what u want.next time don't type in English.If you insist 2,pls type the right words make no wrong,some like "Seams"/ The code below i haven't complie,i m busy. so,any issues,let me know.
It's not flexible, as you have to pass in the constuctor argument type. You can get it from the object you passed in.
why not use "Object" or "Object[]" as constructor parameter type? Class A {public A(Object obj) {}public A(Object[] obj) {} }
Class A {public A(List list) {}}Now, I want to use Java reflect to create A class's instance.My problem is, as I have a parameter Object, I can know this object type. It's a concrete class not an interface, eg: I have a ArrayList object obj
eg: Class paramType[] = new Class[]{obj.getClass()};
constructor.getConstructor(paramType, new Object[]{obj});When I do these, it will throw NoSuchMethodException.Seams I couldn't know the relationship between its class and interface.
public Constructor<T> getConstructor(Class... parameterTypes)
The parameterTypes parameter is an array of Class objects that identify the constructor's formal parameter types, in declared order.
so,code list this.
Class A {public A(List list) {}}
and u got a object a.
Class classA=a.getClass();
Constructor con=classA.getConstructor(new Object[]{List.getClass});
con is what u want.next time don't type in English.If you insist 2,pls type the right words make no wrong,some like "Seams"/
The code below i haven't complie,i m busy.
so,any issues,let me know.
You can get it from the object you passed in.
Class A {public A(Object obj) {}public A(Object[] obj) {}
}