最愛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 閱讀(475) 評論(0)  編輯  收藏 所屬分類: 《AspectJ Cookbook》讀書筆記

          公告


          導航

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

          統計

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 本溪市| 湖南省| 绩溪县| 阿克| 油尖旺区| 济宁市| 永寿县| 大冶市| 高台县| 塔河县| 孟州市| 闻喜县| 南靖县| 儋州市| 仪陇县| 揭阳市| 彭泽县| 太仆寺旗| 阳泉市| 新平| 小金县| 庆云县| 和平区| 山阳县| 梁山县| 高尔夫| 永定县| 乌鲁木齐市| 柳河县| 达孜县| 阿巴嘎旗| 瓮安县| 涿州市| 信宜市| 屏东县| 辉南县| 西丰县| 通江县| 开江县| 仪陇县| 湖北省|