KickJava   Java API By Example, From Geeks To Geeks.

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


1 package alt.jiapi.util;
2
3 import alt.jiapi.reflect.Instruction;
4 import alt.jiapi.reflect.InstructionList;
5 import alt.jiapi.reflect.Signature;
6 import alt.jiapi.reflect.instruction.FieldAccess;
7 import alt.jiapi.reflect.instruction.Invocation;
8
9 /**
10  * HotSpot.<p>
11  *
12  * HotSpot is defined as being an interesting Instruction,
13  * and all the Instructions before it, that has a total
14  * stack usage of 0.<p>
15  *
16  * If the interesting Instruction has a positive or zero stack usage,
17  * HotSpot is defined as being that single Instruction.<p>
18  *
19  * NOTE: We might change this definition to be exactly zero stack usage<p>
20  *
21  * For invocation, this would be
22  * <ol>
23  * <li>Object reference of target (if <i>invokevirtual</i>)
24  * <li>All the Instructions, that push arguments into stack
25  * <li>Actual method invoke
26  * </ol>
27  *
28  *
29  * @author Mika Riekkinen
30  */

31 public class HotSpot {
32     private Instruction startIns;
33     private Instruction endIns;
34     private InstructionList il;
35
36     /**
37      * Create new hotspot for a single Instruction.
38      *
39      * @param il InstructionList that contains hotspot
40      * @param ins An Instruction, that makes up this hotspot
41      */

42     public HotSpot(InstructionList il, Instruction ins) {
43         this(il, ins, ins);
44     }
45
46     /**
47      * Create new hotspot.
48      *
49      * @param il InstructionList that contains hotspot.
50      * @param start First instruction of the hotspot
51      * @param end Last instruction of the hotspot
52      */

53     public HotSpot(InstructionList il, Instruction start, Instruction end) {
54         this.il = il;
55         this.startIns = start;
56         this.endIns = end;
57     }
58
59     /**
60      * Get the InstructionList that represents this hotspot.
61      *
62      * InstructionList returned is a view to underlying list. All the
63      * modifications made to view, is reflected back to
64      * underlying list.
65      *
66      * @return a view of the hotspot
67      */

68     public InstructionList getInstructionList() {
69         int i1 = il.indexOf(startIns);
70         int i2 = il.indexOf(endIns);
71
72         return il.createView(i1, i2 + 1);
73     }
74
75
76     /**
77      * Get the InstructionList that represents arguments of this hotspot.
78      * Arguments of the HotSpot is defined as being all the instructions
79      * returned by getInstructionList(), except the one
80      * returned by getHotSpotInstruction().<p>
81      *
82      * InstructionList returned is a view to underlying list. All the
83      * modifications made to view, is reflected back to
84      * underlying list.
85      *
86      * @return a view of the hotspot
87      */

88     public InstructionList getArgumentList() {
89         int i1 = il.indexOf(startIns);
90         int i2 = il.indexOf(endIns);
91
92         return il.createView(i1, i2);
93     }
94
95
96     /**
97      * Get the Instruction, that was used to trigger creation of this
98      * HotSpot. By definition, hotspot-instruction is allways the
99      * last instruction of the hotspot. So this method is a
100      * shortcut to <code>getInstructionList().get(il.size() - 1);</code>
101      *
102      * @return Instruction
103      */

104     public Instruction getHotSpotInstruction() {
105         return endIns;
106     }
107
108     /**
109      * Gets the name of this HotSpot.
110      * For invocations, name of the hotspot is constructed like this:<br>
111      * <i>ret-type class-name.method-name(param1,param2,...)</i><p>
112      *
113      * For example, for <br>
114      * <code><b>invokevirtual</b> java.io.PrintStream.println(Ljava/lang/String;)V</code><br>
115      * <code>void java.io.PrintStream.println(java.lang.String)</code> is
116      * returned
117      *
118      * <p>
119      *
120      * For field accesses, name of the hotspot is <br>
121      * <i>class-name.field-name</i><p>
122      *
123      * For the rest of the hotspots, an empty String is returned.
124      *
125      * @return Name of the hotspot
126      */

127     public String JavaDoc getName() {
128         if (endIns instanceof Invocation) {
129             Invocation inv = (Invocation)endIns;
130             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
131             if (!"<init>".equals(inv.getMethodName())) {
132                 sb.append(inv.getReturnType());
133                 sb.append(' ');
134             }
135
136             sb.append(inv.getClassName());
137             sb.append('.');
138             sb.append(inv.getMethodName());
139             sb.append('(');
140
141             String JavaDoc[] params = inv.getParameterTypes();
142             for (int i = 0; i < params.length; i++) {
143                 sb.append(params[i]);
144                 if (i < params.length - 1) {
145                     sb.append(',');
146                 }
147             }
148             sb.append(')');
149
150             return sb.toString();
151         }
152         else if (endIns instanceof FieldAccess) {
153             FieldAccess fa = (FieldAccess)endIns;
154             return fa.getClassName() + "." + fa.getFieldName();
155         }
156
157         return ""; // Should we return null instead?
158
}
159 }
160
Popular Tags