首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

反射中怎么区分参数是基本类型还是所对应的包装类

2012-10-26 
反射中如何区分参数是基本类型还是所对应的包装类package demo.testimport java.lang.reflect.Method/**

反射中如何区分参数是基本类型还是所对应的包装类

package demo.test;import java.lang.reflect.Method;/** * Java Reflection Cookbook<br/> * eg:<br/> * &nbsp;&nbsp;&nbsp;&nbsp;Reflection r = new Reflection(A.class);<br/> * &nbsp;&nbsp;&nbsp;&nbsp;Reflection r = new Reflection("com.ehi.A");<br/> */@SuppressWarnings("unchecked")public class Reflection {private Class clazz;private Object object;private Reflection() {}/** * construct method * @param obj */public Reflection(Object obj) {this.object = obj;clazz = obj.getClass();}/** * construct method * @param className * @throws Exception */public Reflection(String className) throws Exception {if (className == null)clazz = null;elseclazz = Class.forName(className);this.object = clazz.newInstance();}/** * 执行对象方法 * 当所执行的函数的参数包含基本类型的包装类是,必须为所有参数定义其参数类型 * @param methodName *            方法名 * @param args *            参数 * @param types * 当函数的参数包含基本类型的包装类时,此参数包含所以参数的类型信息 * @return 方法返回值 * @throws Exception */public Object invoke(String methodName, Object[] args, Class[] types)throws Exception {Class[] parameterTypes = null;if(types == null)parameterTypes = getParameterTypes(args);elseparameterTypes = types;Method method = clazz.getDeclaredMethod(methodName, parameterTypes);method.setAccessible(true); return method.invoke(object, args);}private Class[] getParameterTypes(Object[] args) throws Exception {if(args == null){return null;}Class[] parameterTypes = new Class[args.length];for (int i = 0, j = args.length; i < j; i++) {if(args[i] instanceof Integer){parameterTypes[i] = Integer.TYPE;}else if(args[i] instanceof Byte){parameterTypes[i] = Byte.TYPE;}else if(args[i] instanceof Short){parameterTypes[i] = Short.TYPE;}else if(args[i] instanceof Float){parameterTypes[i] = Float.TYPE;}else if(args[i] instanceof Double){parameterTypes[i] = Double.TYPE;}else if(args[i] instanceof Character){parameterTypes[i] = Character.TYPE;}else if(args[i] instanceof Long){parameterTypes[i] = Long.TYPE;}else if(args[i] instanceof Boolean){parameterTypes[i] = Boolean.TYPE;}else{parameterTypes[i] = args[i].getClass();}}return parameterTypes;}public void print(Integer i, int j){System.out.println("Integer: "+i.intValue());System.out.println("int: "+j);}public void print(int i){System.out.println("int: "+i);}public static boolean isWrapClass(Class clz) {        try {            return ((Class) clz.getField("TYPE").get(null)).isPrimitive();        } catch (Exception e) {            return false;        }    } public static void main(String[] args) throws Exception {Reflection r = new Reflection(new Reflection());Object[] obj = new Object[2];Integer I = new Integer(333);obj[0] = I;obj[1] = 444;Class[] clazz = new Class[2];clazz[0] = Integer.class;clazz[1] = int.class;r.invoke("print", obj, clazz);}}
3 楼 realorg 2008-11-27   请问 JDK1.4 里能这么做么?

热点排行