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