package com.test;
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 targetObj;
public Object getTargetObj(){
return targetObj;
}
public VectorProxy(Object targetObj){
this.targetObj = targetObj;
}
public static Object factory(Object obj){
Class clazz = obj.getClass();
Object proxy = Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), new VectorProxy(obj));
return proxy;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("before invoke"+method.getName());
if(null!=args){
for(Object obj:args){
System.out.println(obj+":ad");
}
}
Object tarObj =((VectorProxy)((Proxy.getInvocationHandler(proxy)))).getTargetObj();
System.out.println(tarObj==targetObj);
Object resultObj = method.invoke(targetObj, args);
System.out.println("after invoke");
return resultObj;
}
@SuppressWarnings({ "unchecked",})
public static void main(String[] args) {
List v =null;
v= (List)factory(new Vector(10));
v.add("new");
v.add("new5");
System.out.println("aa");
}
}