KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > bytecode > JClassWrapper


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.bytecode;
31
32 import java.lang.ref.SoftReference JavaDoc;
33 import java.lang.reflect.Constructor JavaDoc;
34 import java.lang.reflect.Field JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36 import java.lang.reflect.Modifier JavaDoc;
37
38 /**
39  * Wrapper around the java Class for a JClass.
40  */

41 public class JClassWrapper extends JClass {
42   private JClassLoader _loader;
43   
44   private Class JavaDoc _class;
45
46   private SoftReference JavaDoc<JMethod[]> _declaredMethodsRef;
47
48   public JClassWrapper(Class JavaDoc cl, JClassLoader loader)
49   {
50     _loader = loader;
51     
52     _class = cl;
53   }
54
55   JClassWrapper(Class JavaDoc cl)
56   {
57     _class = cl;
58   }
59
60   /**
61    * Returns the class name.
62    */

63   public String JavaDoc getName()
64   {
65     return _class.getName();
66   }
67
68   /**
69    * Returns the Java class.
70    */

71   public Class JavaDoc getJavaClass()
72   {
73     return _class;
74   }
75
76   /**
77    * Returns the class.
78    */

79   public Class JavaDoc getWrappedClass()
80   {
81     return _class;
82   }
83
84   /**
85    * Returns the loader.
86    */

87   public JClassLoader getClassLoader()
88   {
89     if (_loader != null)
90       return _loader;
91     else
92       return JClassLoader.getSystemClassLoader();
93   }
94   
95   /**
96    * Returns true for a primitive class.
97    */

98   public boolean isPrimitive()
99   {
100     return _class.isPrimitive();
101   }
102   
103   /**
104    * Returns true for a primitive class.
105    */

106   public boolean isArray()
107   {
108     return _class.isArray();
109   }
110   
111   /**
112    * Returns the component type for a primitive class.
113    */

114   public JClass getComponentType()
115   {
116     return getClassLoader().forName(_class.getComponentType().getName());
117   }
118   
119   /**
120    * Returns true for a public class.
121    */

122   public boolean isPublic()
123   {
124     return Modifier.isPublic(_class.getModifiers());
125   }
126   
127   /**
128    * Returns true for an abstract class
129    */

130   public boolean isAbstract()
131   {
132     return Modifier.isAbstract(_class.getModifiers());
133   }
134   
135   /**
136    * Returns true for a final class
137    */

138   public boolean isFinal()
139   {
140     return Modifier.isFinal(_class.getModifiers());
141   }
142   
143   /**
144    * Returns true for an interface
145    */

146   public boolean isInterface()
147   {
148     return _class.isInterface();
149   }
150
151   /**
152    * Returns true for assignability.
153    */

154   public boolean isAssignableTo(Class JavaDoc cl)
155   {
156     return cl.isAssignableFrom(_class);
157   }
158
159   /**
160    * Returns true for assignability.
161    */

162   public boolean isAssignableFrom(Class JavaDoc cl)
163   {
164     return _class.isAssignableFrom(cl);
165   }
166
167   /**
168    * Returns true for assignability.
169    */

170   public boolean isAssignableFrom(JClass cl)
171   {
172     return cl.isAssignableTo(_class);
173   }
174   
175   /**
176    * Returns the superclass
177    */

178   public JClass getSuperClass()
179   {
180     Class JavaDoc cl = _class.getSuperclass();
181
182     if (cl != null)
183       return _loader.forName(cl.getName());
184     else
185       return null;
186   }
187   
188   /**
189    * Returns the superclass
190    */

191   public JClass []getInterfaces()
192   {
193     Class JavaDoc []cl = _class.getInterfaces();
194
195     JClass []clList = new JClass[cl.length];
196
197     for (int i = 0; i < cl.length; i++)
198       clList[i] = _loader.forName(cl[i].getName());
199
200     return clList;
201   }
202     
203   /**
204    * Returns the declared methods.
205    */

206   public JMethod []getDeclaredMethods()
207   {
208     SoftReference JavaDoc<JMethod[]> jMethodsRef = _declaredMethodsRef;
209     JMethod []jMethods = null;
210
211     if (jMethodsRef != null) {
212       jMethods = jMethodsRef.get();
213       if (jMethods != null)
214     return jMethods;
215     }
216
217     Method JavaDoc []methods = _class.getDeclaredMethods();
218     
219     jMethods = new JMethod[methods.length];
220
221     for (int i = 0; i < methods.length; i++) {
222       jMethods[i] = new JMethodWrapper(methods[i], getClassLoader());
223     }
224
225     _declaredMethodsRef = new SoftReference JavaDoc<JMethod[]>(jMethods);
226
227     return jMethods;
228   }
229     
230   /**
231    * Returns the public methods.
232    */

233   public JMethod []getMethods()
234   {
235     Method JavaDoc []methods = _class.getMethods();
236     
237     JMethod []jMethods = new JMethod[methods.length];
238
239     for (int i = 0; i < methods.length; i++) {
240       jMethods[i] = new JMethodWrapper(methods[i], getClassLoader());
241     }
242
243     return jMethods;
244   }
245
246   /**
247    * Returns the matching methods.
248    */

249   public JMethod getMethod(String JavaDoc name, JClass []types)
250   {
251     JClassLoader jClassLoader = getClassLoader();
252     
253     return getMethod(_class, name, types, jClassLoader);
254   }
255
256   private static JMethod getMethod(Class JavaDoc cl, String JavaDoc name, JClass []types,
257                    JClassLoader jClassLoader)
258   {
259     if (cl == null)
260       return null;
261     
262     loop:
263     for (Method JavaDoc method : cl.getDeclaredMethods()) {
264       if (! method.getName().equals(name))
265     continue;
266
267       Class JavaDoc []paramTypes = method.getParameterTypes();
268       if (types.length != paramTypes.length)
269     continue;
270
271       for (int i = 0; i < types.length; i++) {
272     if (! types[i].getName().equals(paramTypes[i].getName()))
273       continue loop;
274       }
275
276       return new JMethodWrapper(method, jClassLoader);
277     }
278
279     for (Class JavaDoc ifc : cl.getInterfaces()) {
280       JMethod method = getMethod(ifc, name, types, jClassLoader);
281
282       if (method != null)
283     return method;
284     }
285
286     return getMethod(cl.getSuperclass(), name, types, jClassLoader);
287   }
288     
289   /**
290    * Returns the public constructors.
291    */

292   public JMethod []getConstructors()
293   {
294     Constructor JavaDoc []methods = _class.getConstructors();
295     
296     JMethod []jMethods = new JMethod[methods.length];
297
298     for (int i = 0; i < methods.length; i++) {
299       jMethods[i] = new JConstructorWrapper(methods[i], getClassLoader());
300     }
301
302     return jMethods;
303   }
304     
305   /**
306    * Returns the declared methods.
307    */

308   public JField []getDeclaredFields()
309   {
310     Field JavaDoc []fields = _class.getDeclaredFields();
311     
312     JField []jFields = new JField[fields.length];
313
314     for (int i = 0; i < fields.length; i++) {
315       jFields[i] = new JFieldWrapper(fields[i], getClassLoader());
316     }
317
318     return jFields;
319   }
320     
321   /**
322    * Returns the declared methods.
323    */

324   public JField []getFields()
325   {
326     Field JavaDoc []fields = _class.getFields();
327     
328     JField []jFields = new JField[fields.length];
329
330     for (int i = 0; i < fields.length; i++) {
331       jFields[i] = new JFieldWrapper(fields[i], getClassLoader());
332     }
333
334     return jFields;
335   }
336
337   public String JavaDoc toString()
338   {
339     return "JClassWrapper[" + getName() + "]";
340   }
341 }
342
Popular Tags