package cn.com;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
public class ProxyTest {
public static void main(String args[]) throws Exception{
Class clazzProxy = Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);
System.out.println(clazzProxy.getName());
System.out.println("-----begin constuctor------");
Constructor[] constraints = clazzProxy.getConstructors();
for(Constructor constructor : constraints){
String name = constructor.getName();
StringBuffer sBuffer = new StringBuffer(name);
sBuffer.append("(");
Class[] classParams = constructor.getParameterTypes();
for(Class classparam : classParams){
sBuffer.append(classparam.getName()+" "+
classparam.getName().substring(classparam.getName().lastIndexOf(".")+1,
classparam.getName().length()).toString().toLowerCase()).append(',');
}
if(classParams.length>0){
sBuffer.deleteCharAt(sBuffer.length()-1);
}
sBuffer.append(")");
System.out.println(sBuffer);
}
System.out.println("-----begin method------");
Method[] methods = clazzProxy.getMethods();
for(Method method : methods){
String name = method.getName();
StringBuffer sBuffer = new StringBuffer(name);
sBuffer.append("(");
Class[] classParams = method.getParameterTypes();
for(Class classparam : classParams){
sBuffer.append(classparam.getName()+" "+
classparam.getName().substring(classparam.getName().lastIndexOf(".")+1,
classparam.getName().length()).toString().toLowerCase()).append(',');
}
if(classParams.length>0){
sBuffer.deleteCharAt(sBuffer.length()-1);
}
sBuffer.append(")");
System.out.println(sBuffer);
}
System.out.println("-----begin create instance------");
Constructor constructor = clazzProxy.getConstructor(InvocationHandler.class);
class MyInvocationHander implements InvocationHandler{
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
return null;
}
}
Collection proxy1 = (Collection)constructor.newInstance(new MyInvocationHander());
proxy1.clear();
Collection proxy2 = (Collection)constructor.newInstance(new InvocationHandler(){
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
return null;
}
});
final ArrayList target = new ArrayList();
Collection proxy3 = (Collection) getProxy(target,new MyAdvice());
proxy3.add("abc");
proxy3.add("xyz");
System.out.println(proxy3.size());
}
private static Object getProxy(final Object target,final Advice advice) {
Object proxy3 = Proxy.newProxyInstance(
target.getClass().getClassLoader(),
/* new Class[]{Collection.class},*/
target.getClass().getInterfaces(),
new InvocationHandler(){
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
/*long starttime = System.currentTimeMillis();
Object retVal = method.invoke(target, args);
long endtime = System.currentTimeMillis();
System.out.println(method + " 发费时间:" + (endtime - starttime));
return retVal;*/
advice.beforeMethod();
Object retVal = method.invoke(target, args);
advice.afterMethod(method);
return retVal;
}
});
return proxy3;
}
}