• 动态代理和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;
        }
    }
  • 相关阅读:
    nodeJS + webStrome
    js获取浏览器窗口属性
    理解JavaScript中的arguments,callee,caller,apply
    Java对象及对象引用变量
    Transform-style和Perspective属性
    线程同步的几种方式
    SQL高级查询——50句查询(含答案)
    接口、抽象类的区别与共同点
    JAVA异常架构图及常见面试题
    【转】JVM介绍
  • 原文地址:https://www.cnblogs.com/sunupo/p/15498285.html
Copyright © 2020-2023  润新知