001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.reflect.self;
022:
023: import com.db4o.reflect.*;
024:
025: public class SelfClass implements ReflectClass {
026: private static final SelfField[] EMPTY_FIELDS = new SelfField[0];
027:
028: private boolean _isAbstract;
029:
030: private SelfField[] _fields;
031:
032: private Reflector _parentReflector;
033:
034: private SelfReflectionRegistry _registry;
035:
036: private Class _class;
037:
038: private Class _super Class;
039:
040: // public SelfClass() {
041: // super();
042: // }
043:
044: public SelfClass(Reflector parentReflector,
045: SelfReflectionRegistry registry, Class clazz) {
046: _parentReflector = parentReflector;
047: _registry = registry;
048: _class = clazz;
049: }
050:
051: // TODO: Is this needed at all?
052: public Class getJavaClass() {
053: return _class;
054: }
055:
056: public Reflector reflector() {
057: return _parentReflector;
058: }
059:
060: public ReflectClass getComponentType() {
061: if (!isArray()) {
062: return null;
063: }
064: return _parentReflector.forClass(_registry
065: .componentType(_class));
066: }
067:
068: public ReflectConstructor[] getDeclaredConstructors() {
069: if (isInterface()) {
070: return new SelfConstructor[0];
071: }
072: return new SelfConstructor[] { new SelfConstructor(_class) };
073: }
074:
075: public ReflectField[] getDeclaredFields() {
076: ensureClassInfoLoaded();
077: return _fields;
078: }
079:
080: private void ensureClassInfoLoaded() {
081: if (_fields == null) {
082: ClassInfo classInfo = _registry.infoFor(_class);
083: if (classInfo == null) {
084: _fields = EMPTY_FIELDS;
085: return;
086: }
087: _super Class = classInfo.super Class();
088: _isAbstract = classInfo.isAbstract();
089: FieldInfo[] fieldInfo = classInfo.fieldInfo();
090: if (fieldInfo == null) {
091: _fields = EMPTY_FIELDS;
092: return;
093: }
094: _fields = new SelfField[fieldInfo.length];
095: for (int idx = 0; idx < fieldInfo.length; idx++) {
096: _fields[idx] = selfFieldFor(fieldInfo[idx]);
097: }
098: }
099: }
100:
101: public ReflectField getDeclaredField(String name) {
102: ensureClassInfoLoaded();
103: for (int idx = 0; idx < _fields.length; idx++) {
104: if (_fields[idx].getName().equals(name)) {
105: return _fields[idx];
106: }
107: }
108: return null;
109: }
110:
111: private SelfField selfFieldFor(FieldInfo fieldInfo) {
112: return new SelfField(fieldInfo.name(), _parentReflector
113: .forClass(fieldInfo.type()), this , _registry);
114: }
115:
116: public ReflectClass getDelegate() {
117: return this ;
118: }
119:
120: public ReflectMethod getMethod(String methodName,
121: ReflectClass[] paramClasses) {
122: // TODO !!!!
123: return null;
124: }
125:
126: public String getName() {
127: return _class.getName();
128: }
129:
130: public ReflectClass getSuperclass() {
131: ensureClassInfoLoaded();
132: if (_super Class == null) {
133: return null;
134: }
135: return _parentReflector.forClass(_super Class);
136: }
137:
138: public boolean isAbstract() {
139: ensureClassInfoLoaded();
140: return _isAbstract || isInterface();
141: }
142:
143: public boolean isArray() {
144: return _class.isArray();
145: }
146:
147: public boolean isAssignableFrom(ReflectClass type) {
148: if (!(type instanceof SelfClass)) {
149: return false;
150: }
151: return _class.isAssignableFrom(((SelfClass) type)
152: .getJavaClass());
153: }
154:
155: public boolean isCollection() {
156: return _parentReflector.isCollection(this );
157: }
158:
159: public boolean isInstance(Object obj) {
160: return _class.isInstance(obj);
161: }
162:
163: public boolean isInterface() {
164: return _class.isInterface();
165: }
166:
167: public boolean isPrimitive() {
168: return _registry.isPrimitive(_class);
169: }
170:
171: public boolean isSecondClass() {
172: return isPrimitive();
173: }
174:
175: public Object newInstance() {
176: try {
177: return _class.newInstance();
178: } catch (Exception e) {
179: e.printStackTrace();
180: }
181:
182: // Specialized exceptions break conversion to .NET
183:
184: //
185: //
186: //
187: // } catch (InstantiationException e) {
188: // e.printStackTrace();
189: // } catch (IllegalAccessException e) {
190: // e.printStackTrace();
191: // }
192:
193: return null;
194: }
195:
196: public boolean skipConstructor(boolean flag, boolean testConstructor) {
197: // cannot skip constructors, only available for JDK1.4+
198: return false;
199: }
200:
201: public void useConstructor(ReflectConstructor constructor,
202: Object[] params) {
203: // ignore, there must be a public no-args constructor suitable for
204: // Class.newInstance()
205: }
206:
207: // FIXME: remove. Reintroduced since OM depends on it - refactor OM.
208: public Object[] toArray(Object obj) {
209: return null;
210: }
211:
212: }
|