KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > reflect > generic > GenericClass


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.reflect.generic;
22
23 import com.db4o.Platform4;
24 import com.db4o.foundation.DeepClone;
25 import com.db4o.reflect.ReflectClass;
26 import com.db4o.reflect.ReflectConstructor;
27 import com.db4o.reflect.ReflectField;
28 import com.db4o.reflect.ReflectMethod;
29 import com.db4o.reflect.Reflector;
30
31 /**
32  * @exclude
33  */

34 public class GenericClass implements ReflectClass, DeepClone {
35
36     private static final GenericField[] NO_FIELDS = new GenericField[0];
37     
38     private final GenericReflector _reflector;
39     private final ReflectClass _delegate;
40     
41     private final String JavaDoc _name;
42     private GenericClass _superclass;
43     
44     private GenericClass _array;
45     
46     private boolean _isSecondClass;
47     private boolean _isPrimitive;
48     
49     private int _isCollection;
50     
51     private GenericConverter _converter;
52     
53     private GenericField[] _fields = NO_FIELDS;
54     
55     private int _declaredFieldCount = -1;
56     private int _fieldCount = -1;
57     
58     private final int _hashCode;
59     
60
61     public GenericClass(GenericReflector reflector, ReflectClass delegateClass, String JavaDoc name, GenericClass superclass) {
62         _reflector = reflector;
63         _delegate = delegateClass;
64         _name = name;
65         _superclass = superclass;
66         _hashCode = _name.hashCode();
67     }
68     
69     public GenericClass arrayClass(){
70         if(_array != null){
71             return _array;
72         }
73         _array = new GenericArrayClass(_reflector, this, _name, _superclass);
74         _array._isSecondClass = _isSecondClass;
75         return _array;
76     }
77
78     public Object JavaDoc deepClone(Object JavaDoc obj) {
79         GenericReflector reflector = (GenericReflector)obj;
80         GenericClass superClass = null;
81         if(_superclass != null){
82             _superclass = (GenericClass)reflector.forName(_superclass.getName());
83         }
84         GenericClass ret = new GenericClass(reflector, _delegate, _name, superClass);
85         ret._isSecondClass = _isSecondClass;
86         GenericField[] fields = new GenericField[_fields.length];
87         for (int i = 0; i < fields.length; i++) {
88             fields[i] = (GenericField)_fields[i].deepClone(reflector);
89         }
90         ret.initFields(fields);
91         return ret;
92     }
93     
94     public boolean equals(Object JavaDoc obj) {
95         if(obj == null){
96             return false;
97         }
98         if(this == obj){
99             return true;
100         }
101         if(! (obj instanceof GenericClass)){
102             return false;
103         }
104         GenericClass otherGC = (GenericClass)obj;
105         if(_hashCode != otherGC.hashCode()){
106             return false;
107         }
108         return _name.equals(otherGC._name);
109     }
110     
111     public ReflectClass getComponentType() {
112         if(_delegate != null){
113             return _delegate.getComponentType();
114         }
115         return null;
116     }
117
118     public ReflectConstructor[] getDeclaredConstructors() {
119         if(_delegate != null){
120             return _delegate.getDeclaredConstructors();
121         }
122         return null;
123     }
124     
125     // TODO: consider that classes may have two fields of
126
// the same name after refactoring.
127

128     public ReflectField getDeclaredField(String JavaDoc name) {
129         if(_delegate != null){
130             return _delegate.getDeclaredField(name);
131         }
132         for (int i = 0; i < _fields.length; i++) {
133             if (_fields[i].getName().equals(name)) {
134                 return _fields[i];
135             }
136         }
137         return null;
138     }
139
140     public ReflectField[] getDeclaredFields() {
141         if(_delegate != null){
142             return _delegate.getDeclaredFields();
143         }
144         return _fields;
145     }
146     
147     public ReflectClass getDelegate(){
148         if(_delegate != null){
149             return _delegate;
150         }
151         return this;
152     }
153     
154     int getFieldCount() {
155         if(_fieldCount != -1) {
156             return _fieldCount;
157         }
158         _fieldCount = 0;
159         if(_superclass != null) {
160             _fieldCount = _superclass.getFieldCount();
161         }
162         if(_declaredFieldCount == -1) {
163             _declaredFieldCount = getDeclaredFields().length;
164         }
165         _fieldCount += _declaredFieldCount;
166         return _fieldCount;
167     }
168     
169     public ReflectMethod getMethod(String JavaDoc methodName, ReflectClass[] paramClasses) {
170         if(_delegate != null){
171             return _delegate.getMethod(methodName, paramClasses);
172         }
173         return null;
174     }
175
176     public String JavaDoc getName() {
177         return _name;
178     }
179
180     public ReflectClass getSuperclass() {
181         if(_superclass != null){
182             return _superclass;
183         }
184         if(_delegate == null){
185             return _reflector.forClass(Object JavaDoc.class);
186         }
187         ReflectClass delegateSuperclass = _delegate.getSuperclass();
188         if(delegateSuperclass != null){
189             _superclass = _reflector.ensureDelegate(delegateSuperclass);
190         }
191         return _superclass;
192     }
193     
194     public int hashCode() {
195         return _hashCode;
196     }
197
198     public void initFields(GenericField[] fields) {
199         int startIndex = 0;
200         if(_superclass != null) {
201             startIndex = _superclass.getFieldCount();
202         }
203         _fields = fields;
204         for (int i = 0; i < _fields.length; i++) {
205             _fields[i].setIndex(startIndex + i);
206         }
207     }
208
209      // TODO: Consider: Will this method still be necessary
210
// once constructor logic is pushed into the reflectors?
211
public boolean isAbstract() {
212         if(_delegate != null){
213             return _delegate.isAbstract();
214         }
215         return false;
216     }
217
218     public boolean isArray() {
219         if(_delegate != null){
220             return _delegate.isArray();
221         }
222         return false;
223     }
224
225     public boolean isAssignableFrom(ReflectClass subclassCandidate) {
226         if(subclassCandidate == null){
227             return false;
228         }
229         if (equals(subclassCandidate)) {
230             return true;
231         }
232         if(_delegate != null){
233             if( subclassCandidate instanceof GenericClass){
234                 subclassCandidate = ((GenericClass)subclassCandidate).getDelegate();
235             }
236             return _delegate.isAssignableFrom(subclassCandidate);
237         }
238         if (!(subclassCandidate instanceof GenericClass)) {
239             return false;
240         }
241         return isAssignableFrom(subclassCandidate.getSuperclass());
242     }
243
244     public boolean isCollection() {
245         if(_isCollection == 1){
246             return true;
247         }
248         if(_isCollection == -1){
249             return false;
250         }
251         _isCollection = _reflector.isCollection(this) ? 1 : -1;
252         return isCollection();
253     }
254     
255     public boolean isInstance(Object JavaDoc candidate) {
256         if(_delegate != null){
257             return _delegate.isInstance(candidate);
258         }
259         if (!(candidate instanceof GenericObject)) {
260             return false;
261         }
262         return isAssignableFrom(((GenericObject)candidate)._class);
263     }
264
265     public boolean isInterface() {
266         if(_delegate != null){
267             return _delegate.isInterface();
268         }
269         return false;
270     }
271
272     public boolean isPrimitive() {
273         if(_delegate != null){
274             return _delegate.isPrimitive();
275         }
276         return _isPrimitive;
277     }
278     
279     public boolean isSecondClass() {
280         if(isPrimitive()){
281             return true;
282         }
283         return _isSecondClass;
284     }
285     
286     public Object JavaDoc newInstance() {
287         if(_delegate != null){
288             return _delegate.newInstance();
289         }
290         return new GenericObject(this);
291     }
292
293     public Reflector reflector() {
294         if(_delegate != null){
295             return _delegate.reflector();
296         }
297         return _reflector;
298     }
299     
300     void setConverter (GenericConverter converter) {
301         _converter = converter;
302     }
303     
304     void setDeclaredFieldCount(int count) {
305         _declaredFieldCount = count;
306     }
307     
308     void setPrimitive() {
309         _isPrimitive = true;
310     }
311
312     void setSecondClass(){
313         _isSecondClass = true;
314     }
315     
316     public boolean skipConstructor(boolean flag){
317         if(_delegate != null){
318             return _delegate.skipConstructor(flag);
319         }
320         return false;
321     }
322     
323     public String JavaDoc toString(){
324         return "GenericClass " + _name;
325     }
326     
327     public String JavaDoc toString(GenericObject obj) {
328         if(_converter == null) {
329             return "(G) " + getName();
330         }
331         return _converter.toString(obj);
332     }
333
334     public void useConstructor(ReflectConstructor constructor, Object JavaDoc[] params){
335         if(_delegate != null){
336             _delegate.useConstructor(constructor, params);
337         }
338
339         // ignore, we always create a generic object
340
}
341     
342     public Object JavaDoc[] toArray(Object JavaDoc obj){
343         if(! isCollection()){
344             return new Object JavaDoc[]{obj};
345         }
346         return Platform4.collectionToArray(_reflector.getStream(), obj);
347     }
348
349 }
350
Popular Tags