zoukankan      html  css  js  c++  java
  • 代码篇之代理

    public class ProxyTest {
    
        public static void main(String[] args) throws Exception{
            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 sBuilder = new StringBuilder(name);
                sBuilder.append('(');
                Class[] clazzParams = constructor.getParameterTypes();
                for(Class clazzParam : clazzParams){
                    sBuilder.append(clazzParam.getName()).append(',');
                }
                if(clazzParams!=null && clazzParams.length != 0)
                    sBuilder.deleteCharAt(sBuilder.length()-1);
                sBuilder.append(')');
                System.out.println(sBuilder.toString());            
            }
    
            System.out.println("----------begin methods list----------");
            Method[] methods = clazzProxy1.getMethods();
            for(Method method : methods){
                String name = method.getName();
                StringBuilder sBuilder = new StringBuilder(name);
                sBuilder.append('(');
                Class[] clazzParams = method.getParameterTypes();
                for(Class clazzParam : clazzParams){
                    sBuilder.append(clazzParam.getName()).append(',');
                }
                if(clazzParams!=null && clazzParams.length != 0)
                    sBuilder.deleteCharAt(sBuilder.length()-1);
                sBuilder.append(')');
                System.out.println(sBuilder.toString());            
            }
            
            System.out.println("----------begin create instance object----------");
            Constructor constructor = clazzProxy1.getConstructor(InvocationHandler.class);
            class MyInvocationHander1 implements InvocationHandler{
                public Object invoke(Object proxy, Method method, Object[] args)
                        throws Throwable {
                    return null;
                }
            
            }
            Collection proxy1 = (Collection)constructor.newInstance(new MyInvocationHander1());
            
            System.out.println(proxy1);
            proxy1.clear();
            
            Collection proxy2 = (Collection)constructor.newInstance(new InvocationHandler(){
                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("zxx");
            proxy3.add("lhm");
            proxy3.add("bxd");
            System.out.println(proxy3.size());
            System.out.println(proxy3.getClass().getName());
        }
    
        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;*/
                            advice.beforeMethod(method);
                            Object retVal = method.invoke(target, args);
                            advice.afterMethod(method);
                            return retVal;                        
                            
                        }
                    }
                    );
            return proxy3;
        }
    
    }

    Advice类

    public interface Advice {
        void beforeMethod(Method method);
        void afterMethod(Method method);
    }

    MyAdvice类

    public class MyAdvice implements Advice {
        long beginTime = 0;
        public void afterMethod(Method method) {
            // TODO Auto-generated method stub
            System.out.println("从传智播客毕业上班啦!");        
            long endTime = System.currentTimeMillis();
            System.out.println(method.getName() + " running time of " + (endTime - beginTime));
    
        }
    
        public void beforeMethod(Method method) {
            // TODO Auto-generated method stub
            System.out.println("到传智播客来学习啦!");
            beginTime = System.currentTimeMillis();
        }
    
    }
  • 相关阅读:
    第一周学习总结
    《淘宝网》质量属性分析
    软件架构师如何工作
    MVC模式
    Python学习
    echarts-数据的视觉映射
    热词展示
    热词关系图
    offset、client、scroll三个系列对比,是否有边框 padding?是否有单位?
    元素滚动 scroll系列属性 如果内容溢出盒子、scroll返回的是内容的大小 是否带边框、单位?
  • 原文地址:https://www.cnblogs.com/atomicbomb/p/6624455.html
Copyright © 2011-2022 走看看