`
aimer311
  • 浏览: 97351 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

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

    博客分类:
  • java
J# 
阅读更多
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;
		else
			clazz = Class.forName(className);
		this.object = clazz.newInstance();
	}
	
	/**
	 * 根据方法名,参数,查找对应的方法,并执行
	 * 
	 * @param methodName
	 *            方法名
	 * @param args
	 *            参数
	 * @return 方法返回值
	 * @throws Exception
	 */
	public Object invoke(String methodName, Object[] args)
			throws Exception {
		Class[] parameterTypes = getParameterTypes(args);
		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){
		System.out.println("Integer: "+i.intValue());
	}
	
	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[1];
		int i = 333;
		obj[0] = i;
                //Integer I = new Integer(333);
                //obj[0] = I;
		r.invoke("print", obj);
	}
}

invoke要实现的是根据方法名和输入的参数自动查找对应的方法,并执行之。遇到的问题就是如何区分Object[]里面存储的是一个基本类型还是其对应的包装类?或者这样的思路根本就是有问题的,请提供下其他的思路。
1
2
分享到:
评论
3 楼 realorg 2008-11-27  
请问 JDK1.4 里能这么做么?
2 楼 aimer311 2008-10-10  
当程序执行到如下时:
int i = 333; 
obj[0] = i; 内存实际上存储的是一个Integer对象。
也就是说当把i存储到数组后就已经是一个Integer的对象了,那么以后就没办法知道它具体是从对象定义而来还是具体类型而来了。
所以我在想除非在传入的参数中必须包含它具体定义类型的信息。
如:
    定义的是int i = 333;那么加上类型信息int.class
    定义的是Integer I = 333,那么加上类型信息Integer.class.
这样是比较麻烦一点。
当调用的函数包含有基本类型的包装类时,那么必须传入参数的具体类型,否则不需要。因为传入包装类的情况比较少,当然也可以判断当传入的是基本类型是必须加上类型信息。根据上面的想法修改后的程序如下:
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;
		else
			clazz = 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);
		else
			parameterTypes = 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);
	}
}

1 楼 aimer311 2008-10-09  
按照以上的程序并不能调用print(Integer i).当然,我知道通过
Method method = clazz.getDeclaredMethod("print", int.class);
Method method = clazz.getDeclaredMethod("print", Integer.class);可以区分这两个函数,这样相当于实现知道参数类型。而如果从所输入的参数来判断是什么类型,再判断调用哪个函数就不清楚了。如:
当obj[0] = 333,表明此时的参数是int型,那么调用的应该是print(int i);
当obj[0] = new Integer(333),表明此时的参数是Integer型,那么调用的应该是print(Integer i);


相关推荐

Global site tag (gtag.js) - Google Analytics