1 21 package com.db4o.test.lib; 22 23 24 import java.lang.reflect.*; 25 import java.util.*; 26 27 import com.db4o.*; 28 29 public class TCompare { 30 31 public boolean isEqual(Object a_compare, Object a_with) { 32 return isEqual(a_compare, a_with, null, null); 33 } 34 35 public boolean isEqual(Object a_compare, Object a_with, String a_path, Stack a_stack) { 36 if (a_path == null || a_path.length() < 1) { 37 if (a_compare != null) { 38 a_path = a_compare.getClass().getName() + ":"; 39 } else { 40 if (a_with != null) { 41 a_path = a_with.getClass().getName() + ":"; 42 } 43 } 44 } 45 46 String path = a_path; 47 48 if (a_compare == null) { 49 return a_with == null; 50 } 51 if (a_with == null) { 52 return false; 53 } 54 Class clazz = a_compare.getClass(); 55 if (clazz != a_with.getClass()) { 56 return false; 57 } 58 59 if (Platform4.isSimple(clazz)) { 60 return a_compare.equals(a_with); 61 } 62 63 if (a_stack == null) { 64 a_stack = new Stack(); 65 } 66 67 if (a_stack.contains(a_compare)) { 69 return true; 70 } 71 a_stack.push(a_compare); 72 73 Field fields[] = clazz.getDeclaredFields(); 74 for (int i = 0; i < fields.length; i++) { 75 if (storeableField(clazz, fields[i])) { 76 Platform4.setAccessible(fields[i]); 77 try { 78 path = a_path + fields[i].getName() + ":"; 79 Object compare = fields[i].get(a_compare); 80 Object with = fields[i].get(a_with); 81 if (compare == null) { 82 if (with != null) { 83 return false; 84 } 85 } else if (with == null) { 86 return false; 87 } else { 88 if (compare.getClass().isArray()) { 89 if (!with.getClass().isArray()) { 90 return false; 91 } else { 92 compare = normalizeNArray(compare); 93 with = normalizeNArray(with); 94 int len = Array.getLength(compare); 95 if (len != Array.getLength(with)) { 96 return false; 97 } else { 98 for (int j = 0; j < len; j++) { 99 Object elementCompare = Array.get(compare, j); 100 Object elementWith = Array.get(with, j); 101 if (!isEqual(elementCompare, elementWith, path, a_stack)) { 103 return false; 104 } else if (elementCompare == null) { 105 if (elementWith != null) { 106 return false; 107 } 108 } else if (elementWith == null) { 109 return false; 110 } else { 111 Class elementCompareClass = elementCompare.getClass(); 112 if (elementCompareClass != elementWith.getClass()) { 113 return false; 114 } 115 if (hasPublicConstructor(elementCompareClass)) { 116 if (!isEqual(elementCompare, 117 elementWith, 118 path, 119 a_stack)) { 120 return false; 121 122 } 123 } else if (!elementCompare.equals(elementWith)) { 124 return false; 125 } 126 } 127 128 } 129 130 } 131 } 132 } else if (hasPublicConstructor(fields[i].getType())) { 133 if (!isEqual(compare, with, path, a_stack)) { 134 return false; 135 } 136 } else { 137 if (!compare.equals(with)) { 138 return false; 139 } 140 } 141 } 142 } catch (IllegalAccessException ex) { 143 return true; 146 } catch (Exception e) { 147 System.err.println("TCompare failure executing path:" + path); 148 e.printStackTrace(); 149 return false; 150 } 151 } 152 } 153 return true; 154 } 155 156 boolean hasPublicConstructor(Class a_class) { 157 if (a_class != String .class) { 158 try { 159 return a_class.newInstance() != null; 160 } catch (Throwable t) { 161 } 162 } 163 return false; 164 } 165 166 Object normalizeNArray(Object a_object) { 167 if (Array.getLength(a_object) > 0) { 168 Object first = Array.get(a_object, 0); 169 if (first != null && first.getClass().isArray()) { 170 int dim[] = arrayDimensions(a_object); 171 Object all = new Object [arrayElementCount(dim)]; 172 normalizeNArray1(a_object, all, 0, dim, 0); 173 return all; 174 } 175 } 176 return a_object; 177 } 178 179 int normalizeNArray1(Object a_object, Object a_all, int a_next, int a_dim[], int a_index) { 180 if (a_index == a_dim.length - 1) { 181 for (int i = 0; i < a_dim[a_index]; i++) { 182 Array.set(a_all, a_next++, Array.get(a_object, i)); 183 } 184 } else { 185 for (int i = 0; i < a_dim[a_index]; i++) { 186 a_next = 187 normalizeNArray1(Array.get(a_object, i), a_all, a_next, a_dim, a_index + 1); 188 } 189 190 } 191 return a_next; 192 } 193 194 int[] arrayDimensions(Object a_object) { 195 int count = 0; 196 for (Class clazz = a_object.getClass(); 197 clazz.isArray(); 198 clazz = clazz.getComponentType()) { 199 count++; 200 } 201 int dim[] = new int[count]; 202 for (int i = 0; i < count; i++) { 203 dim[i] = Array.getLength(a_object); 204 a_object = Array.get(a_object, 0); 205 } 206 return dim; 207 } 208 209 int arrayElementCount(int a_dim[]) { 210 int elements = a_dim[0]; 211 for (int i = 1; i < a_dim.length; i++) { 212 elements *= a_dim[i]; 213 } 214 return elements; 215 } 216 217 public boolean storeableField(Class a_class, Field a_field) { 218 return (!Modifier.isStatic(a_field.getModifiers())) 219 && (!Modifier.isTransient(a_field.getModifiers()) 220 & !(a_field.getName().indexOf("$") > -1)); 221 } 222 223 224 } 225 | Popular Tags |