17

package com.haizhitao.dynamicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.Vector;

public class VectorProxy implements InvocationHandler
{
	private Object proxyObj;
	
	public VectorProxy(Object obj)
	{
		this.proxyObj = obj;
	}
	
	public static Object factory(Object obj)
	{
		Class<?> classType = obj.getClass();
		
		return Proxy.newProxyInstance(classType.getClassLoader(), 
				classType.getInterfaces(), new VectorProxy(obj));
	}
	
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
	{
		System.out.println("before calling: " + method);
		
		if(null != args)
		{
			for(Object obj : args)
			{
				System.out.println(obj);
			}
		}
		
		Object object = method.invoke(proxyObj, args);
		
		System.out.println("after calling: " + method);
		
		return object;
	}
	
	public static void main(String[] args)
	{
		List v = (List)factory(new Vector());
		
		System.out.println(v.getClass().getName());
		
		v.add("New");
		v.add("York");
		
		System.out.println(v);
		
		v.remove(0);
		System.out.println(v);
	}
}

//output:
//	$Proxy0
//	before calling: public abstract boolean java.util.List.add(java.lang.Object)
//	New
//	after calling: public abstract boolean java.util.List.add(java.lang.Object)
//	before calling: public abstract boolean java.util.List.add(java.lang.Object)
//	York
//	after calling: public abstract boolean java.util.List.add(java.lang.Object)
//	before calling: public java.lang.String java.lang.Object.toString()
//	after calling: public java.lang.String java.lang.Object.toString()
//	[New, York]
//	before calling: public abstract java.lang.Object java.util.List.remove(int)
//	0
//	after calling: public abstract java.lang.Object java.util.List.remove(int)
//	before calling: public java.lang.String java.lang.Object.toString()
//	after calling: public java.lang.String java.lang.Object.toString()
//	[York]

阅读更多
博主设置当前文章不允许评论。

没有更多推荐了,返回首页

关闭
关闭
关闭