By means of an introspection for ordinary javabean properties, why many a class, I thought this class animal sign are inherited from the Object, but view the Object class source code Only this way the public final native Class getClass (); , and this method is final, and do not make me the javabean object inheritance. But why would the class this term?
This is java bean:
public class ITest {
private String appid;
private String mch_id;
private String device_info;
private String Body;
private String nonce_str;
public String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getMch_id() {
return mch_id;
}
public void setMch_id(String mch_id) {
this.mch_id = mch_id;
}
public String getDevice_info() {
return device_info;
}
public void setDevice_info(String device_info) {
this.device_info = device_info;
}
public String getBody() {
return Body;
}
public void setBody(String body) {
Body = body;
}
public String getNonce_str() {
return nonce_str;
}
public void setNonce_str(String nonce_str) {
this.nonce_str = nonce_str;
}
}
This is my method:
public static Map<String, Object> beanToMap(Object bean, boolean isempty)
throws IntrospectionException, IllegalAccessException, IllegalArgumentException {
Class<? extends Object> type = bean.getClass();
Map<String, Object> params = new TreeMap<String, Object>();
BeanInfo info = Introspector.getBeanInfo(type);
PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
for (int i = 0; i < descriptors.length; i++) {
PropertyDescriptor descriptor = descriptors[i];
String propertyName = descriptor.getName();
Method readMethod = descriptor.getReadMethod();
try {
Object result = readMethod.invoke(bean, new Object[0]);
System.out.println(propertyName + "=" + result);
if (result != null) {
params.put(propertyName, result);
} else {
if (isempty)
params.put(propertyName, "");
}
} catch (InvocationTargetException e) {
log.error("readMethod invoke" + bean + ":" + e.getMessage());
}
}
return params;
}
Why did the results more class attribute this item
appid=wxd930ea5d5a258f4f
body=test
class=class com.haothink123.entity.ITest
device_info=1000
mch_id=10000100
nonce_str=ibuaiVcKdpRxkhJA
Aucun commentaire:
Enregistrer un commentaire