KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > reflect > Loader


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.reflect;
20
21 import java.io.InputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.lang.reflect.Modifier JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import org.apache.log4j.Category;
32
33 import alt.jiapi.Runtime; // Dependency in wrong direction!
34
import alt.jiapi.file.ClassFile;
35
36 /*
37  *
38  * NOTE: Change this javadoc to relevant place !!!!
39  *
40  * This class modifies class loading so, that classes to be loaded
41  * are first checked wheteher or not they need to be instrumented.
42  * The algorithm, that decides the need for instrumentation,
43  * prefers exclusion. That is, a class is not to be instrumented.
44  * In other words, if you want a class to be instrumented, you will
45  * have to say so.<p>
46  *
47  * If there is a set of rules, that would apply both to inclusion and
48  * exclusion, more specific rule will apply.<p>
49  *
50  * That said, if have the following rules : <br>
51  * <blockquote>
52  * exclude : org.opensource<br>
53  * include : org.opensource.coolstuff<br>
54  * </blockquote>
55  * Then, <br>
56  * <blockquote>
57  * <b>org.opensource.coolstuff.Utility</b> class would be instrumented<br>
58  * <b>org.opensource.Thing</b> class would <b>not</b> be instrumented<br>
59  * <b>org.opensource.other.Thing</b> class would <b>not</b> be instrumented<br>
60  * </blockquote>
61  *
62  * There are two levels of rules : Package level and Class rules.
63  * Following describes each of the types of rules.
64  * <ul>
65  * <li>package level rules
66  * <blockquote>
67  * One specifies a package that is to be included or excluded with
68  * instrumentation. Package rules may be given so, that any part of
69  * the package name may be given in a rule. For example, one may
70  * give a package rule "org", or "org.opensource".
71  * All of the subpackages will be covered with this rule.
72  * </blockquote>
73  * <li>class level rules
74  * <blockquote>
75  * One specifies a class that is to be included or excluded with
76  * instrumentation. All of the methods of that class are covered
77  * by this rule.
78  * </blockquote>
79  * </ul>
80  *
81  * @author Mika Riekkinen
82  * @author Joni Suominen
83  * @version $Revision: 1.5 $ $Date: 2004/02/28 21:40:23 $
84  */

85
86 /**
87  * Loads a JiapiClass. This class slightly looks like java.lang.ClassLoader.
88  *
89  * @author Mika Riekkinen
90  * @author Joni Suominen
91  * @version $Revision: 1.5 $ $Date: 2004/02/28 21:40:23 $
92  */

93 public class Loader {
94     /**
95      * Creates new Loader.
96      */

97     public Loader() {
98     }
99
100     /**
101      * Loads a JiapiCass from CLASSPATH. CLASSPATH is searched and if
102      * a class with given name is found, a JiapiClass is created to
103      * represent that class. When JiapiClass is created, it does not
104      * indicate, that corresponding class has been loaded/linked into
105      * Virtual Machine. An in-memory object representation for a given
106      * class is just created.
107      *
108      * @param className name of a class
109      * @return JiapiClass which is an in-memory representation for a class
110      * @exception ClassNotFoundException, if the class with given name can't
111      * be found
112      * @exception IOException, if the loading of class failed.
113      * @see #loadClass(String, URL)
114      */

115     public JiapiClass loadClass(InputStream JavaDoc is) throws ClassNotFoundException JavaDoc, IOException JavaDoc {
116         ClassFile clazz = ClassFile.parse(is);
117
118         JiapiClass jiapiClass = new JiapiClass(clazz);
119         jiapiClass.setLoader(this);
120             
121         return jiapiClass;
122     }
123
124
125     /**
126      * Loads a JiapiCass from CLASSPATH. CLASSPATH is searched and if
127      * a class with given name is found, a JiapiClass is created to
128      * represent that class. When JiapiClass is created, it does not
129      * indicate, that corresponding class has been loaded/linked into
130      * Virtual Machine. An in-memory object representation for a given
131      * class is just created.
132      *
133      * @param className name of a class
134      * @return JiapiClass which is an in-memory representation for a class
135      * @exception ClassNotFoundException, if the class with given name can't
136      * be found
137      * @exception IOException, if the loading of class failed.
138      * @see #loadClass(String, URL)
139      */

140     public JiapiClass loadClass(String JavaDoc className) throws ClassNotFoundException JavaDoc, IOException JavaDoc {
141         return loadClass(className, null);
142     }
143
144     /**
145      * Loads a JiapiCass from a given URL. CLASSPATH is searched and if
146      * a class with given name is found, a JiapiClass is created to
147      * represent that class. When JiapiClass is created, it does not
148      * indicate, that corresponding class has been loaded/linked into
149      * Virtual Machine. An in-memory object representation for a given
150      * class is just created.
151      *
152      * @param className name of a class
153      * @param location an URL where the class should be loaded, if null
154      * then CLASSPATH is used to locate the class
155      * @return JiapiClass which is an in-memory representation for a class
156      * @exception ClassNotFoundException, if the class with given name can't
157      * be found
158      * @exception IOException, if the loading of class failed.
159      */

160     public JiapiClass loadClass(String JavaDoc className, URL JavaDoc location) throws ClassNotFoundException JavaDoc, IOException JavaDoc {
161         JiapiClass jiapiClass = null;
162
163         InputStream JavaDoc is = getInputStream(className, location);
164         if (is == null) {
165             throw new ClassNotFoundException JavaDoc(className);
166         }
167
168         ClassFile clazz = ClassFile.parse(is);
169
170         jiapiClass = new JiapiClass(clazz);
171         jiapiClass.setLoader(this);
172             
173         return jiapiClass;
174     }
175
176
177     private InputStream JavaDoc getInputStream(String JavaDoc name, URL JavaDoc location) throws IOException JavaDoc {
178         if (location == null) {
179             String JavaDoc resource = "/" + name.replace('.', '/') + ".class";
180
181             return getClass().getResourceAsStream(resource);
182         }
183         return location.openStream();
184         //throw new RuntimeException("NOT IMPLEMENTED: " + location);
185
}
186
187     // For testing purposes. args[0] is fully qualified classname
188
// args[1], if given, is the filename, that JiapiClass
189
// gets dumped to.
190
// > java alt.jiapi.reflect.Loader alt.jiapi.reflect.Loader
191
// Would read alt.jiapi.reflect.Loader
192
//
193
// > java alt.jiapi.reflect.Loader alt.jiapi.reflect.Loader test.class
194
// Would read Loader and dump that class into test.class.
195
// test.class should be exactly the same as Loader.class
196
//
197
// > java alt.jiapi.reflect.Loader test
198
// Would read test created above
199
public static void main(String JavaDoc[] args) throws Exception JavaDoc {
200         Loader l = new Loader();
201         JiapiClass clazz = l.loadClass(args[0]);
202         System.out.println(clazz);
203         
204         if (args.length > 2) {
205             clazz.addInterface(args[2]);
206         }
207
208 // if (args.length > 3) {
209
// clazz.addField(args[3]);
210
// }
211

212         clazz.addField("jiapi_generated");
213
214         if (args.length > 1) {
215             clazz.dump(new java.io.FileOutputStream JavaDoc(args[1]));
216         }
217
218     }
219 }
220
Popular Tags