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.jdk;
022:
023: import java.lang.reflect.*;
024:
025: import com.db4o.internal.*;
026: import com.db4o.reflect.*;
027:
028: /**
029: * Reflection implementation for Class to map to JDK reflection.
030: */
031: public class JdkClass implements JavaReflectClass {
032:
033: private final Reflector _reflector;
034: private final Class _clazz;
035: private ReflectConstructor _constructor;
036: private Object[] _constructorParams;
037:
038: public JdkClass(Reflector reflector, Class clazz) {
039: if (reflector == null) {
040: throw new NullPointerException();
041: }
042: _reflector = reflector;
043: _clazz = clazz;
044: }
045:
046: public ReflectClass getComponentType() {
047: return _reflector.forClass(_clazz.getComponentType());
048: }
049:
050: public ReflectConstructor[] getDeclaredConstructors() {
051: Constructor[] constructors = _clazz.getDeclaredConstructors();
052: ReflectConstructor[] reflectors = new ReflectConstructor[constructors.length];
053: for (int i = 0; i < constructors.length; i++) {
054: reflectors[i] = new JdkConstructor(_reflector,
055: constructors[i]);
056: }
057: return reflectors;
058: }
059:
060: public ReflectField getDeclaredField(String name) {
061: try {
062: return new JdkField(_reflector, _clazz
063: .getDeclaredField(name));
064: } catch (Exception e) {
065: return null;
066: }
067: }
068:
069: public ReflectField[] getDeclaredFields() {
070: Field[] fields = _clazz.getDeclaredFields();
071: ReflectField[] reflectors = new ReflectField[fields.length];
072: for (int i = 0; i < reflectors.length; i++) {
073: reflectors[i] = new JdkField(_reflector, fields[i]);
074: }
075: return reflectors;
076: }
077:
078: public ReflectClass getDelegate() {
079: return this ;
080: }
081:
082: public ReflectMethod getMethod(String methodName,
083: ReflectClass[] paramClasses) {
084: try {
085: Method method = _clazz.getMethod(methodName, JdkReflector
086: .toNative(paramClasses));
087: if (method == null) {
088: return null;
089: }
090: return new JdkMethod(method, reflector());
091: } catch (Exception e) {
092: return null;
093: }
094: }
095:
096: public String getName() {
097: return _clazz.getName();
098: }
099:
100: public ReflectClass getSuperclass() {
101: return _reflector.forClass(_clazz.getSuperclass());
102: }
103:
104: public boolean isAbstract() {
105: return Modifier.isAbstract(_clazz.getModifiers());
106: }
107:
108: public boolean isArray() {
109: return _clazz.isArray();
110: }
111:
112: public boolean isAssignableFrom(ReflectClass type) {
113: if (!(type instanceof JavaReflectClass)) {
114: return false;
115: }
116: return _clazz.isAssignableFrom(JdkReflector.toNative(type));
117: }
118:
119: public boolean isCollection() {
120: return _reflector.isCollection(this );
121: }
122:
123: public boolean isInstance(Object obj) {
124: return _clazz.isInstance(obj);
125: }
126:
127: public boolean isInterface() {
128: return _clazz.isInterface();
129: }
130:
131: public boolean isPrimitive() {
132: return _clazz.isPrimitive();
133: }
134:
135: public boolean isSecondClass() {
136:
137: return isPrimitive();
138:
139: // TODO: Internal SecondClass needs testing with many test cases first.
140: // Not sure if the following could break Entry class.
141:
142: // return isPrimitive()||SecondClass.class.isAssignableFrom(_clazz);
143: }
144:
145: public Object newInstance() {
146: if (_constructor == null) {
147: return ReflectPlatform.createInstance(_clazz);
148: }
149: return _constructor.newInstance(_constructorParams);
150: }
151:
152: public Class getJavaClass() {
153: return _clazz;
154: }
155:
156: public Reflector reflector() {
157: return _reflector;
158: }
159:
160: public boolean skipConstructor(boolean skipConstructor,
161: boolean testConstructor) {
162: boolean useSerializableConstructor = false;
163: ReflectConstructor constructor = null;
164: if (skipConstructor) {
165: Constructor serializableConstructor = Platform4.jdk()
166: .serializableConstructor(_clazz);
167: if (serializableConstructor != null) {
168: JdkConstructor jdkConstructor = new JdkConstructor(
169: _reflector, serializableConstructor);
170: boolean serializableIsOk = true;
171: if (testConstructor) {
172: Object obj = jdkConstructor
173: .newInstance((Object[]) null);
174: serializableIsOk = (obj != null);
175: }
176: if (serializableIsOk) {
177: useSerializableConstructor = true;
178: constructor = jdkConstructor;
179: }
180: }
181: }
182: useConstructor(constructor, null);
183: return useSerializableConstructor;
184: }
185:
186: public void useConstructor(ReflectConstructor constructor,
187: Object[] params) {
188: this ._constructor = constructor;
189: _constructorParams = params;
190: }
191:
192: public Object[] toArray(Object obj) {
193: return null;
194: }
195: }
|