package itheima;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.*;
/**
* 写一个ArrayList类的代理,实现和ArrayList中完全相同的功能,并可以计算每个方法运行的时间。
* */
public class Test3{
public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
Class clazzProxy1=Proxy.getProxyClass(Collection.class.getClassLoader(),Collection.class);
System.out.println(clazzProxy1.getName());
System.out.println("-----------begin constructors list---------");
Constructor[] constructors=clazzProxy1.getConstructors();
for(Constructor constructor:constructors)
{
String name=constructor.getName();
StringBuilder sb=new StringBuilder(name);
sb.append('(');
Class[] clazzPrams=constructor.getParameterTypes();
for(Class clazzPram:clazzPrams)
{
sb.append(clazzPram.getName());
}
if(clazzPrams!=null && clazzPrams.length!=0)
sb.deleteCharAt(sb.length()-1);
sb.append(')');
System.out.println(sb.toString());
}
System.out.println("-----------begin methodss list---------");
Method[] methods=clazzProxy1.getMethods();
for(Method method:methods)
{
String name=method.getName();
StringBuilder sb=new StringBuilder(name);
sb.append('(');
Class[] clazzPrams=method.getParameterTypes();
for(Class clazzPram:clazzPrams)
{
sb.append(clazzPram.getName());
}
if(clazzPrams!=null && clazzPrams.length!=0)
sb.deleteCharAt(sb.length()-1);
sb.append(')');
System.out.println(sb.toString());
}
System.out.println("-----------begin create instance Object---------");
//创建对象
Constructor constructor=clazzProxy1.getConstructor(InvocationHandler.class);
//第一种方式
class MyInvocationHandler1 implements InvocationHandler{
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
return null;
}
} //Collection对象
Collection proxy1=(Collection)constructor.newInstance(new MyInvocationHandler1());
Constructor connstructor;
//第二种方式
Collection Proxy2=(Collection)constructor.newInstance(new InvocationHandler(){
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
return null;
}
});
//第三种一步到位 有字节码也有实例对象
final ArrayList target=new ArrayList();
Collection proxy3=(Collection)getProxy(target,new MyAdvice());
proxy3.add("zxx");
}
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 beginTime=System.currentTimeMillis();
Object retVal=method.invoke(target, args);
long endTime=System.currentTimeMillis();
System.out.println(method.getName()+"Running time of"+(endTime-beginTime));
return retVal;*/
//long beginTime=System.currentTimeMillis();
advice.beforeMethod(method);
Object retVal=method.invoke(target, args);
advice.afterMethod(method);
//long endTime=System.currentTimeMillis();
//System.out.println(method.getName()+"Running time of"+(endTime-beginTime));
return retVal;
}
}
);
return Proxy3;
}
}
:victory: |
|