KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > instrumentor > SplitInstrumentor


1 /*
2  * Copyright (C) 2001 Mika Riekkinen, Joni Suominen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package alt.jiapi.instrumentor;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.apache.log4j.Category;
25
26 import alt.jiapi.Instrumentor;
27 import alt.jiapi.Runtime;
28 import alt.jiapi.reflect.InstructionList;
29
30 /**
31  * SplitInstrumentor splits input InstructionList into a set of
32  * InstructionLists. Strategy pattern is used in deciding how the
33  * split is to be done.<p>
34  * If the InstructionList, that is being instrumented is as follows :<br>
35  *
36  * <blockquote>
37  * <code>iiiiiiiXXXXXXXiiiiiiiXXXXiXXX</code>
38  * </blockquote>
39  *
40  * where <code>i</code> represents an Instruction, that is not of interest.
41  * <code>X</code> represents an interesting Instruction, which is
42  * found by Strategy given to this Instrumentor. InstructionList
43  * is then split into four InstructionLists as follows:
44  * <blockquote>
45  * <code>iiiiiii XXXXXXX iiiiiii XXXX i XXX</code>
46  * </blockquote>
47  * each of which are forwarded to the following Instrumentor in chain.
48  *
49  * @see Strategy
50  * @author Mika Riekkinen
51  * @author Joni Suominen
52  * @version $Revision: 1.7 $ $Date: 2004/03/15 14:47:53 $
53  */

54 public class SplitInstrumentor extends AbstractInstrumentor {
55     private static Category log = Runtime.getLogCategory(SplitInstrumentor.class);
56     private Strategy strategy;
57
58     /**
59      * Constructor.
60      * @param strategy A strategy used in splitting.
61      */

62     public SplitInstrumentor(Strategy strategy) {
63         log.info("SplitInstrumentor: " + strategy);
64
65         this.strategy = strategy;
66     }
67
68
69     /**
70      * Splits given Instructionlist into set of InstructionLists.
71      * InstructionLists forwarded to next Instrumentor in chain,
72      * have no gaps between each other. That is, if instruction list
73      * is split into <code>il1</code> and <code>il2</code>, and next
74      * instrumentor appends these two lists together, resulting instruction
75      * list will be the original one.
76      * <pre>
77      * il -> il1, il2
78      * il1 + il2 -> il
79      * <pre>
80      *
81      * @param il InstructionList to consider.
82      */

83     public void instrument(InstructionList il) {
84         log.info("Instrumenting " + getCurrentClass().getName() + "." +
85                  il.getDeclaringMethod().getName());
86
87         ((AbstractStrategy)strategy).setInstrumentation(getInstrumentation());
88
89         List JavaDoc boundaries = strategy.findHotSpots(il);
90         //List boundaries = strategy.findBoundaries(il);
91

92         Iterator JavaDoc iter = boundaries.iterator();
93         int start = 0;
94         int count = 0;
95         HotSpot boundary = null;
96
97         while (iter.hasNext()) {
98             count++;
99             boundary = (HotSpot)iter.next();
100             //Boundary h = (Boundary)iter.next();
101

102             int end = boundary.getStart() - 1; // just before hot spot
103
if (end < start) {
104                 // Case: start==0, end==0
105
// Should we continue here or not.
106
end = start;
107             }
108
109             InstructionList view = il.createView(start, end);
110
111             forward(view);
112
113             start = boundary.getEnd() + 1; // Just after hotspot
114
}
115
116         // Last list after last hotspot
117
if (boundary != null) {
118             //forward(il.createView(boundary.getEnd() + 1));
119
int end = boundary.getEnd() + 1;
120             if (end > il.size()) {
121                 // HotSpot contains the last instruction in il.
122
// Should we forward it or not(empty list)
123

124                 // Uncomment, if empty list gets forwarded
125
//forward(il.createView(il.size(), il.size()));
126
}
127             else {
128                 forward(il.createView(end, il.size()));
129             }
130         }
131         
132     }
133
134
135     /**
136      * Converts this Insturmentor to String.
137      */

138     public String JavaDoc toString() {
139         return super.toString() + "#" + strategy.toString();
140     }
141 }
142
143
Popular Tags