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:
- 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();.
- In the staticInitializer fragment, the probe sets the Date instance
of a probed class to "time zero" (January, 1970) when it is loaded.
- 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.)
- 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 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):