FragmentAtClassScope example

This example demonstrates how to use the FragmentAtClassScope probe fragment.

In this example, a probe counts the number of instances ever created of all classes in packages matching the "com.sample*" wildcard pattern, and reports every hundredth instance creation.

The "fragment at class scope" declares a counter which appears as an int-type static field in the class that the entry fragment is compiled into. The fragment also declares a static initializer for the fragment class which reports that the probe has loaded. Note that the items in the fragmentAtClassScope section are built into the generated probe class, not the classes that the probe applies to. To insert static fields or static initializer logic into classes that a probe applies to, use the staticField and staticInitializer objects.

To use this example code, change package="com.sample*" in the target object to refer to an actual package name.

Note that to use the characters & and < in XML, you must specify the character entities &amp; and &lt;, as shown in the example.

<?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>

Parent topic: Probekit Examples

Related reference
The FragmentAtClassScope probe object

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