1. getMethods()与getDeclaredMethods()的区别
public Method[] getMethods()返回某个类的所有公用(public)方法包括其继承类的公用方法,当 然也包括它所实现接口的方法。
public Method[] getDeclaredMethods()对象表示的类或接口声明的所有方法,包括公共、保护、默 认(包)访问和私有方法,但不包括继承的方法。当然也包括它所实现接口的方法。
2.getFields()与getDeclaredFields()的区别
getDeclaredFields()返回所有字段包括私有字段
getFields()只返回公有字段即public修饰的字段
学习反射获取传入JavaBean的参数名与参数值并转为Map
方法一:
public static Map<String, Object> convertBean(Object bean)
throws IntrospectionException, IllegalAccessException, InvocationTargetException {
Class type = bean.getClass();
Map<String, Object> returnMap = new HashMap<String, Object>();
BeanInfo beanInfo = Introspector.getBeanInfo(type);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i< propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
System.out.println(propertyName);
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(bean, new Object[0]);
if (result != null) {
returnMap.put(propertyName, result);
} else {
returnMap.put(propertyName, "");
}
}
}
return returnMap;
}
public static void main(String[] args) {
Student s = new Student();
s.setAge(12);
s.setSex("nan");
s.setUserName("张^^^");
Course course = new Course();
course.setCourseName("高数");
course.setCourseCode("BH675FG");
s.setCourse(course);
try {
Map<String, Object> gh = convertBean(s);
Iterator<Entry<String, Object>> iterator = gh.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<java.lang.String, java.lang.Object> entry = (Map.Entry<java.lang.String, java.lang.Object>) iterator
.next();
System.out.println(entry);
}
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IntrospectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
方法二:
<pre name="code" class="java">package com.orgJson;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import com.model.Course;
import com.model.Student;
public class JsonUtil {
public static Map<Object, Object> toMap(Object javaBean) {
Map<Object, Object> result = new HashMap<Object, Object>();
Method[] methods = javaBean.getClass().getDeclaredMethods();
for (Method method : methods) {
try {
if (method.getName().startsWith("get")) {
String field = method.getName();
System.out.println("刚拿到方法名称" + field);
field = field.substring(field.indexOf("get") + 3);
System.out.println("get截取掉" + field);
field = field.toLowerCase().charAt(0) + field.substring(1);
System.out.println("put map前" + field);
Object value = method.invoke(javaBean, (Object[]) null);
result.put(field, null == value ? "" : value.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static void main(String[] args) {
Student s = new Student();
s.setAge(12);
s.setSex("nan");
Course course = new Course();
course.setCourseName("高数");
s.setCourse(course);
Map<Object, Object> g = toMap(s);
Iterator<Entry<Object, Object>> d = g.entrySet().iterator();
while (d.hasNext()) {
Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) d
.next();
System.out.println(entry);
}
}
}