KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > hotspot > Sample2


1 package samples.hotspot;
2
3 import alt.jiapi.*;
4 import alt.jiapi.util.*;
5
6 import org.apache.log4j.Category;
7
8 public class Sample2 extends HotSpotAdvice {
9     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
10         String JavaDoc className = "samples.Foo";
11         String JavaDoc resolution = "*"; // Match all hotspots
12
if (args.length > 0) {
13             className = args[0];
14         }
15         if (args.length > 1) {
16             resolution = args[1];
17         }
18
19         new Sample2(className, resolution);
20     }
21
22
23     public Sample2(String JavaDoc className, String JavaDoc resolution) throws Exception JavaDoc {
24         // Configure:
25
InstrumentationContext ctx = new InstrumentationContext();
26         InstrumentationDescriptor id = new InstrumentationDescriptor();
27         id.addInclusionRule("samples.*");
28         ctx.addInstrumentationDescriptor(id);
29         
30         HotSpotAdvisor hsi =
31             new HotSpotAdvisor(id, this,HotSpotAdvisor.INVOCATIONS,resolution);
32
33         // Launch samples.Foo
34
Bootstrapper.launch(className, null, ctx,
35                             InstrumentingClassLoader.createClassLoader(ctx));
36     }
37
38
39     /**
40      * This method is the beef of this sample.
41      * Each hot-spot (INVOCATIONS, @see above) is replaced
42      * with content of this method.
43      * Lines <i>before</i> a call to <i>doHotSpot()</i> are inserted before
44      * invocation, and lines <i>after</i> a call to <i>doHotSpot()</i>
45      * are inserted after invocation
46      */

47     public void advice() {
48         System.out.println("Calling a method: " + getHotSpotName());
49         doHotSpot();
50         System.out.println("Returned from method: " + getHotSpotName());
51     }
52 }
53
Popular Tags