1/* 2 * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24import java.beans.IntrospectionException; 25import java.beans.Introspector; 26import java.beans.PropertyDescriptor; 27 28import java.lang.reflect.Array; 29import java.lang.reflect.Field; 30import java.lang.reflect.InvocationTargetException; 31import java.lang.reflect.Method; 32import java.lang.reflect.Modifier; 33 34import java.util.ArrayList; 35import java.util.Collection; 36import java.util.IdentityHashMap; 37import java.util.Iterator; 38import java.util.List; 39import java.util.Map; 40import java.util.Queue; 41import java.util.SortedMap; 42import java.util.SortedSet; 43 44final class BeanValidator { 45 private final Map<Object, Object> cache = new IdentityHashMap<Object, Object>(); 46 47 public void validate(Object object1, Object object2) { 48 // compare references 49 if (object1 == object2) { 50 return; 51 } 52 // check for null 53 if ((object1 == null) || (object2 == null)) { 54 throw new IllegalStateException("could not compare object with null"); 55 } 56 // resolve self references 57 if (isCyclic(object1, object2)) { 58 return; 59 } 60 // resolve cross references 61 if (isCyclic(object2, object1)) { 62 return; 63 } 64 Class type = object1.getClass(); 65 if (!type.equals(object2.getClass())) { 66 // resolve different implementations of the Map.Entry interface 67 if ((object1 instanceof Map.Entry) && (object2 instanceof Map.Entry)) { 68 log("!!! special case", "Map.Entry"); 69 Map.Entry entry1 = (Map.Entry) object1; 70 Map.Entry entry2 = (Map.Entry) object2; 71 validate(entry1.getKey(), entry2.getKey()); 72 validate(entry1.getValue(), entry2.getValue()); 73 return; 74 } 75 throw new IllegalStateException("could not compare objects with different types"); 76 } 77 // validate elements of arrays 78 if (type.isArray()) { 79 int length = Array.getLength(object1); 80 if (length != Array.getLength(object2)) { 81 throw new IllegalStateException("could not compare arrays with different lengths"); 82 } 83 try { 84 this.cache.put(object1, object2); 85 for (int i = 0; i < length; i++) { 86 log("validate array element", Integer.valueOf(i)); 87 validate(Array.get(object1, i), Array.get(object2, i)); 88 } 89 } finally { 90 this.cache.remove(object1); 91 } 92 return; 93 } 94 // special case for collections: do not use equals 95 boolean ignore = Collection.class.isAssignableFrom(type) 96 || Map.Entry.class.isAssignableFrom(type) 97 || Map.class.isAssignableFrom(type); 98 // validate objects using equals() 99 // we assume that the method equals(Object) can be called, 100 // if the class declares such method 101 if (!ignore && isDefined(type, "equals", Object.class)) { 102 if (object1.equals(object2)) { 103 return; 104 } 105 throw new IllegalStateException("the first object is not equal to the second one"); 106 } 107 // validate comparable objects using compareTo() 108 // we assume that the method compareTo(Object) can be called, 109 // if the class declares such method and implements interface Comparable 110 if (Comparable.class.isAssignableFrom(type) && isDefined(type, "compareTo", Object.class)) { 111 Comparable cmp = (Comparable) object1; 112 if (0 == cmp.compareTo(object2)) { 113 return; 114 } 115 throw new IllegalStateException("the first comparable object is not equal to the second one"); 116 } 117 try { 118 this.cache.put(object1, object2); 119 // validate values of public fields 120 for (Field field : getFields(type)) { 121 int mod = field.getModifiers(); 122 if (!Modifier.isStatic(mod)) { 123 log("validate field", field.getName()); 124 validate(object1, object2, field); 125 } 126 } 127 // validate values of properties 128 for (PropertyDescriptor pd : getDescriptors(type)) { 129 Method method = pd.getReadMethod(); 130 if (method != null) { 131 log("validate property", pd.getName()); 132 validate(object1, object2, method); 133 } 134 } 135 // validate contents of maps 136 if (SortedMap.class.isAssignableFrom(type)) { 137 validate((Map) object1, (Map) object2, true); 138 } else if (Map.class.isAssignableFrom(type)) { 139 validate((Map) object1, (Map) object2, false); 140 } 141 // validate contents of collections 142 if (SortedSet.class.isAssignableFrom(type)) { 143 validate((Collection) object1, (Collection) object2, true); 144 } else if (List.class.isAssignableFrom(type)) { 145 validate((Collection) object1, (Collection) object2, true); 146 } else if (Queue.class.isAssignableFrom(type)) { 147 validate((Collection) object1, (Collection) object2, true); 148 } else if (Collection.class.isAssignableFrom(type)) { 149 validate((Collection) object1, (Collection) object2, false); 150 } 151 } finally { 152 this.cache.remove(object1); 153 } 154 } 155 156 private void validate(Object object1, Object object2, Field field) { 157 try { 158 object1 = field.get(object1); 159 object2 = field.get(object2); 160 161 validate(object1, object2); 162 } 163 catch (IllegalAccessException exception) { 164 log(exception); 165 } 166 } 167 168 private void validate(Object object1, Object object2, Method method) { 169 try { 170 object1 = method.invoke(object1); 171 object2 = method.invoke(object2); 172 173 validate(object1, object2); 174 } 175 catch (IllegalAccessException exception) { 176 log(exception); 177 } 178 catch (InvocationTargetException exception) { 179 log(exception.getCause()); 180 } 181 } 182 183 private void validate(Collection c1, Collection c2, boolean sorted) { 184 if (c1.size() != c2.size()) { 185 throw new IllegalStateException("could not compare collections with different sizes"); 186 } 187 if (sorted) { 188 Iterator first = c1.iterator(); 189 Iterator second = c2.iterator(); 190 for (int i = 0; first.hasNext() && second.hasNext(); i++) { 191 log("validate collection element", Integer.valueOf(i)); 192 validate(first.next(), second.next()); 193 } 194 if (first.hasNext() || second.hasNext()) { 195 throw new IllegalStateException("one collection contains more elements than another one"); 196 } 197 } else { 198 List list = new ArrayList(c2); 199 Iterator first = c1.iterator(); 200 for (int i = 0; first.hasNext(); i++) { 201 Object value = first.next(); 202 log("validate collection element", Integer.valueOf(i)); 203 Iterator second = list.iterator(); 204 for (int j = 0; second.hasNext(); j++) { 205 log("validate collection element against", Integer.valueOf(j)); 206 try { 207 validate(value, second.next()); 208 second.remove(); 209 break; 210 } catch (IllegalStateException exception) { 211 if (!second.hasNext()) { 212 throw new IllegalStateException("one collection does not contain some elements from another one", exception); 213 } 214 } 215 } 216 } 217 } 218 } 219 220 private void validate(Map map1, Map map2, boolean sorted) { 221 validate(map1.entrySet(), map2.entrySet(), sorted); 222 } 223 224 private boolean isCyclic(Object object1, Object object2) { 225 Object object = this.cache.get(object1); 226 if (object == null) { 227 return false; 228 } 229 if (object == object2) { 230 return true; 231 } 232 throw new IllegalStateException("could not resolve cyclic reference"); 233 } 234 235 private boolean isDefined(Class type, String name, Class... params) { 236 try { 237 return type.equals(type.getMethod(name, params).getDeclaringClass()); 238 } 239 catch (NoSuchMethodException exception) { 240 log(exception); 241 } 242 catch (SecurityException exception) { 243 log(exception); 244 } 245 return false; 246 } 247 248 private static final Field[] FIELDS = {}; 249 250 private Field[] getFields(Class type) { 251 try { 252 return type.getFields(); 253 } 254 catch (SecurityException exception) { 255 log(exception); 256 } 257 return FIELDS; 258 } 259 260 private static final PropertyDescriptor[] DESCRIPTORS = {}; 261 262 private PropertyDescriptor[] getDescriptors(Class type) { 263 try { 264 return Introspector.getBeanInfo(type, Object.class).getPropertyDescriptors(); 265 } 266 catch (IntrospectionException exception) { 267 log(exception); 268 } 269 return DESCRIPTORS; 270 } 271 272 private final StringBuilder sb = new StringBuilder(1024); 273 274 private void log(String message, Object value) { 275 this.sb.setLength(0); 276 int size = this.cache.size(); 277 while (0 < size--) { 278 this.sb.append(" "); 279 } 280 this.sb.append(" - "); 281 this.sb.append(message); 282 if (value != null) { 283 this.sb.append(": "); 284 this.sb.append(value); 285 } 286 System.out.println(this.sb.toString()); 287 } 288 289 private void log(Throwable throwable) { 290 this.sb.setLength(0); 291 int size = this.cache.size(); 292 while (0 < size--) { 293 this.sb.append(" "); 294 } 295 this.sb.append(" ? "); 296 this.sb.append(throwable); 297 System.out.println(this.sb.toString()); 298 } 299} 300