KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > reflect > self > SelfClass


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.self;
22
23 import com.db4o.reflect.*;
24
25 public class SelfClass implements ReflectClass {
26     private static final SelfField[] EMPTY_FIELDS = new SelfField[0];
27
28     private boolean _isAbstract;
29
30     private SelfField[] _fields;
31
32     private Reflector _parentReflector;
33
34     private SelfReflectionRegistry _registry;
35
36     private Class JavaDoc _class;
37
38     private Class JavaDoc _superClass;
39
40     // public SelfClass() {
41
// super();
42
// }
43

44     public SelfClass(Reflector parentReflector,
45             SelfReflectionRegistry registry, Class JavaDoc clazz) {
46         _parentReflector = parentReflector;
47         _registry = registry;
48         _class = clazz;
49     }
50
51     // TODO: Is this needed at all?
52
public Class JavaDoc getJavaClass() {
53         return _class;
54     }
55
56     public Reflector reflector() {
57         return _parentReflector;
58     }
59
60     public ReflectClass getComponentType() {
61         if (!isArray()) {
62             return null;
63         }
64         return _parentReflector.forClass(_registry.componentType(_class));
65     }
66
67     public ReflectConstructor[] getDeclaredConstructors() {
68         if (isInterface()) {
69             return new SelfConstructor[0];
70         }
71         return new SelfConstructor[] { new SelfConstructor(_class) };
72     }
73
74     public ReflectField[] getDeclaredFields() {
75         ensureClassInfoLoaded();
76         return _fields;
77     }
78
79     private void ensureClassInfoLoaded() {
80         if (_fields == null) {
81             ClassInfo classInfo = _registry.infoFor(_class);
82             if (classInfo == null) {
83                 _fields = EMPTY_FIELDS;
84                 return;
85             }
86             _superClass = classInfo.superClass();
87             _isAbstract = classInfo.isAbstract();
88             FieldInfo[] fieldInfo = classInfo.fieldInfo();
89             if (fieldInfo == null) {
90                 _fields = EMPTY_FIELDS;
91                 return;
92             }
93             _fields = new SelfField[fieldInfo.length];
94             for (int idx = 0; idx < fieldInfo.length; idx++) {
95                 _fields[idx] = selfFieldFor(fieldInfo[idx]);
96             }
97         }
98     }
99
100     public ReflectField getDeclaredField(String JavaDoc name) {
101         ensureClassInfoLoaded();
102         for (int idx = 0; idx < _fields.length; idx++) {
103             if (_fields[idx].getName().equals(name)) {
104                 return _fields[idx];
105             }
106         }
107         return null;
108     }
109
110     private SelfField selfFieldFor(FieldInfo fieldInfo) {
111         return new SelfField(fieldInfo.name(), _parentReflector
112                 .forClass(fieldInfo.type()), this, _registry);
113     }
114
115     public ReflectClass getDelegate() {
116         return this;
117     }
118
119     public ReflectMethod getMethod(String JavaDoc methodName,
120             ReflectClass[] paramClasses) {
121         // TODO !!!!
122
return null;
123     }
124
125     public String JavaDoc getName() {
126         return _class.getName();
127     }
128
129     public ReflectClass getSuperclass() {
130         ensureClassInfoLoaded();
131         if (_superClass == null) {
132             return null;
133         }
134         return _parentReflector.forClass(_superClass);
135     }
136
137     public boolean isAbstract() {
138         ensureClassInfoLoaded();
139         return _isAbstract || isInterface();
140     }
141
142     public boolean isArray() {
143         return _class.isArray();
144     }
145
146     public boolean isAssignableFrom(ReflectClass type) {
147         if (!(type instanceof SelfClass)) {
148             return false;
149         }
150         return _class.isAssignableFrom(((SelfClass) type).getJavaClass());
151     }
152
153     public boolean isCollection() {
154         return _parentReflector.isCollection(this);
155     }
156
157     public boolean isInstance(Object JavaDoc obj) {
158         return _class.isInstance(obj);
159     }
160
161     public boolean isInterface() {
162         return _class.isInterface();
163     }
164
165     public boolean isPrimitive() {
166         return _registry.isPrimitive(_class);
167     }
168
169     public boolean isSecondClass() {
170         return isPrimitive();
171     }
172
173     public Object JavaDoc newInstance() {
174         try {
175             return _class.newInstance();
176         } catch (Exception JavaDoc e) {
177             e.printStackTrace();
178         }
179
180         // Specialized exceptions break conversion to .NET
181

182         //
183
//
184
//
185
// } catch (InstantiationException e) {
186
// e.printStackTrace();
187
// } catch (IllegalAccessException e) {
188
// e.printStackTrace();
189
// }
190

191         return null;
192     }
193
194     public boolean skipConstructor(boolean flag) {
195         // cannot skip constructors, only available for JDK1.4+
196
return false;
197     }
198
199     public void useConstructor(ReflectConstructor constructor, Object JavaDoc[] params) {
200         // ignore, there must be a public no-args constructor suitable for
201
// Class.newInstance()
202
}
203
204     // FIXME: remove. Reintroduced since OM depends on it - refactor OM.
205
public Object JavaDoc[] toArray(Object JavaDoc obj) {
206         return null;
207     }
208
209 }
210
Popular Tags