KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > util > HotSpotLocator


1 package alt.jiapi.util;
2
3 import java.util.ArrayList JavaDoc;
4
5 import alt.jiapi.Rule;
6 import alt.jiapi.JiapiException;
7 import alt.jiapi.reflect.Instruction;
8 import alt.jiapi.reflect.InstructionList;
9 import alt.jiapi.reflect.instruction.Opcodes;
10
11 /**
12  * HotSpotLocator locates HotSpots from InstructionList.<p>
13  *
14  * @see HotSpot definition of HotSpot
15  * @author Mika Riekkinen
16  */

17 public class HotSpotLocator {
18     private InstructionList il;
19     private byte[] opCodes;
20     private Rule rule;
21     
22     /**
23      * Create new HotSpotLocator.
24      * HotSpotLocator scans for given opCode, and when found creates
25      * a HotSpot of that Instruction, and possibly some Instructions
26      * just before it.
27      *
28      * @param il InstructionList to scan for hotspots
29      * @param opCode opCode, that triggers creation of hotspot
30      */

31     public HotSpotLocator(InstructionList il, byte opCode) {
32         this(il, new byte[] { opCode }, "*");
33     }
34
35     /**
36      * Create new HotSpotLocator.
37      * HotSpotLocator scans for any of the given opCodes, and when found
38      * creates a HotSpot of that Instruction, and possibly some Instructions
39      * just before it.
40      *
41      * @param il InstructionList to scan for hotspots
42      * @param opCodes An array of opCodes, that may trigger creation of hotspot
43      */

44     public HotSpotLocator(InstructionList il, byte[] opCodes) {
45         this(il, opCodes, "*");
46     }
47
48     /**
49      * Create new HotSpotLocator.
50      * HotSpotLocator scans for any of the given opCodes, and when found
51      * creates a HotSpot of that Instruction, and possibly some Instructions
52      * just before it.<p>
53      *
54      * HotSpot is accepted only, if resolution matches to the name of
55      * the hotspot(HotSpot.getName())
56      *
57      * @param il InstructionList to scan for hotspots
58      * @param opCodes An array of opCodes, that may trigger creation of hotspot
59      * @param resolution Resolution to use
60      */

61     public HotSpotLocator(InstructionList il, byte[] opCodes, String JavaDoc resolution) {
62 // if (il == null) {
63
// il = new InstructionList();
64
// }
65
this.il = il;
66
67
68         this.opCodes = opCodes;
69
70         try {
71             rule = new Rule(resolution);
72         }
73         catch(JiapiException je) {
74         }
75     }
76     
77
78     /**
79      * Get all the hotspots that was found. Note, that HotSpots found
80      * may overlap each other. This could happen, for example, if
81      * a return value of one invocation is a parameter to another one.
82      *
83      * @return An array of HotSpots. If no HotSpots were found, an array
84      * of length 0 is returned.
85      */

86     public HotSpot[] getHotSpots() {
87         ArrayList JavaDoc al = new ArrayList JavaDoc();
88
89         for (int i = 0; i < il.size(); i++) {
90             Instruction ins = il.get(i);
91             short opCode = ins.getOpcode();
92
93             for (int j = 0; j < opCodes.length; j++) {
94                 if (opCode == opCodes[j]) {
95                     HotSpot hs = createHotSpot(il, i);
96 // if (rule.match(hs.getName()))
97
al.add(hs);
98                 }
99             }
100         }
101
102         return (HotSpot[])al.toArray(new HotSpot[0]);
103     }
104
105
106     /**
107      * Create HotSpot.
108      * InstructionList is scanned backwards until stack consumption
109      * of Instruction at given index is satisfied.
110      *
111      * @param il InstructionList
112      * @param idx hotspot Instruction.
113      */

114     private HotSpot createHotSpot(InstructionList il, int idx) {
115         Instruction end = il.get(idx);
116         Instruction start = end;
117
118         int stackConsumption = end.stackConsumption();
119         while(stackConsumption > 0) {
120             start = il.get(idx - 1); // Previous instruction
121
stackConsumption -= start.stackUsage();
122             idx--;
123         }
124
125         return new HotSpot(il, start, end);
126     }
127 }
128
Popular Tags