001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.bytecode;
031:
032: import java.lang.annotation.Annotation;
033:
034: import java.lang.ref.SoftReference;
035: import java.lang.reflect.Constructor;
036: import java.lang.reflect.Field;
037: import java.lang.reflect.Method;
038: import java.lang.reflect.Modifier;
039:
040: /**
041: * Wrapper around the java Class for a JClass.
042: */
043: public class JClassWrapper extends JClass {
044: private JClassLoader _loader;
045:
046: private Class _class;
047:
048: private SoftReference<JMethod[]> _declaredMethodsRef;
049:
050: public JClassWrapper(Class cl, JClassLoader loader) {
051: _loader = loader;
052:
053: _class = cl;
054: }
055:
056: public static JClassWrapper create(Class cl) {
057: return new JClassWrapper(cl, JClassLoaderWrapper.create());
058: }
059:
060: JClassWrapper(Class cl) {
061: _class = cl;
062: }
063:
064: /**
065: * Returns the class name.
066: */
067: public String getName() {
068: return _class.getName();
069: }
070:
071: /**
072: * Returns the Java class.
073: */
074: public Class getJavaClass() {
075: return _class;
076: }
077:
078: /**
079: * Returns the class.
080: */
081: public Class getWrappedClass() {
082: return _class;
083: }
084:
085: /**
086: * Returns the loader.
087: */
088: public JClassLoader getClassLoader() {
089: if (_loader != null)
090: return _loader;
091: else
092: return JClassLoader.getSystemClassLoader();
093: }
094:
095: /**
096: * Returns true for a primitive class.
097: */
098: public boolean isPrimitive() {
099: return _class.isPrimitive();
100: }
101:
102: /**
103: * Returns true for a primitive class.
104: */
105: public boolean isArray() {
106: return _class.isArray();
107: }
108:
109: /**
110: * Returns the component type for a primitive class.
111: */
112: public JClass getComponentType() {
113: return getClassLoader().forName(
114: _class.getComponentType().getName());
115: }
116:
117: /**
118: * Returns true for a public class.
119: */
120: public boolean isPublic() {
121: return Modifier.isPublic(_class.getModifiers());
122: }
123:
124: /**
125: * Returns true for an abstract class
126: */
127: public boolean isAbstract() {
128: return Modifier.isAbstract(_class.getModifiers());
129: }
130:
131: /**
132: * Returns true for a final class
133: */
134: public boolean isFinal() {
135: return Modifier.isFinal(_class.getModifiers());
136: }
137:
138: /**
139: * Returns true for an interface
140: */
141: public boolean isInterface() {
142: return _class.isInterface();
143: }
144:
145: /**
146: * Returns true for assignability.
147: */
148: public boolean isAssignableTo(Class cl) {
149: return cl.isAssignableFrom(_class);
150: }
151:
152: /**
153: * Returns true for assignability.
154: */
155: public boolean isAssignableFrom(Class cl) {
156: return _class.isAssignableFrom(cl);
157: }
158:
159: /**
160: * Returns true for assignability.
161: */
162: public boolean isAssignableFrom(JClass cl) {
163: return cl.isAssignableTo(_class);
164: }
165:
166: /**
167: * Returns the superclass
168: */
169: public JClass getSuperClass() {
170: Class cl = _class.getSuperclass();
171:
172: if (cl != null)
173: return _loader.forName(cl.getName());
174: else
175: return null;
176: }
177:
178: /**
179: * Returns the superclass
180: */
181: public JClass[] getInterfaces() {
182: Class[] cl = _class.getInterfaces();
183:
184: JClass[] clList = new JClass[cl.length];
185:
186: for (int i = 0; i < cl.length; i++)
187: clList[i] = _loader.forName(cl[i].getName());
188:
189: return clList;
190: }
191:
192: /**
193: * Returns the declared methods.
194: */
195: public JMethod[] getDeclaredMethods() {
196: SoftReference<JMethod[]> jMethodsRef = _declaredMethodsRef;
197: JMethod[] jMethods = null;
198:
199: if (jMethodsRef != null) {
200: jMethods = jMethodsRef.get();
201: if (jMethods != null)
202: return jMethods;
203: }
204:
205: Method[] methods = _class.getDeclaredMethods();
206:
207: jMethods = new JMethod[methods.length];
208:
209: for (int i = 0; i < methods.length; i++) {
210: jMethods[i] = new JMethodWrapper(methods[i],
211: getClassLoader());
212: }
213:
214: _declaredMethodsRef = new SoftReference<JMethod[]>(jMethods);
215:
216: return jMethods;
217: }
218:
219: /**
220: * Returns the public methods.
221: */
222: public JMethod[] getMethods() {
223: Method[] methods = _class.getMethods();
224:
225: JMethod[] jMethods = new JMethod[methods.length];
226:
227: for (int i = 0; i < methods.length; i++) {
228: jMethods[i] = new JMethodWrapper(methods[i],
229: getClassLoader());
230: }
231:
232: return jMethods;
233: }
234:
235: /**
236: * Returns the matching methods.
237: */
238: public JMethod getMethod(String name, JClass[] types) {
239: JClassLoader jClassLoader = getClassLoader();
240:
241: return getMethod(_class, name, types, jClassLoader);
242: }
243:
244: private static JMethod getMethod(Class cl, String name,
245: JClass[] types, JClassLoader jClassLoader) {
246: if (cl == null)
247: return null;
248:
249: loop: for (Method method : cl.getDeclaredMethods()) {
250: if (!method.getName().equals(name))
251: continue;
252:
253: Class[] paramTypes = method.getParameterTypes();
254: if (types.length != paramTypes.length)
255: continue;
256:
257: for (int i = 0; i < types.length; i++) {
258: if (!types[i].getName().equals(paramTypes[i].getName()))
259: continue loop;
260: }
261:
262: return new JMethodWrapper(method, jClassLoader);
263: }
264:
265: for (Class ifc : cl.getInterfaces()) {
266: JMethod method = getMethod(ifc, name, types, jClassLoader);
267:
268: if (method != null)
269: return method;
270: }
271:
272: return getMethod(cl.getSuperclass(), name, types, jClassLoader);
273: }
274:
275: /**
276: * Returns the public constructors.
277: */
278: public JMethod[] getConstructors() {
279: Constructor[] methods = _class.getConstructors();
280:
281: JMethod[] jMethods = new JMethod[methods.length];
282:
283: for (int i = 0; i < methods.length; i++) {
284: jMethods[i] = new JConstructorWrapper(methods[i],
285: getClassLoader());
286: }
287:
288: return jMethods;
289: }
290:
291: /**
292: * Returns the declared methods.
293: */
294: public JField[] getDeclaredFields() {
295: Field[] fields = _class.getDeclaredFields();
296:
297: JField[] jFields = new JField[fields.length];
298:
299: for (int i = 0; i < fields.length; i++) {
300: jFields[i] = new JFieldWrapper(fields[i], getClassLoader());
301: }
302:
303: return jFields;
304: }
305:
306: /**
307: * Returns the declared methods.
308: */
309: public JField[] getFields() {
310: Field[] fields = _class.getFields();
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 annotation.
323: */
324: @Override
325: public JAnnotation getAnnotation(String name) {
326: Annotation[] ann = _class.getAnnotations();
327:
328: for (int i = 0; i < ann.length; i++) {
329: if (ann[i].annotationType().getName().equals(name))
330: return new JAnnotationWrapper(ann[i]);
331: }
332:
333: return null;
334: }
335:
336: /**
337: * Returns the annotation.
338: */
339: @Override
340: public JAnnotation[] getDeclaredAnnotations() {
341: Annotation[] ann = _class.getAnnotations();
342:
343: JAnnotation[] jAnn = new JAnnotation[ann.length];
344:
345: for (int i = 0; i < ann.length; i++) {
346: jAnn[i] = new JAnnotationWrapper(ann[i]);
347: }
348:
349: return jAnn;
350: }
351:
352: public String toString() {
353: return "JClassWrapper[" + getName() + "]";
354: }
355: }
|