package test;import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class ReflectionTest { public static void main(String[] args) throws NoSuchMethodException,InvocationTargetException,IllegalAccessException{
  //观察
  reflection(Student.class);
  
  //生成需要复制给bean 的值 list -> map;
  List<Object> list=new ArrayList<Object>();
  Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "小李");
map.put("age", "1");
map.put("goToSchool", true);
map.put("aaa", "11");
list.add(map);

//生成一个储存bean的list, list -> bean.
List<Object> listStudent = new ArrayList<Object>();

//给存储list负值
for (Object object : list) {
Student student=new Student();
for (String key : ((Map<String,Object>)object).keySet()) {
Method method=setMethods(Student.class,key);
method.invoke(student, ((Map<String,Object>)object).get(key));
}
listStudent.add(student);
}

//把存储的list -> bean 还原成 list -> map 值.
List<Map<String,Object>> list2=new ArrayList<Map<String,Object>>();

for (Object object2 : listStudent) {
list2.add( getMethods(object2.getClass()) );
}

for (Map<String, Object> map2 : list2) {
for (String key : map2.keySet()) {
System.out.println(map2.get(key));
}
}
} /**
 * 反射小小应用,只要来看看能拿到的东西.
 * 
 * @param cls
 * @throws NoSuchMethodException
 */
public static void reflection(Class cls) throws NoSuchMethodException {
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
System.out.println("Name  ::  " + field.getName());
System.out.println("Type  ::  " + field.getType().getName());
System.out.println("Class  ::  " + field.getDeclaringClass());
System.out.println(" boolean " +(field.getType().getName().equals("boolean")));
int mod = field.getModifiers();
System.out.println(Modifier.isPrivate(mod));
System.out.println(Modifier.isPublic(mod));
System.out.println(Modifier.isProtected(mod));
System.out.println(Modifier.isFinal(mod));
System.out.println(Modifier.isStatic(mod));
System.out.println();
} Method[] methods = cls.getDeclaredMethods();
for (Method method : methods) {
System.out.println("Method  ::  " + method.getName());
System.out.println("Param Type  ::  " + method.getParameterTypes());
System.out.println("Return Type  ::  " + method.getReturnType().getName());
System.out.println();
}
} /**
 * 获得bean set方法.
 * 
 * @param cls
 * @param fieldName
 * @return
 * @throws NoSuchMethodException
 */
public static Method setMethods(Class cls, String fieldName) throws NoSuchMethodException {
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
if(field.getName().equals(fieldName)){
StringBuffer methodName=new StringBuffer("set");//方法名
methodName.append(fieldName.substring(0,1).toUpperCase());
methodName.append(fieldName.substring(1));
Class type=field.getType();//参数类型
System.out.println("setMethods: methodName  ::  "+methodName.toString());
System.out.println("setMethods: type  ::  "+type);
return cls.getDeclaredMethod(methodName.toString(), type);
}
}
return null;
} /**
 * 获得bean get方法,boolean类型的get方法只能是isXXX().
 * 
 * @param cls
 * @param fieldName
 * @return
 * @throws NoSuchMethodException
 */
public static Method getMethods(Class cls, String fieldName) throws NoSuchMethodException{
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
if(field.getName().equals(fieldName)){
StringBuffer methodName=new StringBuffer();//方法名称
if(field.getType().getName().equals("boolean")){
methodName.append("is");
}else{
methodName.append("get");
}
methodName.append(fieldName.substring(0,1).toUpperCase());
methodName.append(fieldName.substring(1));
Class type=field.getType();
return cls.getDeclaredMethod(methodName.toString(), type);
}
}
return null;
} /**
 * 把bean的值放到map中去.
 * 
 * @param cls
 * @return
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 */
public static Map getMethods(Class cls) throws InvocationTargetException,IllegalAccessException {
  Method []methods=cls.getDeclaredMethods();
  Map<String,Object> map=new HashMap<String, Object>();
  for (Method method : methods) {
   if( !method.getName().substring(0,3).equals("set") ){
    StringBuffer fieldName=new StringBuffer();
    String name=method.getName();
    if(method.getReturnType().getName().equals("boolean")){
     fieldName.append(name.substring(2,3).toLowerCase());
     fieldName.append(name.substring(3));
    }else{
     fieldName.append(name.substring(3,4));
     fieldName.append(name.substring(4));
    }
    map.put(fieldName.toString(), method.invoke(cls, null));
   }
  }
  return null;
 }
}

解决方案 »

  1.   

    问题是:这只是一个自己做的bean的拆装箱,但是报异常了,想调通,但是要下班了,就把它挂网上了。O(∩_∩)OStudent 只有3个属性String name,int age,boolean goToSchool
      

  2.   

    List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>(); for (Object object2 : listStudent) {
    list2.add(getMethods(object2.getClass()));
    }
    这里类型转换错误
      

  3.   

    for (Object object : list) {
    Student student = new Student();
    for (String key : ((Map<String, Object>) object).keySet()) {
    Method method = setMethods(Student.class, key);
    method.invoke(student, ((Map<String, Object>) object).get(key));
    }
    listStudent.add(student);
    }
    错误在这
      

  4.   

    map.put("name", "小李");
    map.put("age", 1);
    map.put("goToSchool", true);
    //map.put("aaa", "11");
    程序没有错,是因为你的数据错误,age必须对应整数,你原来是字符串,肯定要出现转换异常。没了
    还有最后一个数据,在我这里会爆出空指针异常,因为student里面没有这个Field
      

  5.   

    是的,类型转换错误,JAVA反射时的bean属性类型还是要自己转下的。