最愛Java

          書山有路勤為徑,學海無涯苦作舟

          《AspectJ Cookbook》讀書筆記十六: 增強類和編譯器

                  本章節說明了如何使用靜態橫切技術,以靜態方式使用AspectJ中的方面把行為和接口引入現有的類中。使用這些技術,可以擴展類來實現接口,從新的父類擴展類,引入新方法和屬性,減輕說發生異常的影響,以及繼承多個基類。
          一.擴展現有的類
          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類中。

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

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

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

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

          四.聲明默認的接口實現
          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語句,可以指定一組應該減輕其影響的異常--也就是說,在通過特定連接點選擇的連接點上引發這些異常時,將其轉換成未捕獲的異常。
              示例中說明了減輕在void foo()方法上引發的ExcepionA異常的影響,使得該方法的用戶不必關心如何處理這個異常。
          package com.aspectj;

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

          六.擴展編譯
          分別使用declare error或declare warning語句,指定應該引發編譯器錯誤或警告的條件。
          示例說明了如何聲明一個新的錯誤和警告,如果在正在編譯的應用程序內發現指定的條件,編譯器就會引發該錯誤或警告。
          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 閱讀(469) 評論(0)  編輯  收藏 所屬分類: 《AspectJ Cookbook》讀書筆記

          公告


          導航

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

          統計

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 秦皇岛市| 鹿泉市| 林芝县| 托里县| 黄石市| 鄂伦春自治旗| 英超| 缙云县| 湘潭市| 北宁市| 曲周县| 伊吾县| 定西市| 丰顺县| 冕宁县| 永定县| 北京市| 全州县| 仪征市| 青铜峡市| 宜昌市| 牟定县| 和顺县| 荥经县| 紫阳县| 澜沧| 赞皇县| 扎兰屯市| 亳州市| 舒城县| 盐城市| 济阳县| 贞丰县| 安阳市| 民县| 白山市| 凤翔县| 犍为县| 当涂县| 佳木斯市| 洛扎县|