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