KickJava   Java API By Example, From Geeks To Geeks.

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


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.Method JavaDoc;
22 import java.lang.reflect.Modifier JavaDoc;
23
24 import org.apache.log4j.Category;
25
26 import alt.jiapi.Runtime;
27
28 import alt.jiapi.reflect.InstructionFactory;
29 import alt.jiapi.reflect.InstructionList;
30 import alt.jiapi.reflect.Loader;
31 import alt.jiapi.reflect.JiapiClass;
32 import alt.jiapi.reflect.JiapiField;
33 import alt.jiapi.reflect.JiapiMethod;
34
35 /**
36  * This patch can be used to copy instructions from one class
37  * to another.
38  *
39  * @author Mika Riekkinen
40  * @author Joni Suominen
41  * @version $Revision: 1.8 $ $Date: 2004/03/15 14:47:53 $
42  */

43 public class CopyInstrumentor extends AbstractInstrumentor {
44     private InstructionList source;
45
46     /**
47      * Construct a CopyInstrumentor from a given method.
48      * The given method's instructions can then be copied elsewhere.
49      * <p>
50      * One may first wonder why this method can throw ClassNotFoundException
51      * and NoSuchMethodException. The reason is that in some (rare) cases
52      * Jiapi runtime might use different CLASSPATH or class loading
53      * mechanism than calling client.
54      *
55      * @param sourceMethod a method for source instructions
56      * @exception ClassNotFoundException thrown if the declaring class
57      * is not found
58      * @exception NoSuchMethodException thrown if the class son't contain
59      * the requested method
60      */

61     public CopyInstrumentor(Method JavaDoc sourceMethod, Loader loader)
62             throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc {
63
64         // Load sourceMethod's instructions.
65
// Loader loader = getContext().getLoader();
66
JiapiClass clazz = null;
67         try {
68             clazz=loader.loadClass(sourceMethod.getDeclaringClass().getName());
69         }
70         catch(java.io.IOException JavaDoc ioe) {
71             ioe.printStackTrace();
72             throw new ClassNotFoundException JavaDoc(sourceMethod.getDeclaringClass().getName());
73         }
74
75         Class JavaDoc[] parameterTypes = sourceMethod.getParameterTypes();
76         String JavaDoc[] parameterTypeNames = new String JavaDoc[parameterTypes.length];
77         for (int i = 0; i < parameterTypes.length; i++) {
78             parameterTypeNames[i] = parameterTypes[i].getName();
79         }
80
81         JiapiMethod method = clazz.getMethod(sourceMethod.getName(),
82                                              parameterTypeNames);
83         source = method.getInstructionList();
84     }
85
86     /**
87      * Construct a CopyInstrumentor from given instructions.
88      * The given instructions can then be copied elsewhere.
89      *
90      * @param source source instructions
91      */

92     public CopyInstrumentor(InstructionList source) {
93         this.source = source;
94     }
95
96     public void instrument(InstructionList il) {
97         InstructionFactory factory =
98             il.getDeclaringMethod().getInstructionFactory();
99
100         if (true)
101             throw new RuntimeException JavaDoc("NOT IMPLEMENTED");
102 // il.add(factory.copy(source));
103
forward(il);
104     }
105 }
106
Popular Tags