A staticField and staticInitializer probe example

This example demonstrates the use of the staticField data item and the staticInitializer probe fragment.

In the example, the probe keeps track of when instances of a class are created. If an instance is created more than one second after the previous instance of that class, the probe writes a log message.

Here is what this probe does:
  1. Using the static field definition, the probe creates a static field of type Date in every probed class. The static field will be initialized by calling new Date();.
  2. In the staticInitializer fragment, the probe sets the Date instance of a probed class to "time zero" (January, 1970) when it is loaded.
  3. In the entry fragment, the probe checks to see when the previous update was performed, and issues a report if the update was performed more than a second ago. (Because of the target rules, the entry fragment runs only in constructors.)
  4. Finally, the probe updates the value of the Date instance to "now."

The probe uses the static field definition to create a new static field in every probed class. By comparison, using a fragment at class scope to declare the Date field would result in a single instance of Date appearing in the generated class that holds the probe fragments, no matter how many classes the probe is applied to. You can do this if you want to track the time delay between creation of instances of any probed class, instead of tracking the delay between the creation of instances of each probed class.

This probe is defined by the following entries in the Probekit editor:
  • Fully qualified Java type for static field (right-click Probe in the tree pane, then click New > Static Field to enter this value):

    java.util.Date

  • 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>
    • Method Signature: *
  • Target (to enter these values, click Add, then Edit in the edit pane):
    • Target Type: exclude
    • Package: *
    • Class: *
    • Method: *
    • Method Signature: *
  • Fragment (select Fragment in the tree pane to enter these values):
    • Fragment Type: entry
    • Data Items:
      • Data Type: staticField, Name: lastInstanceDate
      • Data Type: className, Name: clname
    • Java Code:

      java.util.Date d = new java.util.Date();
      long now = d.getTime();
      long prev = lastInstanceDate.getTime();
      if (prev != 0 && prev + 1000 < now) {
        System.out.println("[" + clname + " instance after > 1 second]");
      }
      lastInstanceDate.setTime(now);

    • Fragment (right-click Probe and select New > Fragment to enter these values):
      • Fragment Type: staticInitializer
      • Data Items:
        • Data Type: staticField, Name: lastInstanceDate
        • Data Type: className, Name: clname
      • Java Code:

                 lastInstanceDate.setTime(0);
                 System.out.println("[" + clname + " class loaded]");