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: /**
024: * Contains the application-specific reflection information (that would
025: * be generated by a bytecode enhancer), as opposed to the 'generic'
026: * functionality contained in SelfReflector.
027: */
028: public abstract class SelfReflectionRegistry {
029:
030: private final static Class[] ARRAYTYPES = { int[].class,
031: long[].class, short[].class, char[].class, byte[].class,
032: boolean[].class, float[].class, double[].class,
033: String[].class };
034: private final static Class[] PRIMITIVES = { Integer.class,
035: Long.class, Short.class, Character.class, Byte.class,
036: Boolean.class, Float.class, Double.class, String.class };
037:
038: public boolean isPrimitive(Class clazz) {
039: for (int idx = 0; idx < PRIMITIVES.length; idx++) {
040: if (PRIMITIVES[idx].equals(clazz)) {
041: return true;
042: }
043: }
044: return false;
045: }
046:
047: public abstract ClassInfo infoFor(Class clazz);
048:
049: public Object arrayFor(Class clazz, int length) {
050: if (Integer.class.isAssignableFrom(clazz)
051: || int.class.isAssignableFrom(clazz)) {
052: return new int[length];
053: }
054: if (Long.class.isAssignableFrom(clazz)
055: || long.class.isAssignableFrom(clazz)) {
056: return new long[length];
057: }
058: if (Short.class.isAssignableFrom(clazz)
059: || short.class.isAssignableFrom(clazz)) {
060: return new short[length];
061: }
062: if (Boolean.class.isAssignableFrom(clazz)
063: || boolean.class.isAssignableFrom(clazz)) {
064: return new boolean[length];
065: }
066: if (Byte.class.isAssignableFrom(clazz)
067: || byte.class.isAssignableFrom(clazz)) {
068: return new byte[length];
069: }
070: if (Character.class.isAssignableFrom(clazz)
071: || char.class.isAssignableFrom(clazz)) {
072: return new char[length];
073: }
074: if (Float.class.isAssignableFrom(clazz)
075: || float.class.isAssignableFrom(clazz)) {
076: return new float[length];
077: }
078: if (Double.class.isAssignableFrom(clazz)
079: || double.class.isAssignableFrom(clazz)) {
080: return new double[length];
081: }
082: if (String.class.isAssignableFrom(clazz)) {
083: return new String[length];
084: }
085: return null;
086: }
087:
088: public Class componentType(Class clazz) {
089: for (int i = 0; i < ARRAYTYPES.length; i++) {
090: if (ARRAYTYPES[i].equals(clazz)) {
091: return PRIMITIVES[i];
092: }
093: }
094: return null;
095: }
096:
097: public int arrayLength(Object array) {
098: if (array instanceof boolean[]) {
099: return ((boolean[]) array).length;
100: }
101: if (array instanceof byte[]) {
102: return ((byte[]) array).length;
103: }
104: if (array instanceof short[]) {
105: return ((short[]) array).length;
106: }
107: if (array instanceof char[]) {
108: return ((char[]) array).length;
109: }
110: if (array instanceof int[]) {
111: return ((int[]) array).length;
112: }
113: if (array instanceof long[]) {
114: return ((long[]) array).length;
115: }
116: if (array instanceof float[]) {
117: return ((float[]) array).length;
118: }
119: if (array instanceof double[]) {
120: return ((double[]) array).length;
121: }
122: return 0;
123: }
124:
125: public void setArray(Object array, int index, Object element) {
126: if (array instanceof boolean[]) {
127: ((boolean[]) array)[index] = ((Boolean) element)
128: .booleanValue();
129: }
130: if (array instanceof byte[]) {
131: ((byte[]) array)[index] = ((Byte) element).byteValue();
132: }
133: if (array instanceof short[]) {
134: ((short[]) array)[index] = ((Short) element).shortValue();
135: }
136: if (array instanceof char[]) {
137: ((char[]) array)[index] = ((Character) element).charValue();
138: }
139: if (array instanceof int[]) {
140: ((int[]) array)[index] = ((Integer) element).intValue();
141: }
142: if (array instanceof long[]) {
143: ((long[]) array)[index] = ((Long) element).longValue();
144: }
145: if (array instanceof float[]) {
146: ((float[]) array)[index] = ((Float) element).floatValue();
147: }
148: if (array instanceof double[]) {
149: ((double[]) array)[index] = ((Double) element)
150: .doubleValue();
151: }
152: }
153:
154: public Object getArray(Object array, int index) {
155: if (array instanceof boolean[]) {
156: return new Boolean(((boolean[]) array)[index]);
157: }
158: if (array instanceof byte[]) {
159: return new Byte(((byte[]) array)[index]);
160: }
161: if (array instanceof short[]) {
162: return new Short(((short[]) array)[index]);
163: }
164: if (array instanceof char[]) {
165: return new Character(((char[]) array)[index]);
166: }
167: if (array instanceof int[]) {
168: return new Integer(((int[]) array)[index]);
169: }
170: if (array instanceof long[]) {
171: return new Long(((long[]) array)[index]);
172: }
173: if (array instanceof float[]) {
174: return new Float(((float[]) array)[index]);
175: }
176: if (array instanceof double[]) {
177: return new Double(((double[]) array)[index]);
178: }
179: return null;
180: }
181:
182: public int flattenArray(Object array, Object[] a_flat) {
183: if (array instanceof boolean[]) {
184: boolean[] shaped = (boolean[]) array;
185: for (int i = 0; i < shaped.length; i++) {
186: a_flat[i] = new Boolean(shaped[i]);
187: }
188: return shaped.length;
189: }
190: if (array instanceof byte[]) {
191: byte[] shaped = (byte[]) array;
192: for (int i = 0; i < shaped.length; i++) {
193: a_flat[i] = new Byte(shaped[i]);
194: }
195: return shaped.length;
196: }
197: if (array instanceof short[]) {
198: short[] shaped = (short[]) array;
199: for (int i = 0; i < shaped.length; i++) {
200: a_flat[i] = new Short(shaped[i]);
201: }
202: return shaped.length;
203: }
204: if (array instanceof char[]) {
205: char[] shaped = (char[]) array;
206: for (int i = 0; i < shaped.length; i++) {
207: a_flat[i] = new Character(shaped[i]);
208: }
209: return shaped.length;
210: }
211: if (array instanceof int[]) {
212: int[] shaped = (int[]) array;
213: for (int i = 0; i < shaped.length; i++) {
214: a_flat[i] = new Integer(shaped[i]);
215: }
216: return shaped.length;
217: }
218: if (array instanceof long[]) {
219: long[] shaped = (long[]) array;
220: for (int i = 0; i < shaped.length; i++) {
221: a_flat[i] = new Long(shaped[i]);
222: }
223: return shaped.length;
224: }
225: if (array instanceof float[]) {
226: float[] shaped = (float[]) array;
227: for (int i = 0; i < shaped.length; i++) {
228: a_flat[i] = new Float(shaped[i]);
229: }
230: return shaped.length;
231: }
232: if (array instanceof double[]) {
233: double[] shaped = (double[]) array;
234: for (int i = 0; i < shaped.length; i++) {
235: a_flat[i] = new Double(shaped[i]);
236: }
237: return shaped.length;
238: }
239: return 0;
240: }
241:
242: public int shapeArray(Object[] a_flat, Object array) {
243: if (array instanceof boolean[]) {
244: boolean[] shaped = (boolean[]) array;
245: for (int i = 0; i < shaped.length; i++) {
246: shaped[i] = ((Boolean) a_flat[i]).booleanValue();
247: }
248: return a_flat.length;
249: }
250: if (array instanceof byte[]) {
251: byte[] shaped = (byte[]) array;
252: for (int i = 0; i < shaped.length; i++) {
253: shaped[i] = ((Byte) a_flat[i]).byteValue();
254: }
255: return a_flat.length;
256: }
257: if (array instanceof short[]) {
258: short[] shaped = (short[]) array;
259: for (int i = 0; i < shaped.length; i++) {
260: shaped[i] = ((Short) a_flat[i]).shortValue();
261: }
262: return a_flat.length;
263: }
264: if (array instanceof char[]) {
265: char[] shaped = (char[]) array;
266: for (int i = 0; i < shaped.length; i++) {
267: shaped[i] = ((Character) a_flat[i]).charValue();
268: }
269: return a_flat.length;
270: }
271: if (array instanceof int[]) {
272: int[] shaped = (int[]) array;
273: for (int i = 0; i < shaped.length; i++) {
274: shaped[i] = ((Integer) a_flat[i]).intValue();
275: }
276: return a_flat.length;
277: }
278: if (array instanceof long[]) {
279: long[] shaped = (long[]) array;
280: for (int i = 0; i < shaped.length; i++) {
281: shaped[i] = ((Long) a_flat[i]).longValue();
282: }
283: return a_flat.length;
284: }
285: if (array instanceof float[]) {
286: float[] shaped = (float[]) array;
287: for (int i = 0; i < shaped.length; i++) {
288: shaped[i] = ((Float) a_flat[i]).floatValue();
289: }
290: return a_flat.length;
291: }
292: if (array instanceof double[]) {
293: double[] shaped = (double[]) array;
294: for (int i = 0; i < shaped.length; i++) {
295: shaped[i] = ((Double) a_flat[i]).doubleValue();
296: }
297: return a_flat.length;
298: }
299: return 0;
300: }
301: }
|