zoukankan      html  css  js  c++  java
  • 动态代理和cglib代理(精简小例子)

    1.

    @RunWith(AndroidJUnit4.class)
    public class ExampleInstrumentedTest {
        final String TAG = "GenericTest";
    
        //测试通过反射获取到类(class)的方法(method)修饰符(public、private等等)对应的整数值
        @Test(timeout = 100)
        public void testModifierFinalNum() {
    
            try {
                Log.e(TAG, "try: ");
                Class<?> clazz = Class.forName("com.example.androidstudydemo.ExampleInstrumentedTest");//当前类路径
                Method[] declaredMethods = clazz.getDeclaredMethods();
                for (Method method : declaredMethods) {
                    Log.e(TAG, method.getName());
                    int modifier = method.getModifiers(); //每个修饰符对应一个整数值,返回的数值是多个修饰符对应数值之和
                    Log.e(TAG, method.getName() + "---" + modifier);
    
                    Log.e(TAG, String.valueOf(method.getReturnType().equals(void.class)));
                    Log.e(TAG + '1', String.valueOf(method.getReturnType() == int.class));
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        //fun1()  fun2(),用于验证反射获取其修饰符的值
        private static int fun1() { return 1; }
        public static void fun2() {}
    
        @Test
        public void testProxy() {
            BuyHouse buyHouse = new BuyHouseImpl();
            BuyHouse proxyBuyHouse = (BuyHouse) Proxy.newProxyInstance(BuyHouse.class.getClassLoader(), new
                    Class[]{BuyHouse.class}, new DynamicProxyHandler(buyHouse));
            proxyBuyHouse.buyHosue();
        }
    
        class DynamicProxyHandler implements InvocationHandler {
            String TAG ="BuyHouseImpl";
    
            private Object object;
    
            public DynamicProxyHandler(Object object) {
                this.object = object;
            }
    
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Log.e(TAG, "买房前");
                Object result = method.invoke(object, args);
                Log.e(TAG, "买房后");
                return (Integer)(result);
            }
        }
    
    
        @Test
        public void testCglibProxy(){
    //        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "./");
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(BuyBook.class);
            enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {
                System.out.println("事务开始......" + method.getName());
                Object o1 = proxy.invokeSuper(obj, args);
                System.out.println("事务结束......." + method.getName());
                return o1;
            });
            BuyBook buyBook = (BuyBook) enhancer.create();
            buyBook.buy();
        }
    
    }
    
    
    
    
    
    interface BuyHouse {
        int buyHosue();
    }
    
    class BuyHouseImpl implements BuyHouse {
        String TAG = "BuyHouseImpl";
        @Override
        public int buyHosue() {
            Log.e(TAG, "买房中");
            return 200;
        }
    }
  • 相关阅读:
    工厂模式一
    面向对象的简单理解二
    工厂模式三
    线程的简单学习
    nyoj35 表达式求值
    nyoj305 表达式求值
    poj1298 The Hardest Problem Ever
    poj1363 Rails
    hdu2036 改革春风吹满地
    nyoj467 中缀式变后缀式
  • 原文地址:https://www.cnblogs.com/sunupo/p/15498285.html
Copyright © 2011-2022 走看看