KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > hotspot > Sample1


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 Sample1 extends HotSpotAdvice {
9     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
10         String JavaDoc className = "samples.Foo";
11         if (args.length > 0) {
12             className = args[0];
13         }
14
15         new Sample1(className);
16     }
17
18
19     public Sample1(String JavaDoc className) throws Exception JavaDoc {
20         // Configure:
21
InstrumentationContext ctx = new InstrumentationContext();
22         InstrumentationDescriptor id = new InstrumentationDescriptor();
23         id.addInclusionRule("samples.*");
24         ctx.addInstrumentationDescriptor(id);
25         
26         HotSpotAdvisor hsi =
27             new HotSpotAdvisor(id, this, HotSpotAdvisor.INVOCATIONS);
28
29         // Launch samples.Foo
30
Bootstrapper.launch(className, null, ctx,
31                             InstrumentingClassLoader.createClassLoader(ctx));
32     }
33
34
35     /**
36      * This method is the beef of this sample.
37      * Each hot-spot (INVOCATIONS, @see above) is replaced
38      * with content of this method.
39      * Lines <i>before</i> a call to <i>doHotSpot()</i> are inserted before
40      * invocation, and lines <i>after</i> a call to <i>doHotSpot()</i>
41      * are inserted after invocation
42      */

43     public void advice() {
44         System.out.println("Calling a method: " + getHotSpotName());
45         doHotSpot();
46         System.out.println("Returned from method: " + getHotSpotName());
47     }
48 }
49
Popular Tags