1 21 package com.db4o.reflect.self; 22 23 28 public abstract class SelfReflectionRegistry { 29 30 private final static Class [] ARRAYTYPES={int[].class,long[].class,short[].class,char[].class,byte[].class,boolean[].class,float[].class,double[].class,String [].class}; 31 private final static Class [] PRIMITIVES={Integer .class,Long .class,Short .class,Character .class,Byte .class,Boolean .class,Float .class,Double .class,String .class}; 32 33 public boolean isPrimitive(Class clazz) { 34 for (int idx = 0; idx < PRIMITIVES.length; idx++) { 35 if(PRIMITIVES[idx].equals(clazz)) { 36 return true; 37 } 38 } 39 return false; 40 } 41 42 public abstract ClassInfo infoFor(Class clazz); 43 44 public Object arrayFor(Class clazz,int length) { 45 if(Integer .class.isAssignableFrom(clazz)||int.class.isAssignableFrom(clazz)) { 46 return new int[length]; 47 } 48 if(Long .class.isAssignableFrom(clazz)||long.class.isAssignableFrom(clazz)) { 49 return new long[length]; 50 } 51 if(Short .class.isAssignableFrom(clazz)||short.class.isAssignableFrom(clazz)) { 52 return new short[length]; 53 } 54 if(Boolean .class.isAssignableFrom(clazz)||boolean.class.isAssignableFrom(clazz)) { 55 return new boolean[length]; 56 } 57 if(Byte .class.isAssignableFrom(clazz)||byte.class.isAssignableFrom(clazz)) { 58 return new byte[length]; 59 } 60 if(Character .class.isAssignableFrom(clazz)||char.class.isAssignableFrom(clazz)) { 61 return new char[length]; 62 } 63 if(Float .class.isAssignableFrom(clazz)||float.class.isAssignableFrom(clazz)) { 64 return new float[length]; 65 } 66 if(Double .class.isAssignableFrom(clazz)||double.class.isAssignableFrom(clazz)) { 67 return new double[length]; 68 } 69 if(String .class.isAssignableFrom(clazz)) { 70 return new String [length]; 71 } 72 return null; 73 } 74 75 public Class componentType(Class clazz) { 76 for (int i = 0; i < ARRAYTYPES.length; i++) { 77 if(ARRAYTYPES[i].equals(clazz)) { 78 return PRIMITIVES[i]; 79 } 80 } 81 return null; 82 } 83 84 public int arrayLength(Object array) { 85 if(array instanceof boolean[]) { 86 return ((boolean[])array).length; 87 } 88 if(array instanceof byte[]) { 89 return ((byte[])array).length; 90 } 91 if(array instanceof short[]) { 92 return ((short[])array).length; 93 } 94 if(array instanceof char[]) { 95 return ((char[])array).length; 96 } 97 if(array instanceof int[]) { 98 return ((int[])array).length; 99 } 100 if(array instanceof long[]) { 101 return ((long[])array).length; 102 } 103 if(array instanceof float[]) { 104 return ((float[])array).length; 105 } 106 if(array instanceof double[]) { 107 return ((double[])array).length; 108 } 109 return 0; 110 } 111 112 public void setArray(Object array, int index, Object element) { 113 if(array instanceof boolean[]) { 114 ((boolean[])array)[index]=((Boolean )element).booleanValue(); 115 } 116 if(array instanceof byte[]) { 117 ((byte[])array)[index]=((Byte )element).byteValue(); 118 } 119 if(array instanceof short[]) { 120 ((short[])array)[index]=((Short )element).shortValue(); 121 } 122 if(array instanceof char[]) { 123 ((char[])array)[index]=((Character )element).charValue(); 124 } 125 if(array instanceof int[]) { 126 ((int[])array)[index]=((Integer )element).intValue(); 127 } 128 if(array instanceof long[]) { 129 ((long[])array)[index]=((Long )element).longValue(); 130 } 131 if(array instanceof float[]) { 132 ((float[])array)[index]=((Float )element).floatValue(); 133 } 134 if(array instanceof double[]) { 135 ((double[])array)[index]=((Double )element).doubleValue(); 136 } 137 } 138 139 public Object getArray(Object array, int index) { 140 if(array instanceof boolean[]) { 141 return new Boolean (((boolean[])array)[index]); 142 } 143 if(array instanceof byte[]) { 144 return new Byte (((byte[])array)[index]); 145 } 146 if(array instanceof short[]) { 147 return new Short (((short[])array)[index]); 148 } 149 if(array instanceof char[]) { 150 return new Character (((char[])array)[index]); 151 } 152 if(array instanceof int[]) { 153 return new Integer (((int[])array)[index]); 154 } 155 if(array instanceof long[]) { 156 return new Long (((long[])array)[index]); 157 } 158 if(array instanceof float[]) { 159 return new Float (((float[])array)[index]); 160 } 161 if(array instanceof double[]) { 162 return new Double (((double[])array)[index]); 163 } 164 return null; 165 } 166 167 public int flattenArray(Object array, Object [] a_flat) { 168 if(array instanceof boolean[]) { 169 boolean[] shaped=(boolean[])array; 170 for (int i = 0; i < shaped.length; i++) { 171 a_flat[i]=new Boolean (shaped[i]); 172 } 173 return shaped.length; 174 } 175 if(array instanceof byte[]) { 176 byte[] shaped=(byte[])array; 177 for (int i = 0; i < shaped.length; i++) { 178 a_flat[i]=new Byte (shaped[i]); 179 } 180 return shaped.length; 181 } 182 if(array instanceof short[]) { 183 short[] shaped=(short[])array; 184 for (int i = 0; i < shaped.length; i++) { 185 a_flat[i]=new Short (shaped[i]); 186 } 187 return shaped.length; 188 } 189 if(array instanceof char[]) { 190 char[] shaped=(char[])array; 191 for (int i = 0; i < shaped.length; i++) { 192 a_flat[i]=new Character (shaped[i]); 193 } 194 return shaped.length; 195 } 196 if(array instanceof int[]) { 197 int[] shaped=(int[])array; 198 for (int i = 0; i < shaped.length; i++) { 199 a_flat[i]=new Integer (shaped[i]); 200 } 201 return shaped.length; 202 } 203 if(array instanceof long[]) { 204 long[] shaped=(long[])array; 205 for (int i = 0; i < shaped.length; i++) { 206 a_flat[i]=new Long (shaped[i]); 207 } 208 return shaped.length; 209 } 210 if(array instanceof float[]) { 211 float[] shaped=(float[])array; 212 for (int i = 0; i < shaped.length; i++) { 213 a_flat[i]=new Float (shaped[i]); 214 } 215 return shaped.length; 216 } 217 if(array instanceof double[]) { 218 double[] shaped=(double[])array; 219 for (int i = 0; i < shaped.length; i++) { 220 a_flat[i]=new Double (shaped[i]); 221 } 222 return shaped.length; 223 } 224 return 0; 225 } 226 227 public int shapeArray(Object [] a_flat, Object array) { 228 if(array instanceof boolean[]) { 229 boolean[] shaped=(boolean[])array; 230 for (int i = 0; i < shaped.length; i++) { 231 shaped[i]=((Boolean )a_flat[i]).booleanValue(); 232 } 233 return a_flat.length; 234 } 235 if(array instanceof byte[]) { 236 byte[] shaped=(byte[])array; 237 for (int i = 0; i < shaped.length; i++) { 238 shaped[i]=((Byte )a_flat[i]).byteValue(); 239 } 240 return a_flat.length; 241 } 242 if(array instanceof short[]) { 243 short[] shaped=(short[])array; 244 for (int i = 0; i < shaped.length; i++) { 245 shaped[i]=((Short )a_flat[i]).shortValue(); 246 } 247 return a_flat.length; 248 } 249 if(array instanceof char[]) { 250 char[] shaped=(char[])array; 251 for (int i = 0; i < shaped.length; i++) { 252 shaped[i]=((Character )a_flat[i]).charValue(); 253 } 254 return a_flat.length; 255 } 256 if(array instanceof int[]) { 257 int[] shaped=(int[])array; 258 for (int i = 0; i < shaped.length; i++) { 259 shaped[i]=((Integer )a_flat[i]).intValue(); 260 } 261 return a_flat.length; 262 } 263 if(array instanceof long[]) { 264 long[] shaped=(long[])array; 265 for (int i = 0; i < shaped.length; i++) { 266 shaped[i]=((Long )a_flat[i]).longValue(); 267 } 268 return a_flat.length; 269 } 270 if(array instanceof float[]) { 271 float[] shaped=(float[])array; 272 for (int i = 0; i < shaped.length; i++) { 273 shaped[i]=((Float )a_flat[i]).floatValue(); 274 } 275 return a_flat.length; 276 } 277 if(array instanceof double[]) { 278 double[] shaped=(double[])array; 279 for (int i = 0; i < shaped.length; i++) { 280 shaped[i]=((Double )a_flat[i]).doubleValue(); 281 } 282 return a_flat.length; 283 } 284 return 0; 285 } 286 } 287 | Popular Tags |