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;
}
}
反射机制解决多重ifelse base语句
最新推荐文章于 2022-08-24 16:05:21 发布