反射实例

package reflect;


import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectCase {

	private static final String PACKAGE_NAME = "reflect.Test";

	public static void main(String[] args) {
		ReflectCase r = new ReflectCase();
		Object obj1 = r.initObject(PACKAGE_NAME, null, null);

		Class[] clazzs = { int.class, String.class };
		Object[] objs = { 5, "66" };
		Object obj2 = r.initObject(PACKAGE_NAME, clazzs, objs);

		r.initMethod(obj2, "print", null, null);

		Class[] methodClazzs = { int.class, int.class };
		Object[] methodValues = { 6, 7 };
		r.initMethod(obj2, "print", methodClazzs, methodValues);

	}

	public Class loadClass(String packgeName) throws ClassNotFoundException {
		return Class.forName(packgeName);
	}

	public void initMethod(Object obj, String methodName, Class[] methodClazzs, Object[] methodVals) {
		try {
			getMethod(loadClass(PACKAGE_NAME), obj, methodName, methodClazzs, methodVals);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public Object initObject(String packgeName, Class[] clazzName,
			Object[] clazzValues) {
		try {
			return getConstructor(loadClass(packgeName), clazzName, clazzValues);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}

	
	
	private Object getConstructor(Class clazz, Class[] clsName,
			Object[] clsValues) throws SecurityException,
			NoSuchMethodException, IllegalArgumentException,
			InstantiationException, IllegalAccessException,
			InvocationTargetException {
		Object obj = null;
		if (null == clsName) {
            Constructor con = clazz.getDeclaredConstructor();
            con.setAccessible(true);
			obj = con.newInstance();
		} else {
            Constructor con = clazz.getDeclaredConstructor(clsName);
            con.setAccessible(true);
			obj = con.newInstance(clsValues);
		}
		return obj;
	}
	
	
	private void getMethod(Class clazz, Object obj, String methodName, Class[] methodClazzs, Object[] clazzValues) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		Method method = null;
		if(null == clazzValues){
            method = clazz.getDeclaredMethod(methodName);
            method.setAccessible(true);
			 method.invoke(obj);
		}else{
            method = clazz.getDeclaredMethod(methodName, methodClazzs);
            method.setAccessible(true);
			method.invoke(obj, clazzValues);
		}
	}

}

class Test {
	public Test() {
		System.out.println("Constructor Test");
	}

	public Test(int t, String a) {
		System.out.println("Constructor Test(" + t + "," + a + ")");
	}

	public void print() {
		System.out.println("Method print()");
	}

	public void print(int a, int b) {
		System.out.println("Method print("+a+","+ b+")");
	}
}


运行结果:

Constructor Test
Constructor Test(5,66)
Method print()
Method print(6,7)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值