最愛Java

          書山有路勤為徑,學(xué)海無涯苦作舟

          《AspectJ Cookbook》讀書筆記十六: 增強(qiáng)類和編譯器

                  本章節(jié)說明了如何使用靜態(tài)橫切技術(shù),以靜態(tài)方式使用AspectJ中的方面把行為和接口引入現(xiàn)有的類中。使用這些技術(shù),可以擴(kuò)展類來實現(xiàn)接口,從新的父類擴(kuò)展類,引入新方法和屬性,減輕說發(fā)生異常的影響,以及繼承多個基類。
          一.擴(kuò)展現(xiàn)有的類
          package com.aspectj;

          public aspect ExtendClassRecipe {
              
          private int MyClass.newVariable = 20;
              
              
          public int MyClass.bar(String name) 
                  System.out.println(
          "In bar(String name) , name:" + name);
                  
          return this.newVariable; 
              }

          }

           
                  示例中將屬性newVariable和方法bar(String)添加到了MyClass類中。

          二.聲明類之間的繼承關(guān)系
              使用declare parents語句,指定特定的類是從另一個類擴(kuò)展而來。
              以下代碼說明了如何為MyClass類指定新的繼承關(guān)系
          package com.aspectj;

          public aspect IntroduceInheritanceRecipe {
              declare parents:MyClass 
          extends AnotherClass;
          }

          三.使用方面實現(xiàn)接口
              使用declare parents語句,指定特定的類實現(xiàn)特定的接口。
          package com.aspectj;

          public aspect ImplementInterfaceRecipe {
              declare parents:MyClass 
          implements MyInterface;
          }
              把接口應(yīng)用于現(xiàn)有類的能力允許通過接口類型的引用那個類的對象,如:
          //Create an instance of MyClass
          MyInterface myObject = new MyClass();
          //
          //Work with the interface reference
          myObject.foo(1,"Russ");

          四.聲明默認(rèn)的接口實現(xiàn)
          package com.aspectj;

          public aspect DefaultInterfaceImplementationRecipe {
              declare parents:MyClass 
          implements MyInterface;

              
          //public void MyInterface.bar(String name) {
                  
          //System.out.println("bar(String) called on " + this);
              
          //}
          }

          五.減輕異常的影響
              使用declare soft語句,可以指定一組應(yīng)該減輕其影響的異常--也就是說,在通過特定連接點選擇的連接點上引發(fā)這些異常時,將其轉(zhuǎn)換成未捕獲的異常。
              示例中說明了減輕在void foo()方法上引發(fā)的ExcepionA異常的影響,使得該方法的用戶不必關(guān)心如何處理這個異常。
          package com.aspectj;

          public aspect SoftExceptionRecipe {
              pointcut callPointCut() : call(
          void MyClass.foo());
              declare soft : ExceptionA : callPointCut();
          }

          六.擴(kuò)展編譯
          分別使用declare error或declare warning語句,指定應(yīng)該引發(fā)編譯器錯誤或警告的條件。
          示例說明了如何聲明一個新的錯誤和警告,如果在正在編譯的應(yīng)用程序內(nèi)發(fā)現(xiàn)指定的條件,編譯器就會引發(fā)該錯誤或警告。
          package com.aspectj;

          public aspect CompilaionAdviceRecipe {
              declare error:call(
          void ProtectedAccessClass.setValue(int))  : "Must only set the ProtectedAccessClass.value from a MyClass object";
              
              declare warning:call(
          void ProtectedAccessClass.getValue()) :"Should only be reading ProtectedAccessClass.value from a MyClass object";
          }

          posted on 2008-08-27 09:31 Brian 閱讀(475) 評論(0)  編輯  收藏 所屬分類: 《AspectJ Cookbook》讀書筆記

          公告


          導(dǎo)航

          <2008年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          統(tǒng)計

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 交口县| 白银市| 鲁甸县| 衡南县| 夏津县| 泗洪县| 灵川县| 昔阳县| 德州市| 卓尼县| 徐汇区| 临高县| 长兴县| 富顺县| 河津市| 淮南市| 双鸭山市| 博白县| 盈江县| 渝中区| 和林格尔县| 舞阳县| 镇平县| 苏州市| 遂川县| 缙云县| 浦东新区| 平谷区| 万源市| 绥滨县| 彰化县| 澄迈县| 瑞安市| 崇文区| 龙口市| 会宁县| 寿阳县| 东山县| 石渠县| 迭部县| 尚志市|