Java反射的一个问题。求帮助
接收一串字符串,格式像这样的(key=value|key=value||||)key 代表Field的name,value代表set进Object中的值,将他装换成一个Object对象,调用setter方法为其赋值,我用下面的方法调用method.invoke(obj, obj..);测试的时候用的其中一个Bean类Student。Student里面有很多Filed,怎么把value(String)转换invoke方法的第2个参数的类型啊,是像我下面那样一个一个判断吗,还是有别的方法啊。大家帮帮忙=。= 谢谢……^^
package com.ytbysj.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Map.Entry;
import com.ytbysj.beans.Student;
import com.ytbysj.exception.FooException;
/**
* Message Handler类
* @author haniyaseyo
*
*/
public class MessageHandler {
/**
* 将message转换成Map对象
* @param message like:key=value|key=value||||
* @return 通过message转换成的MAP key=value
*/
public static Map<String,String> messageHandler(String message){
//如果message=null则抛出异常
if(message == null) throw new FooException("MessageHandler messageHandler(String message) message=null");
//判断message的格式
if(message.indexOf("|") == -1) throw new FooException("MessageHandler messageHandler(String message) message do not contains "|"");
if(message.indexOf("=") == -1) throw new FooException("MessageHandler messageHandler(String message) message do not contains "="");
//定义Map对象
Map<String, String> map = new HashMap<String, String>();
//定义StringTokenizer对象 并指定"|"为分隔符
StringTokenizer stken = new StringTokenizer(message, "|");
/*
* 循环StringTokenizer中的对象 并复制到Map中
*/
while (stken.hasMoreTokens()) {
String[] temp = stken.nextToken().split("=");
map.put(temp[0], temp[1]);
}
return map;
}
/**
* 根据给定的message和Class构造一个?对象
* @param message like:key=value|key=value||||||
* @param clazz ?的Class对象
* @return clazz.newInstance()
*/
public static Object getObject(String message, Class<?> clazz){
//获取Map
Map<String, String> map = messageHandler(message);
//判断Map是否为空
if(map.isEmpty()) throw new FooException("MessageHandler getObject(String message, Class<?> clazz) map is empty");
Object obj = null;
try {
obj = clazz.newInstance();
Iterator<Entry<String,String>> ite = map.entrySet().iterator();
while(ite.hasNext()){
Entry<String,String> entry = ite.next();
//取出每一个key value
String item_name = entry.getKey().trim();
String value = entry.getValue().trim();
//构造方法名
String methodName = "set" + String.valueOf(item_name.charAt(0)).toUpperCase() + item_name.substring(1);
//获取Field的类型
Class<?> type = clazz.getDeclaredField(item_name).getType();
System.out.println(methodName + "," + value + "," +type.getName());
//获取Method对象
Method method = clazz.getDeclaredMethod(methodName, type);
//type.getClassLoader().
//判断Field类型
if(type == value.getClass()){
method.invoke(obj, value);
}else if(type == int.class){
method.invoke(obj, Integer.parseInt(value));
}//....如果在这里接着判断有很多不方便的地方-。-
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return obj;
}
public static void main(String[] args) {
String message = "student_no=0810024103|password= |username= |sex=男" +
"|moblie=13396736137|native_place=重庆|college= |tie=软件工程|stuclass=09级2班|limits=3";
System.out.println(getObject(message,Student.class));
}
}