A fragment at class scope probe example

This example demonstrates how to use the fragment at class scope in a probe.

In the 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 that 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 to report that the probe has been loaded. Note that the items in the fragment at class scope are built into the generated probe class, not into the classes that the probe applies to. To insert static fields or static initializer logic into classes that a probe applies to, use probe fragments of the type staticInitializer, or define a static field.

This probe is defined by the following entries in the Probekit editor:
  • Java code for Fragment At Class Scope (select Probe in the tree pane to enter this value):

    static public int entry_counter = 0;
    static int counter = 0;
    static { System.out.println("[fragmentAtClassScope sample probe has loaded.]"); }

  • Target (to enter these values, select Target in the tree pane, then click Edit):
    • Target Type: include
    • Package: com.sample* (Use an actual package name here.)
    • Class: *
    • Method: <init>
    • Signature: *
  • Target (to enter these values, click Add, then Edit in the edit pane):
    • Target Type: exclude
    • Package: *
    • Class: *
    • Method: *
    • Signature: *
  • Fragment (select Fragment in the tree pane to enter these values):
    • Fragment Type: entry
    • Java Code:

      ++counter;
      if (counter % 100 == 0)
        System.out.println("[" + counter + " instances of classes in com.sample package so far]");