1 21 package com.db4o.test; 22 23 import java.lang.reflect.*; 24 25 import com.db4o.*; 26 import com.db4o.foundation.*; 27 28 public class Compare { 29 30 static boolean hasPublicConstructor(Class a_class) { 31 if (a_class == null || a_class == String .class) { 32 return false; 33 } 34 try { 35 Object o = a_class.newInstance(); 36 if (o != null) 37 return true; 38 } catch (Throwable t) { 39 } 40 return false; 41 } 42 43 static boolean isPrimitiveOrPrimitiveWrapper(Class clazz){ 44 if(clazz.isPrimitive()){ 45 return true; 46 } 47 return Platform4.isSimple(clazz); 48 } 49 50 static Object normalizeNArray(Object a_object) { 51 if (Array.getLength(a_object) > 0) { 52 Object first = Array.get(a_object, 0); 53 if (first != null && first.getClass().isArray()) { 54 int dim[] = arrayDimensions(a_object); 55 Object all = new Object [arrayElementCount(dim)]; 56 normalizeNArray1(a_object, all, 0, dim, 0); 57 return all; 58 } 59 } 60 return a_object; 61 } 62 63 public static void compare( 64 com.db4o.ObjectContainer a_con, 65 Object a_Compare, 66 Object a_With, 67 String a_path, 68 Collection4 a_list) { 69 if (!Regression.DEACTIVATE) { 70 a_con.activate(a_With, 1); 71 } 72 73 74 75 if (a_list == null) { 76 a_list = new Collection4(); 77 } 78 if (a_list.containsByIdentity(a_Compare)) { 80 return; 81 } 82 83 a_list.add(a_Compare); 84 85 if (a_path == null || a_path.length() < 1) 86 if (a_Compare != null) { 87 a_path = a_Compare.getClass().getName() + ":"; 88 } else { 89 if (a_With != null) 90 a_path = a_With.getClass().getName() + ":"; 91 } 92 String path = a_path; 93 if (a_Compare == null){ 94 if (a_With == null) { 95 return; 96 } 97 Regression.addError("1==null:" + path); 98 return; 99 } 100 if (a_With == null) { 101 Regression.addError("2==null:" + path); 102 return; 103 } 104 Class compareClass = a_Compare.getClass(); 105 if (!compareClass.isInstance(a_With)) { 106 Regression.addError( 107 "class!=:" + path + compareClass.getName() + ":" + a_With.getClass().getName()); 108 return; 109 } 110 if(isPrimitiveOrPrimitiveWrapper(compareClass)){ 111 if(a_Compare.equals(a_With)){ 112 return; 113 } 114 Regression.addError( 115 "class!=:" + path + compareClass.getName() + ":" + a_With.getClass().getName()); 116 return; 117 } 118 119 Field l_Fields[] = compareClass.getDeclaredFields(); 120 for (int i = 0; i < l_Fields.length; i++) { 121 if (storeableField(compareClass, l_Fields[i])) { 122 Platform4.setAccessible(l_Fields[i]); 123 try { 124 path = a_path + l_Fields[i].getName() + ":"; 125 Object l_Compare = l_Fields[i].get(a_Compare); 126 Object l_With = l_Fields[i].get(a_With); 127 if (l_Compare == null) { 128 if (l_With != null) { 129 Regression.addError("f1==null:" + path); 130 } 131 } else if (l_With == null) 132 Regression.addError("f2==null:" + path); 133 else if (l_Compare.getClass().isArray()) { 134 if (!l_With.getClass().isArray()) { 135 Regression.addError("f2!=array:" + path); 136 } else { 137 l_Compare = normalizeNArray(l_Compare); 138 l_With = normalizeNArray(l_With); 139 int l_len = Array.getLength(l_Compare); 140 if (l_len != Array.getLength(l_With)) { 141 Regression.addError("arraylen!=:" + path); 142 } else { 143 Class fieldClass = l_Fields[i].getType().getComponentType(); 144 boolean l_persistentArray = 145 hasPublicConstructor(fieldClass) || 146 isPrimitiveOrPrimitiveWrapper(fieldClass); 147 for (int j = 0; j < l_len; j++) { 148 Object l_ElementCompare = Array.get(l_Compare, j); 149 Object l_ElementWith = Array.get(l_With, j); 150 if (l_persistentArray){ 151 compare( 152 a_con, 153 l_ElementCompare, 154 l_ElementWith, 155 path, 156 a_list); 157 } else if (l_ElementCompare == null) { 158 if (l_ElementWith != null){ 159 Regression.addError("1e" + j + "==null:" + path); 160 } 161 } else if (l_ElementWith == null) { 162 Regression.addError("2e" + j + "==null:" + path); 163 } else { 164 Class elementCompareClass = l_ElementCompare.getClass(); 165 if (elementCompareClass != l_ElementWith.getClass()){ 166 Regression.addError( 167 "e" 168 + j 169 + "!=class:" 170 + path 171 + elementCompareClass.toString() 172 + ":" 173 + l_ElementWith.getClass().toString()); 174 }else if (hasPublicConstructor(elementCompareClass)) { 175 compare( 176 a_con, 177 l_ElementCompare, 178 l_ElementWith, 179 path, 180 a_list); 181 } else { 182 if (!l_ElementCompare.equals(l_ElementWith)) 183 Regression.addError( 184 "e" 185 + j 186 + "!=:" 187 + path 188 + l_ElementCompare.toString() 189 + ":" 190 + l_ElementWith.toString()); 191 } 192 } 193 } 194 195 } 196 } 197 } else if (hasPublicConstructor(l_Fields[i].getType())) 198 compare(a_con, l_Compare, l_With, path, a_list); 199 else if (!l_Compare.equals(l_With)) 200 Regression.addError("!=:" + path); 201 } catch (Exception e) { 202 Regression.addError("Exception:" + path); 203 } 204 } 205 } 206 } 207 208 static int[] arrayDimensions(Object a_object) { 209 int count = 0; 210 for (Class clazz = a_object.getClass(); clazz.isArray(); clazz = clazz.getComponentType()) 211 count++; 212 213 int dim[] = new int[count]; 214 for (int i = 0; i < count; i++) { 215 dim[i] = Array.getLength(a_object); 216 a_object = Array.get(a_object, 0); 217 } 218 219 return dim; 220 } 221 222 static int normalizeNArray1( 223 Object a_object, 224 Object a_all, 225 int a_next, 226 int a_dim[], 227 int a_index) { 228 if (a_index == a_dim.length - 1) { 229 for (int i = 0; i < a_dim[a_index]; i++) 230 Array.set(a_all, a_next++, Array.get(a_object, i)); 231 232 } else { 233 for (int i = 0; i < a_dim[a_index]; i++) 234 a_next = 235 normalizeNArray1(Array.get(a_object, i), a_all, a_next, a_dim, a_index + 1); 236 237 } 238 return a_next; 239 } 240 241 static int arrayElementCount(int a_dim[]) { 242 int elements = a_dim[0]; 243 for (int i = 1; i < a_dim.length; i++) 244 elements *= a_dim[i]; 245 246 return elements; 247 } 248 249 static String nl() { 250 return System.getProperty("line.separator"); 251 } 252 253 public static boolean storeableField(Class a_class, Field a_field) { 254 return (!Modifier.isStatic(a_field.getModifiers())) 255 && (!Modifier.isTransient(a_field.getModifiers()) 256 & !(a_field.getName().indexOf("$") > -1)); 257 } 258 259 } 260 | Popular Tags |