反射机制解决多重ifelse base语句_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 反射机制解决多重ifelse base语句

反射机制解决多重ifelse base语句

 2015/3/19 1:00:19  fkueje001  程序员俱乐部  我要评论(0)
  • 摘要:packagecom.suneee.common;importjava.lang.reflect.InvocationTargetException;importjava.lang.reflect.Method;importjava.util.HashMap;importjava.util.Map;publicclassReflectInstance
  • 标签:解决 反射 ASE 反射机制
class="java" name="code">
package com.suneee.common;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class ReflectInstance {

	/**
	 * 
	 * @param classPath  classpath
	 * @param methodName  methodName
	 * @param args   its type can be (String,Integer,Boolean,Map,List)
	 */
	@SuppressWarnings("unchecked")
	public static Map<String,String> sendMsg(String classPath,String methodName,Map<String,String> args1,Map<String,Object> args2) {
		try {
			Class<?> simpleClass = Class.forName(classPath);
			Method simpleMethod = null;
			Object simpelObject = simpleClass.newInstance();
			
				simpleMethod = simpleClass.getDeclaredMethod(methodName, Map.class,Map.class);
			
			simpleMethod.setAccessible(true);
			Map<String,String> result = (Map<String, String>) simpleMethod.invoke(simpelObject, args1,args2);
			return result;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args)  {
		Map map1 = new HashMap();
		map1.put("aa", "kill me!");
		Map map2 = new HashMap();
		map2.put("bb", " don't kill me !");
		sendMsg("com.test.common.ReflectInstance","sendMsg",map1,map2);

	}
	
	public Map<String,String> sendMsg(Map<String,String> args,Map<String,Object> args2){
		System.out.println("order-----------"+args);
		Map<String,String> map = new HashMap<String,String>();
		map.put("test", args.get("aa"));
		return map;
	}
}
发表评论
用户名: 匿名