FragmentAtClassScope 示例

本示例说明了如何使用 FragmentAtClassScope 探测片段。

在该示例中,探测计算匹配“com.sample*”通配符模式的软件包中的所有类曾经创建的实例数,并在每创建 100 个实例时进行报告。

“类作用域的片段”声明了一个计数器,在将 entry 片段编译到的类中该计数器显示为整数类型的静态字段。该片段还声明了一个 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 探测对象

使用条款 | 反馈
(C) Copyright IBM Corporation 2000, 2004. All Rights Reserved.