KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > binding > model > ClassWrapper


1 /*
2 Copyright (c) 2004-2005, Dennis M. Sosnoski
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29 package org.jibx.binding.model;
30
31 import org.jibx.binding.classes.ClassFile;
32 import org.jibx.binding.classes.ClassItem;
33 import org.jibx.runtime.JiBXException;
34
35 /**
36  * Wrapper for class information. This wraps the BCEL-based class handling
37  * implementation to support the interface defined for use with the binding
38  * model.
39  *
40  * @author Dennis M. Sosnoski
41  * @version 1.0
42  */

43
44 public class ClassWrapper implements IClass
45 {
46     private final ClassFile m_class;
47     
48     public ClassWrapper(ClassFile clas) {
49         m_class = clas;
50     }
51     
52     /* (non-Javadoc)
53      * @see org.jibx.binding.model.IClass#getName()
54      */

55     public String JavaDoc getName() {
56         return m_class.getName();
57     }
58
59     /* (non-Javadoc)
60      * @see org.jibx.binding.model.IClass#getSignature()
61      */

62     public String JavaDoc getSignature() {
63         return m_class.getSignature();
64     }
65
66     /* (non-Javadoc)
67      * @see org.jibx.binding.model.IClass#getPackage()
68      */

69     public String JavaDoc getPackage() {
70         return m_class.getPackage();
71     }
72
73     /* (non-Javadoc)
74      * @see org.jibx.binding.model.IClass#getSuperClass()
75      */

76     public IClass getSuperClass() {
77         ClassFile scf = m_class.getSuperFile();
78         if (scf == null) {
79             return null;
80         } else {
81             return new ClassWrapper(m_class.getSuperFile());
82         }
83     }
84
85     /* (non-Javadoc)
86      * @see org.jibx.binding.model.IClass#getInterfaces()
87      */

88     public String JavaDoc[] getInterfaces() {
89         return m_class.getInterfaces();
90     }
91
92     /* (non-Javadoc)
93      * @see org.jibx.binding.model.IClass#getInstanceSigs()
94      */

95     public String JavaDoc[] getInstanceSigs() {
96         try {
97             return m_class.getInstanceSigs();
98         } catch (JiBXException e) {
99             // TODO need to handle this differently - perhaps get all when created
100
throw new IllegalStateException JavaDoc("Internal error: instance " +
101                 "signatures not found for class " + m_class.getName());
102         }
103     }
104
105     /* (non-Javadoc)
106      * @see org.jibx.binding.model.IClass#isImplements(java.lang.String)
107      */

108     public boolean isImplements(String JavaDoc sig) {
109         try {
110             return m_class.isImplements(sig);
111         } catch (JiBXException e) {
112             // TODO need to handle this differently - perhaps get all when created
113
throw new IllegalStateException JavaDoc("Internal error: instance " +
114                 "signatures not found for class " + m_class.getName());
115         }
116     }
117
118     /* (non-Javadoc)
119      * @see org.jibx.binding.model.IClass#isSuperclass(org.jibx.binding.model.IClass)
120      */

121     public boolean isSuperclass(String JavaDoc name) {
122         ClassFile current = m_class;
123         while (current != null) {
124             if (current.getName().equals(name)) {
125                 return true;
126             } else {
127                 current = current.getSuperFile();
128             }
129         }
130         return false;
131     }
132
133     /* (non-Javadoc)
134      * @see org.jibx.binding.model.IClass#getDirectField(java.lang.String)
135      */

136     public IClassItem getDirectField(String JavaDoc name) {
137         ClassItem item = m_class.getDirectField(name);
138         if (item == null) {
139             return null;
140         } else {
141             return new ClassItemWrapper(this, item);
142         }
143     }
144
145     /* (non-Javadoc)
146      * @see org.jibx.binding.model.IClass#getField(java.lang.String)
147      */

148     public IClassItem getField(String JavaDoc name) {
149         try {
150             return new ClassItemWrapper(this, m_class.getField(name));
151         } catch (JiBXException e) {
152             // TODO need to handle this differently - perhaps get all when created
153
return null;
154         }
155     }
156
157     /* (non-Javadoc)
158      * @see org.jibx.binding.model.IClass#getMethod(java.lang.String, java.lang.String)
159      */

160     public IClassItem getMethod(String JavaDoc name, String JavaDoc sig) {
161         ClassItem item = m_class.getMethod(name, sig);
162         if (item == null) {
163             return null;
164         } else {
165             return new ClassItemWrapper(this, item);
166         }
167     }
168
169     /* (non-Javadoc)
170      * @see org.jibx.binding.model.IClass#getMethod(java.lang.String, java.lang.String[])
171      */

172     public IClassItem getMethod(String JavaDoc name, String JavaDoc[] sigs) {
173         ClassItem item = m_class.getMethod(name, sigs);
174         if (item == null) {
175             return null;
176         } else {
177             return new ClassItemWrapper(this, item);
178         }
179     }
180
181     /* (non-Javadoc)
182      * @see org.jibx.binding.model.IClass#getInitializerMethod(java.lang.String)
183      */

184     public IClassItem getInitializerMethod(String JavaDoc sig) {
185         ClassItem item = m_class.getInitializerMethod(sig);
186         if (item == null) {
187             return null;
188         } else {
189             return new ClassItemWrapper(this, item);
190         }
191     }
192
193     /* (non-Javadoc)
194      * @see org.jibx.binding.model.IClass#getStaticMethod(java.lang.String, java.lang.String)
195      */

196     public IClassItem getStaticMethod(String JavaDoc name, String JavaDoc sig) {
197         ClassItem item = m_class.getStaticMethod(name, sig);
198         if (item == null) {
199             return null;
200         } else {
201             return new ClassItemWrapper(this, item);
202         }
203     }
204
205     /* (non-Javadoc)
206      * @see org.jibx.binding.model.IClass#isAccessible(org.jibx.binding.model.IClassItem)
207      */

208     public boolean isAccessible(IClassItem item) {
209         return m_class.isAccessible(((ClassItemWrapper)item).m_item);
210     }
211
212     /* (non-Javadoc)
213      * @see org.jibx.binding.model.IClass#isAssignable(org.jibx.binding.model.IClass)
214      */

215     public boolean isAssignable(IClass other) {
216         String JavaDoc[] sigs;
217         try {
218             sigs = m_class.getInstanceSigs();
219         } catch (JiBXException e) {
220             throw new IllegalStateException JavaDoc
221                 ("Internal error: class information not available");
222         }
223         String JavaDoc match = other.getSignature();
224         for (int i = 0; i < sigs.length; i++) {
225             if (match.equals(sigs[i])) {
226                 return true;
227             }
228         }
229         return false;
230     }
231
232     /* (non-Javadoc)
233      * @see org.jibx.binding.model.IClass#getBestMethod(java.lang.String, java.lang.String, java.lang.String[])
234      */

235     public IClassItem getBestMethod(String JavaDoc name, String JavaDoc type, String JavaDoc[] args) {
236         ClassItem item = m_class.getBestMethod(name, type, args);
237         if (item == null) {
238             return null;
239         } else {
240             return new ClassItemWrapper(this, item);
241         }
242     }
243
244     /* (non-Javadoc)
245      * @see org.jibx.binding.model.IClass#getClassFile()
246      * TODO: eliminate this method
247      */

248     public ClassFile getClassFile() {
249         return m_class;
250     }
251 }
Popular Tags