FragmentAtClassScope 範例

這個範例示範如何使用 FragmentAtClassScope 探針片段。

在這個範例中,探針會計算套件中所有類別曾經建立過的實例數(符合 "com.sample*" 萬用字元型樣),並且每一百個實例建立時會提出報告。

「類別範圍的片段」宣告一個計數器,此計數器會在 entry 片段要編譯到的類別中,顯示為 int 類型靜態欄位。片段還有宣告一個 fragment 類別的靜態起始設定程式,用來報告已載入探針。請注意,fragmentAtClassScope 區段中的項目會建置到產生的探針類別中,而不是探針套用的類別。如果要將靜態欄位或靜態起始設定程式邏輯插入到探針套用的類別中,請使用 staticFieldstaticInitializer 物件。

如果要使用這個程式碼範例,請變更目標物件中的 package="com.sample*" 以參照實際的套件名稱。

請注意,如果要在 XML 中使用 &< 字元,您必須指定字元實體 &amp;&lt;,如範例所示。

<?xml version="1.0" encoding="ASCII"?>
<probekit>
   <probe>
      <fragmentAtClassScope>
         static public int entry_counter = 0;
         static int counter = 0;
         static { System.out.println("[fragmentAtClassScope sample probe has loaded.]"); }
      </fragmentAtClassScope>
      <target type="include" package="com.sample*" method="&lt;init>" />
      <target type="exclude" package="*" />
      <fragment type="entry"> 
         <code>
            ++counter;
            if (counter % 100 == 0)
              System.out.println("[" + counter + " instances of classes in com.sample package so far]");
         </code>
      </fragment>
   </probe>
</probekit>

上層主題: Probekit 範例

相關參考
FragmentAtClassScope 探針物件

Terms of use | Feedback
(C) Copyright IBM Corporation 2000, 2004. All Rights Reserved.