FragmentAtClassScope 示例

此示例演示如何使用 FragmentAtClassScope 探针段。

在此示例中,探针对与“com.sample*”通配符模式相匹配的包中的所有类创建过的实例数进行计数,并且每创建 100 个实例就报告一次。

“类作用域中的段”声明一个计数器,该计数器作为 entry 段编译成的类中整型静态字段出现。该段还对报告已装入探针的 fragment 类声明静态初始化方法。注意,fragmentAtClassScope 部分中的项被构建到生成的探针类而不是探针所应用的类中。要将静态字段或静态初始化方法逻辑插入到探针所应用的类中,使用 staticFieldstaticInitializer 对象。

要使用此示例代码,更改 target 对象中的 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 探针对象

Copyright IBM Corporation and others 2000, 2004.