001: /*
002: Copyright (c) 2004-2007, Dennis M. Sosnoski
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without modification,
006: are permitted provided that the following conditions are met:
007:
008: * Redistributions of source code must retain the above copyright notice, this
009: list of conditions and the following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: * Neither the name of JiBX nor the names of its contributors may be used
014: to endorse or promote products derived from this software without specific
015: prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
021: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028:
029: package org.jibx.binding.model;
030:
031: import org.jibx.binding.classes.ClassFile;
032: import org.jibx.binding.classes.ClassItem;
033: import org.jibx.runtime.JiBXException;
034:
035: /**
036: * Wrapper for class information. This wraps the BCEL-based class handling
037: * implementation to support the interface defined for use with the binding
038: * model.
039: *
040: * @author Dennis M. Sosnoski
041: */
042: public class ClassWrapper implements IClass {
043: private final IClassLocator m_locator;
044: private final ClassFile m_class;
045: private IClassItem[] m_fields;
046: private IClassItem[] m_methods;
047:
048: /**
049: * Constructor.
050: *
051: * @param loc
052: * @param clas
053: */
054: public ClassWrapper(IClassLocator loc, ClassFile clas) {
055: m_locator = loc;
056: m_class = clas;
057: }
058:
059: /**
060: * Build an item wrapper. This method may be overridden by subclasses to
061: * return a specialized form of wrapper.
062: *
063: * @param item
064: * @return wrapper
065: */
066: protected IClassItem buildItem(ClassItem item) {
067: return new ClassItemWrapper(this , item);
068: }
069:
070: /* (non-Javadoc)
071: * @see org.jibx.binding.model.IClass#getName()
072: */
073: public String getName() {
074: return m_class.getName();
075: }
076:
077: /* (non-Javadoc)
078: * @see org.jibx.binding.model.IClass#getSignature()
079: */
080: public String getSignature() {
081: return m_class.getSignature();
082: }
083:
084: /* (non-Javadoc)
085: * @see org.jibx.binding.model.IClass#getPackage()
086: */
087: public String getPackage() {
088: return m_class.getPackage();
089: }
090:
091: /* (non-Javadoc)
092: * @see org.jibx.binding.model.IClass#getSuperClass()
093: */
094: public IClass getSuperClass() {
095: ClassFile scf = m_class.getSuperFile();
096: if (scf == null) {
097: return null;
098: } else {
099: return new ClassWrapper(m_locator, m_class.getSuperFile());
100: }
101: }
102:
103: /* (non-Javadoc)
104: * @see org.jibx.binding.model.IClass#getInterfaces()
105: */
106: public String[] getInterfaces() {
107: return m_class.getInterfaces();
108: }
109:
110: /* (non-Javadoc)
111: * @see org.jibx.binding.model.IClass#getInstanceSigs()
112: */
113: public String[] getInstanceSigs() {
114: try {
115: return m_class.getInstanceSigs();
116: } catch (JiBXException e) {
117: // TODO need to handle this differently - perhaps get all when created
118: throw new IllegalStateException("Internal error: instance "
119: + "signatures not found for class "
120: + m_class.getName());
121: }
122: }
123:
124: /* (non-Javadoc)
125: * @see org.jibx.binding.model.IClass#isImplements(java.lang.String)
126: */
127: public boolean isImplements(String sig) {
128: try {
129: return m_class.isImplements(sig);
130: } catch (JiBXException e) {
131: // TODO need to handle this differently - perhaps get all when created
132: throw new IllegalStateException("Internal error: instance "
133: + "signatures not found for class "
134: + m_class.getName());
135: }
136: }
137:
138: /* (non-Javadoc)
139: * @see org.jibx.binding.model.IClass#isAbstract()
140: */
141: public boolean isAbstract() {
142: return m_class.isAbstract();
143: }
144:
145: /* (non-Javadoc)
146: * @see org.jibx.binding.model.IClass#isInterface()
147: */
148: public boolean isInterface() {
149: return m_class.isInterface();
150: }
151:
152: /* (non-Javadoc)
153: * @see org.jibx.binding.model.IClass#isModifiable()
154: */
155: public boolean isModifiable() {
156: return m_class.isModifiable();
157: }
158:
159: /* (non-Javadoc)
160: * @see org.jibx.binding.model.IClass#isSuperclass(org.jibx.binding.model.IClass)
161: */
162: public boolean isSuperclass(String name) {
163: ClassFile current = m_class;
164: while (current != null) {
165: if (current.getName().equals(name)) {
166: return true;
167: } else {
168: current = current.getSuperFile();
169: }
170: }
171: return false;
172: }
173:
174: /* (non-Javadoc)
175: * @see org.jibx.binding.model.IClass#getDirectField(java.lang.String)
176: */
177: public IClassItem getDirectField(String name) {
178: ClassItem item = m_class.getDirectField(name);
179: if (item == null) {
180: return null;
181: } else {
182: return buildItem(item);
183: }
184: }
185:
186: /* (non-Javadoc)
187: * @see org.jibx.binding.model.IClass#getField(java.lang.String)
188: */
189: public IClassItem getField(String name) {
190: try {
191: return buildItem(m_class.getField(name));
192: } catch (JiBXException e) {
193: // TODO need to handle this differently - perhaps get all when created
194: return null;
195: }
196: }
197:
198: /* (non-Javadoc)
199: * @see org.jibx.binding.model.IClass#getMethod(java.lang.String, java.lang.String)
200: */
201: public IClassItem getMethod(String name, String sig) {
202: ClassItem item = m_class.getMethod(name, sig);
203: if (item == null) {
204: return null;
205: } else {
206: return buildItem(item);
207: }
208: }
209:
210: /* (non-Javadoc)
211: * @see org.jibx.binding.model.IClass#getMethod(java.lang.String, java.lang.String[])
212: */
213: public IClassItem getMethod(String name, String[] sigs) {
214: ClassItem item = m_class.getMethod(name, sigs);
215: if (item == null) {
216: return null;
217: } else {
218: return buildItem(item);
219: }
220: }
221:
222: /* (non-Javadoc)
223: * @see org.jibx.binding.model.IClass#getInitializerMethod(java.lang.String)
224: */
225: public IClassItem getInitializerMethod(String sig) {
226: ClassItem item = m_class.getInitializerMethod(sig);
227: if (item == null) {
228: return null;
229: } else {
230: return buildItem(item);
231: }
232: }
233:
234: /* (non-Javadoc)
235: * @see org.jibx.binding.model.IClass#getStaticMethod(java.lang.String, java.lang.String)
236: */
237: public IClassItem getStaticMethod(String name, String sig) {
238: ClassItem item = m_class.getStaticMethod(name, sig);
239: if (item == null) {
240: return null;
241: } else {
242: return buildItem(item);
243: }
244: }
245:
246: /* (non-Javadoc)
247: * @see org.jibx.binding.model.IClass#isAccessible(org.jibx.binding.model.IClassItem)
248: */
249: public boolean isAccessible(IClassItem item) {
250: return m_class.isAccessible(((ClassItemWrapper) item)
251: .getClassItem());
252: }
253:
254: /* (non-Javadoc)
255: * @see org.jibx.binding.model.IClass#isAssignable(org.jibx.binding.model.IClass)
256: */
257: public boolean isAssignable(IClass other) {
258: String[] sigs;
259: try {
260: sigs = m_class.getInstanceSigs();
261: } catch (JiBXException e) {
262: throw new IllegalStateException(
263: "Internal error: class information not available");
264: }
265: String match = other.getSignature();
266: for (int i = 0; i < sigs.length; i++) {
267: if (match.equals(sigs[i])) {
268: return true;
269: }
270: }
271: return false;
272: }
273:
274: /* (non-Javadoc)
275: * @see org.jibx.binding.model.IClass#getBestMethod(java.lang.String, java.lang.String, java.lang.String[])
276: */
277: public IClassItem getBestMethod(String name, String type,
278: String[] args) {
279: ClassItem item = m_class.getBestMethod(name, type, args);
280: if (item == null) {
281: return null;
282: } else {
283: return buildItem(item);
284: }
285: }
286:
287: /* (non-Javadoc)
288: * @see org.jibx.binding.model.IClass#getClassFile()
289: * TODO: eliminate this method
290: */
291: public ClassFile getClassFile() {
292: return m_class;
293: }
294:
295: /* (non-Javadoc)
296: * @see org.jibx.binding.model.IClass#loadClass()
297: */
298: public Class loadClass() {
299: String name = m_class.getName();
300: Class clas = ClassFile.loadClass(name);
301: if (clas == null) {
302: // TODO: this is a kludge
303: try {
304: clas = ClassUtils.class.getClassLoader()
305: .loadClass(name);
306: } catch (ClassNotFoundException ex) { /* deliberately empty */
307: }
308: }
309: return clas;
310: }
311:
312: /* (non-Javadoc)
313: * @see org.jibx.binding.model.IClass#getFields()
314: */
315: public IClassItem[] getFields() {
316: if (m_fields == null) {
317: ClassItem[] items = m_class.getFieldItems();
318: m_fields = new IClassItem[items.length];
319: for (int i = 0; i < items.length; i++) {
320: m_fields[i] = buildItem(items[i]);
321: }
322: }
323: return m_fields;
324: }
325:
326: /* (non-Javadoc)
327: * @see org.jibx.binding.model.IClass#getMethods()
328: */
329: public IClassItem[] getMethods() {
330: if (m_methods == null) {
331: ClassItem[] items = m_class.getMethodItems();
332: m_methods = new IClassItem[items.length];
333: for (int i = 0; i < items.length; i++) {
334: m_methods[i] = buildItem(items[i]);
335: }
336: }
337: return m_methods;
338: }
339:
340: /* (non-Javadoc)
341: * @see org.jibx.binding.model.IClass#getJavaDoc()
342: */
343: public String getJavaDoc() {
344: return null;
345: }
346:
347: /* (non-Javadoc)
348: * @see org.jibx.binding.model.IClass#getLocator()
349: */
350: public IClassLocator getLocator() {
351: return m_locator;
352: }
353: }
|