w******t 发帖数: 1422 | 1 ok, I have a following bean class:
public class user {
String a1;
String a2;
....
String a20
setA1(){}
getA1(){}
...
setA20(){}
getA20(){}
}
I have a hashtable like following
key value |
g*s 发帖数: 2277 | 2 java.lang.Class
java.lang.reflect.Method
【在 w******t 的大作中提到】 : ok, I have a following bean class: : public class user { : String a1; : String a2; : .... : String a20 : setA1(){} : getA1(){} : ... : setA20(){}
|
w******t 发帖数: 1422 | 3 show me how. Reflection is something I know. Thanks
【在 g*s 的大作中提到】 : java.lang.Class : java.lang.reflect.Method
|
g*s 发帖数: 2277 | 4 Hashtable ht = ....;
user userObject = ....;
for (Enumeration e = ht.keys() ; e.hasMoreElements() ;) {
String key = (String)e.nextElement();
String value = (String)ht.get(key);
Object [] methodParamValues = new Object[1];
methodParamValues[0] = value;
Method method = userObject.getClass().getMethod("set"+key,
new Class [] { new String().getClass() });
method.invoke(userObject, methodParamValues);
}
【在 w******t 的大作中提到】 : show me how. Reflection is something I know. Thanks
|
w******t 发帖数: 1422 | 5 thanks.
It's not gong to work for my case though - in my real case, key and method
does not match that well. (key is string, it's from database table value, may
contain space)
I solved this issue other way.
【在 g*s 的大作中提到】 : Hashtable ht = ....; : user userObject = ....; : for (Enumeration e = ht.keys() ; e.hasMoreElements() ;) { : String key = (String)e.nextElement(); : String value = (String)ht.get(key); : Object [] methodParamValues = new Object[1]; : methodParamValues[0] = value; : Method method = userObject.getClass().getMethod("set"+key, : new Class [] { new String().getClass() }); : method.invoke(userObject, methodParamValues);
|