Cglib FastClass例子

image.png

DemoService 原始類
DemoService$$FastClassByCGLIB$$3215ecb4 原始類的FastClass

DemoService$$EnhancerByCGLIB$$c23dc77c DemoService的代理類
DemoService$$EnhancerByCGLIB$$c23dc77c$$FastClassByCGLIB$$396d31d2 代理類的FastClass

   static void CGLIB$STATICHOOK1() {
        CGLIB$THREAD_CALLBACKS = new ThreadLocal();
        CGLIB$emptyArgs = new Object[0];

        // 生成的代理類
        Class class_ = Class.forName("com.zhiyin.cglib.cando.DemoService$$EnhancerByCGLIB$$c23dc77c");
        Class class_2 = Class.forName("java.lang.Object");
        Method[] arrmethod = ReflectUtils.findMethods((String[])new String[]{"equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, (Method[])class_2.getDeclaredMethods());
        CGLIB$equals$1$Method = arrmethod[0];
        CGLIB$equals$1$Proxy = MethodProxy.create(class_2, class_, (String)"(Ljava/lang/Object;)Z", (String)"equals", (String)"CGLIB$equals$1");
        CGLIB$toString$2$Method = arrmethod[1];
        CGLIB$toString$2$Proxy = MethodProxy.create(class_2, class_, (String)"()Ljava/lang/String;", (String)"toString", (String)"CGLIB$toString$2");
        CGLIB$hashCode$3$Method = arrmethod[2];
        CGLIB$hashCode$3$Proxy = MethodProxy.create(class_2, class_, (String)"()I", (String)"hashCode", (String)"CGLIB$hashCode$3");
        CGLIB$clone$4$Method = arrmethod[3];
        CGLIB$clone$4$Proxy = MethodProxy.create(class_2, class_, (String)"()Ljava/lang/Object;", (String)"clone", (String)"CGLIB$clone$4");

        // 原始類
        class_2 = Class.forName("com.zhiyin.cglib.cando.DemoService");

        
        CGLIB$hello$0$Method = ReflectUtils.findMethods((String[])new String[]{"hello", "(Ljava/lang/String;)Ljava/lang/String;"}, (Method[])class_2.getDeclaredMethods())[0];
        // hello方法代理 參數(shù)分別為 原始類 代理類 參數(shù)與返回值描述 方法名 代理方法名
        CGLIB$hello$0$Proxy = MethodProxy.create(class_2, class_, (String)"(Ljava/lang/String;)Ljava/lang/String;", (String)"hello", (String)"CGLIB$hello$0");
    }

// 真正的服務方法
    final String CGLIB$hello$0(String string) {
        return super.hello(string);
    }

// 代理方法
    public final String hello(String string) {
        MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
        if (methodInterceptor == null) {
            DemoService$$EnhancerByCGLIB$$c23dc77c.CGLIB$BIND_CALLBACKS((Object)this);
            methodInterceptor = this.CGLIB$CALLBACK_0;
        }
        if (methodInterceptor != null) {
            return (String)methodInterceptor.intercept((Object)this, CGLIB$hello$0$Method, new Object[]{string}, CGLIB$hello$0$Proxy);
        }
        return super.hello(string);
    }

參數(shù)見上面說明

  public static MethodProxy create(Class c1, Class c2, String desc, String name1, String name2) {
        MethodProxy proxy = new MethodProxy();
        proxy.sig1 = new Signature(name1, desc);
        proxy.sig2 = new Signature(name2, desc);
        proxy.createInfo = new CreateInfo(c1, c2);
        return proxy;
    }
    private void init()
    {
        /* 
         * Using a volatile invariant allows us to initialize the FastClass and
         * method index pairs atomically.
         * 
         * Double-checked locking is safe with volatile in Java 5.  Before 1.5 this 
         * code could allow fastClassInfo to be instantiated more than once, which
         * appears to be benign.
         */
        if (fastClassInfo == null)
        {
            synchronized (initLock)
            {
                if (fastClassInfo == null)
                {
                    CreateInfo ci = createInfo;

                    FastClassInfo fci = new FastClassInfo();
                    // ci.c1是原始類DemoService
                    fci.f1 = helper(ci, ci.c1);
                    // ci.c2是代理類
                    fci.f2 = helper(ci, ci.c2);
                    // hello
                    fci.i1 = fci.f1.getIndex(sig1);
                    // CGLIB$hello$0
                    fci.i2 = fci.f2.getIndex(sig2);
                    fastClassInfo = fci;
                    createInfo = null;
                }
            }
        }
    }

調(diào)用這里調(diào)用的是 代理類的代理方法

  public Object invokeSuper(Object obj, Object[] args) throws Throwable {
        try {
            // 初始化fastClassInfo
            init();
            FastClassInfo fci = fastClassInfo;
            // 調(diào)用代理類的CGLIB$hello$0方法
            return fci.f2.invoke(fci.i2, obj, args);
        } catch (InvocationTargetException e) {
            throw e.getTargetException();
        }
    }
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市充尉,隨后出現(xiàn)的幾起案子恃锉,更是在濱河造成了極大的恐慌总寻,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,640評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件彰阴,死亡現(xiàn)場離奇詭異辐益,居然都是意外死亡偷崩,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評論 3 395
  • 文/潘曉璐 我一進店門帚呼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來掏缎,“玉大人,你說我怎么就攤上這事煤杀【祢冢” “怎么了?”我有些...
    開封第一講書人閱讀 165,011評論 0 355
  • 文/不壞的土叔 我叫張陵沈自,是天一觀的道長酌儒。 經(jīng)常有香客問我,道長枯途,這世上最難降的妖魔是什么忌怎? 我笑而不...
    開封第一講書人閱讀 58,755評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮酪夷,結果婚禮上榴啸,老公的妹妹穿的比我還像新娘。我一直安慰自己晚岭,他們只是感情好鸥印,可當我...
    茶點故事閱讀 67,774評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般库说。 火紅的嫁衣襯著肌膚如雪狂鞋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,610評論 1 305
  • 那天璃弄,我揣著相機與錄音要销,去河邊找鬼。 笑死夏块,一個胖子當著我的面吹牛疏咐,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播脐供,決...
    沈念sama閱讀 40,352評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼浑塞,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了政己?” 一聲冷哼從身側響起酌壕,我...
    開封第一講書人閱讀 39,257評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎歇由,沒想到半個月后卵牍,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,717評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡沦泌,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,894評論 3 336
  • 正文 我和宋清朗相戀三年糊昙,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片谢谦。...
    茶點故事閱讀 40,021評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡释牺,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出回挽,到底是詐尸還是另有隱情没咙,我是刑警寧澤,帶...
    沈念sama閱讀 35,735評論 5 346
  • 正文 年R本政府宣布千劈,位于F島的核電站祭刚,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏墙牌。R本人自食惡果不足惜袁梗,卻給世界環(huán)境...
    茶點故事閱讀 41,354評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望憔古。 院中可真熱鬧遮怜,春花似錦、人聲如沸鸿市。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至陌凳,卻和暖如春剥懒,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背合敦。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評論 1 270
  • 我被黑心中介騙來泰國打工初橘, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人充岛。 一個月前我還...
    沈念sama閱讀 48,224評論 3 371
  • 正文 我出身青樓保檐,卻偏偏與公主長得像,于是被迫代替她去往敵國和親崔梗。 傳聞我的和親對象是個殘疾皇子夜只,可洞房花燭夜當晚...
    茶點故事閱讀 44,974評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn)蒜魄,斷路器扔亥,智...
    卡卡羅2017閱讀 134,658評論 18 139
  • 轉(zhuǎn)至元數(shù)據(jù)結尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 1,718評論 0 9
  • 1. Java基礎部分 基礎部分的順序:基本語法谈为,類相關的語法旅挤,內(nèi)部類的語法,繼承相關的語法伞鲫,異常的語法粘茄,線程的語...
    子非魚_t_閱讀 31,639評論 18 399
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,159評論 25 707
  • 一 最近在看一些有關影視編劇的教材,有一句希區(qū)柯克的名言印象深刻——“電影就是將平淡無奇的片段切去去以后的人生”榔昔。...
    逐日free閱讀 341評論 0 0