KickJava   Java API By Example, From Geeks To Geeks.

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


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.Rule;
29 import alt.jiapi.reflect.InstructionList;
30
31 /**
32  * GrepInstrumentor finds known spots from intstruction lists and forwards
33  * them to next Instrumentor in chain. Known spots are found by Strategies
34  * given to this class. This instrumentor can be configured to work in
35  * reverse mode, where known spots are blocked from further instrumentation.<p>
36  *
37  * If the instructionList, that is being instrumented is as follows:
38  *
39  * <blockquote>
40  * <code>iiiiiiiXXXXXXXiiiiiiiXXXXXXX</code>
41  * </blockquote>
42  *
43  * where <code>i</code> represents an Instruction, that is not of interest.
44  * <code>X</code> represents an interesting Instruction, which is
45  * found by Strategy given to this Instrumentor. InstructionList
46  * is then split into two InstructionLists as follows:
47  * <blockquote>
48  * <code>XXXXXXX XXXXXXX</code>
49  * </blockquote>
50  * However, if GrepInstrumentor was configred in reverse mode, it would
51  * split InstructionList as follows:
52  * <blockquote>
53  * <code>iiiiiii iiiiiii</code>
54  * </blockquote>
55  * each of which are forwarded to the following Instrumentor in chain.
56  *
57  * @see Strategy
58  * @author Mika Riekkinen
59  * @author Joni Suominen
60  * @version $Revision: 1.11 $ $Date: 2004/03/15 14:47:53 $
61  */

62 public class GrepInstrumentor extends AbstractInstrumentor {
63     private static Category log = Runtime.getLogCategory(GrepInstrumentor.class);
64
65     private Strategy strategy;
66
67
68     /**
69      * This constructor creates GrepInstrumentor that operates in
70      * normal mode. That is, known spots found by Strategies are forwarded
71      * to next Instrumentor in chain. Unknown spots are blocked from
72      * further instrumentation.
73      *
74      * @param strategy A Strategy to be used in finding known spots
75      * from InstructionLists
76      */

77     public GrepInstrumentor(Strategy strategy) {
78         log.info("GrepInstrumentor: " + strategy);
79
80         this.strategy = strategy;
81     }
82
83     
84     /**
85      * Splits given InstructionList into set of InstructionLists.
86      */

87     public void instrument(InstructionList il) {
88         log.info("Instrumenting " + getCurrentClass().getName() + "." +
89                  il.getDeclaringMethod().getName());
90
91         ((AbstractStrategy)strategy).setInstrumentation(getInstrumentation());
92         //List boundaries = strategy.findBoundaries(il);
93
List JavaDoc hotSpots = strategy.findHotSpots(il);
94
95         Iterator JavaDoc iter = hotSpots.iterator();
96         while (iter.hasNext()) {
97             HotSpot hs = (HotSpot)iter.next();
98             InstructionList view = il.createView(hs.getStart(), hs.getEnd());
99 // System.out.println("Grep:" + hs.getStart() + "-" + hs.getEnd() +
100
// ":" + view);
101

102             forward(view);
103         }
104     }
105
106
107     /**
108      * Set the resolution, that is to be used by this GrepInstrumentor
109      * and its Strategy.
110      *
111      * @param resolution Resolution to be used.
112      */

113     public void setResolution(String JavaDoc resolution) {
114         setResolutions(new String JavaDoc[] {resolution});
115     }
116     
117     /**
118      * Set the resolutions, that are to be used by this GrepInstrumentor
119      * and its Strategy.
120      *
121      * @param resolutions Resolutions to be used.
122      */

123     public void setResolutions(String JavaDoc[] resolutions) {
124         if (strategy instanceof AbstractStrategy) {
125             ((AbstractStrategy)strategy).setResolutions(resolutions);
126         }
127         else {
128             // Should we declare matcher method in Strategy.
129
// Or should we get rid of Strategy and use AbstractStrategy
130
// in APIs
131
log.warn("Cannot set resolutions to " + strategy +
132                      ": It is not an instanceof AbstractStrategy");
133         }
134     }
135
136
137     /**
138      * Converts this GrepInstrumentor to String.
139      */

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