KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Modifier JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.NoSuchElementException JavaDoc;
26
27 import org.apache.log4j.Category;
28
29 import alt.jiapi.reflect.InstructionList;
30 import alt.jiapi.reflect.JiapiClass;
31 import alt.jiapi.reflect.JiapiMethod;
32
33 import alt.jiapi.Instrumentor;
34 /**
35  * InstrumentorChain.
36  *
37  * @author Mika Riekkinen
38  * @author Joni Suominen
39  * @version $Revision: 1.1 $ $Date: 2004/03/15 14:45:06 $
40  */

41 public class InstrumentorChain implements Instrumentor {
42     private Instrumentation instrumentation = new Instrumentation();
43     private static Category log = alt.jiapi.Runtime.getLogCategory(InstrumentorChain.class);
44
45     private JiapiClass currentClass;
46
47     /**
48      * Instrumentors which will be used in this chain.
49      */

50     private List JavaDoc instrumentors = new ArrayList JavaDoc();
51
52     public InstrumentorChain() {
53     }
54
55     /**
56      * Adds a new Instrumentor to the end of this chain.
57      * @param instrumentor Instrumentor
58      */

59     public void add(ChainInstrumentor instrumentor) {
60         if (instrumentors.size() > 0) {
61             AbstractInstrumentor parent =
62                 (AbstractInstrumentor)instrumentors.get(instrumentors.size() - 1);
63             parent.setChild((AbstractInstrumentor)instrumentor);
64         }
65
66         instrumentors.add(instrumentor);
67     }
68
69     public JiapiClass getCurrentClass() {
70         return currentClass;
71     }
72
73     /**
74      * Starts the chain using a given class.
75      */

76     public void instrument(JiapiClass clazz) {
77         currentClass = clazz;
78         // NOTE! This is temporary kludge. This splitting should
79
// be accomblished with SplitInstrumentor.
80
JiapiMethod[] methods = clazz.getDeclaredMethods();
81
82         AbstractInstrumentor instr =
83             (AbstractInstrumentor)instrumentors.get(0);
84
85         log.info("Bootstrapping instrumentation of " +
86                  clazz.getName() + " to " + this);
87
88         instr.setCurrentClass(clazz);
89         instr.instrument(null, instrumentation);
90
91         /*
92         for (int i = 0; i < methods.length; i++) {
93             InstructionList il = methods[i].getInstructionList();
94             instr.setCurrentMethod(methods[i]);
95             //instr.setInstrumentation(instrumentation);
96
97             //il = il.createView(0);
98
99             int originalSize = il.size();
100             instr.instrument(il, instrumentation);
101             log.info(clazz + " :: " + methods[i].getName() + ", size change : " + (il.size() - originalSize));
102         }
103         */

104
105         log.info("Instrumentation of " +
106                  clazz.getName() + " is finished with " + this);
107     }
108
109
110     public String JavaDoc toString() {
111         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("[");
112
113         Iterator JavaDoc iter = instrumentors.iterator();
114         while(iter.hasNext()) {
115             ChainInstrumentor i = (ChainInstrumentor)iter.next();
116             sb.append(i.toString());
117
118             if (iter.hasNext()) {
119                 sb.append(", ");
120             }
121         }
122
123         sb.append(']');
124         
125         return sb.toString();
126     }
127
128
129     // Pacakge access, called by AbstractInstrumentor
130
Instrumentation getInstrumentation() {
131         return instrumentation;
132     }
133 }
134
Popular Tags