File: MXBeanTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (773 lines) | stat: -rw-r--r-- 29,760 bytes parent folder | download | duplicates (16)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
/*
 * Copyright (c) 2005, 2015, 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.
 */

/*
 * @test
 * @bug 6175517 6278707 6318827 6305746 6392303 6600709 8010285
 * @summary General MXBean test.
 * @author Eamonn McManus
 * @author Jaroslav Bachorik
 * @modules java.management.rmi
 * @run clean MXBeanTest MerlinMXBean TigerMXBean
 * @run build MXBeanTest MerlinMXBean TigerMXBean
 * @run main MXBeanTest
 */

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import javax.management.JMX;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerFactory;
import javax.management.MBeanServerInvocationHandler;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.StandardMBean;
import javax.management.openmbean.ArrayType;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataInvocationHandler;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;
import javax.management.openmbean.TabularData;
import javax.management.openmbean.TabularType;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

public class MXBeanTest {
    public static void main(String[] args) throws Exception {
        testInterface(MerlinMXBean.class, false);
        testInterface(TigerMXBean.class, false);
        testInterface(MerlinMXBean.class, true);
        testInterface(TigerMXBean.class, true);
        testExplicitMXBean();
        testSubclassMXBean();
        testIndirectMXBean();
        testNonCompliantMXBean("Private", new Private());
        testNonCompliantMXBean("NonCompliant", new NonCompliant());

        if (failures == 0)
            System.out.println("Test passed");
        else
            throw new Exception("TEST FAILURES: " + failures);
    }

    private static int failures = 0;

    private static interface PrivateMXBean {
        public int[] getInts();
    }

    public static class Private implements PrivateMXBean {
        public int[] getInts() {
            return new int[]{1,2,3};
        }
    }

    public static interface NonCompliantMXBean {
        public boolean getInt();
        public boolean isInt();
        public void setInt(int a);
        public void setInt(long b);
    }

    public static class NonCompliant implements NonCompliantMXBean {
        public boolean getInt() {
            return false;
        }

        public boolean isInt() {
            return true;
        }

        public void setInt(int a) {
        }

        public void setInt(long b) {
        }
    }

    public static interface ExplicitMXBean {
        public int[] getInts();
    }
    public static class Explicit implements ExplicitMXBean {
        public int[] getInts() {
            return new int[] {1, 2, 3};
        }
    }
    public static class Subclass
        extends StandardMBean
        implements ExplicitMXBean {
        public Subclass() {
            super(ExplicitMXBean.class, true);
        }

        public int[] getInts() {
            return new int[] {1, 2, 3};
        }
    }
    public static interface IndirectInterface extends ExplicitMXBean {}
    public static class Indirect implements IndirectInterface {
        public int[] getInts() {
            return new int[] {1, 2, 3};
        }
    }

    private static void testNonCompliantMXBean(String type, Object bean) throws Exception {
        System.out.println(type + " MXBean test...");
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName on = new ObjectName("test:type=" + type);
        try {
            mbs.registerMBean(bean, on);
            failure(bean.getClass().getInterfaces()[0].getName() + " is not a compliant "
                + "MXBean interface");
        } catch (NotCompliantMBeanException e) {
            success("Non-compliant MXBean not registered");
        }
    }

    private static void testExplicitMXBean() throws Exception {
        System.out.println("Explicit MXBean test...");
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName on = new ObjectName("test:type=Explicit");
        Explicit explicit = new Explicit();
        mbs.registerMBean(explicit, on);
        testMXBean(mbs, on);
    }

    private static void testSubclassMXBean() throws Exception {
        System.out.println("Subclass MXBean test...");
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName on = new ObjectName("test:type=Subclass");
        Subclass subclass = new Subclass();
        mbs.registerMBean(subclass, on);
        testMXBean(mbs, on);
    }

    private static void testIndirectMXBean() throws Exception {
        System.out.println("Indirect MXBean test...");
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName on = new ObjectName("test:type=Indirect");
        Indirect indirect = new Indirect();
        mbs.registerMBean(indirect, on);
        testMXBean(mbs, on);
    }

    private static void testMXBean(MBeanServer mbs, ObjectName on)
            throws Exception {
        MBeanInfo mbi = mbs.getMBeanInfo(on);
        MBeanAttributeInfo[] attrs = mbi.getAttributes();
        int nattrs = attrs.length;
        if (mbi.getAttributes().length != 1)
            failure("wrong number of attributes: " + attrs);
        else {
            MBeanAttributeInfo mbai = attrs[0];
            if (mbai.getName().equals("Ints")
                && mbai.isReadable() && !mbai.isWritable()
                && mbai.getDescriptor().getFieldValue("openType")
                    .equals(new ArrayType<int[]>(SimpleType.INTEGER, true))
                && attrs[0].getType().equals("[I"))
                success("MBeanAttributeInfo");
            else
                failure("MBeanAttributeInfo: " + mbai);
        }

        int[] ints = (int[]) mbs.getAttribute(on, "Ints");
        if (equal(ints, new int[] {1, 2, 3}, null))
            success("getAttribute");
        else
            failure("getAttribute: " + Arrays.toString(ints));

        ExplicitMXBean proxy =
            JMX.newMXBeanProxy(mbs, on, ExplicitMXBean.class);
        int[] pints = proxy.getInts();
        if (equal(pints, new int[] {1, 2, 3}, null))
            success("getAttribute through proxy");
        else
            failure("getAttribute through proxy: " + Arrays.toString(pints));
    }

    private static class NamedMXBeans extends HashMap<ObjectName, Object> {
        private static final long serialVersionUID = 0;

        NamedMXBeans(MBeanServerConnection mbsc) {
            this.mbsc = mbsc;
        }

        MBeanServerConnection getMBeanServerConnection() {
            return mbsc;
        }

        private final MBeanServerConnection mbsc;
    }

    /* This is the core of the test.  Given the MXBean interface c, we
       make an MXBean object that implements that interface by
       constructing a dynamic proxy.  If the interface defines an
       attribute Foo (with getFoo and setFoo methods), then it must
       also contain a field (constant) Foo of the same type, and a
       field (constant) FooType that is an OpenType.  The field Foo is
       a reference value for this case.  We check that the attribute
       does indeed have the given OpenType.  The dynamically-created
       MXBean will return the reference value from the getFoo()
       method, and we check that that value survives the mapping to
       open values and back when the attribute is accessed through an
       MXBean proxy.  The MXBean will also check in its setFoo method
       that the value being set is equal to the reference value, which
       tests that the mapping and unmapping also works in the other
       direction.  The interface should define an operation opFoo with
       two parameters and a return value all of the same type as the
       attribute.  The MXBean will check that the two parameters are
       equal to the reference value, and will return that value.  The
       test checks that calling the operation through an MXBean proxy
       returns the reference value, again after mapping to and back
       from open values.

       If any field (constant) in the MXBean interface has a name that
       ends with ObjectName, say FooObjectName, then its value must be
       a String containing an ObjectName value.  There must be a field
       (constant) called Foo that is a valid MXBean, and that MXBean
       will be registered in the MBean Server with the given name before
       the test starts.  This enables us to test that inter-MXBean
       references are correctly converted to ObjectNames and back.
     */
    private static <T> void testInterface(Class<T> c, boolean nullTest)
            throws Exception {

        System.out.println("Testing " + c.getName() +
                           (nullTest ? " for null values" : "") + "...");

        MBeanServer mbs = MBeanServerFactory.newMBeanServer();

        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
        JMXConnectorServer cs =
            JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
        cs.start();
        JMXServiceURL addr = cs.getAddress();
        JMXConnector cc = JMXConnectorFactory.connect(addr);
        MBeanServerConnection mbsc = cc.getMBeanServerConnection();

        NamedMXBeans namedMXBeans = new NamedMXBeans(mbsc);
        InvocationHandler ih =
            nullTest ? new MXBeanNullImplInvocationHandler(c, namedMXBeans) :
                       new MXBeanImplInvocationHandler(c, namedMXBeans);
        T impl = c.cast(Proxy.newProxyInstance(c.getClassLoader(),
                                               new Class[] {c},
                                               ih));
        ObjectName on = new ObjectName("test:type=" + c.getName());
        mbs.registerMBean(impl, on);

        System.out.println("Register any MXBeans...");

        Field[] fields = c.getFields();
        for (Field field : fields) {
            String n = field.getName();
            if (n.endsWith("ObjectName")) {
                String objectNameString = (String) field.get(null);
                String base = n.substring(0, n.length() - 10);
                Field f = c.getField(base);
                Object mxbean = f.get(null);
                ObjectName objectName =
                    ObjectName.getInstance(objectNameString);
                mbs.registerMBean(mxbean, objectName);
                namedMXBeans.put(objectName, mxbean);
            }
        }

        try {
            testInterface(c, mbsc, on, namedMXBeans, nullTest);
        } finally {
            try {
                cc.close();
            } finally {
                cs.stop();
            }
        }
    }

    private static <T> void testInterface(Class<T> c,
                                          MBeanServerConnection mbsc,
                                          ObjectName on,
                                          NamedMXBeans namedMXBeans,
                                          boolean nullTest)
            throws Exception {

        System.out.println("Type check...");

        MBeanInfo mbi = mbsc.getMBeanInfo(on);
        MBeanAttributeInfo[] mbais = mbi.getAttributes();
        for (int i = 0; i < mbais.length; i++) {
            MBeanAttributeInfo mbai = mbais[i];
            String name = mbai.getName();
            Field typeField = c.getField(name + "Type");
            OpenType typeValue = (OpenType) typeField.get(null);
            OpenType openType =
                (OpenType) mbai.getDescriptor().getFieldValue("openType");
            if (typeValue.equals(openType))
                success("attribute " + name);
            else {
                final String msg =
                    "Wrong type attribute " + name + ": " +
                    openType + " should be " + typeValue;
                failure(msg);
            }
        }

        MBeanOperationInfo[] mbois = mbi.getOperations();
        for (int i = 0; i < mbois.length; i++) {
            MBeanOperationInfo mboi = mbois[i];
            String oname = mboi.getName();
            if (!oname.startsWith("op"))
                throw new Error();
            OpenType retType =
                (OpenType) mboi.getDescriptor().getFieldValue("openType");
            MBeanParameterInfo[] params = mboi.getSignature();
            MBeanParameterInfo p1i = params[0];
            MBeanParameterInfo p2i = params[1];
            OpenType p1Type =
                (OpenType) p1i.getDescriptor().getFieldValue("openType");
            OpenType p2Type =
                (OpenType) p2i.getDescriptor().getFieldValue("openType");
            if (!retType.equals(p1Type) || !p1Type.equals(p2Type)) {
                final String msg =
                    "Parameter and return open types should all be same " +
                    "but are not: " + retType + " " + oname + "(" + p1Type +
                    ", " + p2Type + ")";
                failure(msg);
                continue;
            }
            String name = oname.substring(2);
            Field typeField = c.getField(name + "Type");
            OpenType typeValue = (OpenType) typeField.get(null);
            if (typeValue.equals(retType))
                success("operation " + oname);
            else {
                final String msg =
                    "Wrong type operation " + oname + ": " +
                    retType + " should be " + typeValue;
                failure(msg);
            }
        }


        System.out.println("Mapping check...");

        Object proxy =
            JMX.newMXBeanProxy(mbsc, on, c);

        Method[] methods = c.getMethods();
        for (int i = 0; i < methods.length; i++) {
            final Method method = methods[i];
            if (method.getDeclaringClass() != c)
                continue; // skip hashCode() etc inherited from Object
            final String mname = method.getName();
            final int what = getType(method);
            final String name = getName(method);
            final Field refField = c.getField(name);
            if (nullTest && refField.getType().isPrimitive())
                continue;
            final Field openTypeField = c.getField(name + "Type");
            final OpenType openType = (OpenType) openTypeField.get(null);
            final Object refValue = nullTest ? null : refField.get(null);
            Object setValue = refValue;
            try {
                Field onField = c.getField(name + "ObjectName");
                String refName = (String) onField.get(null);
                ObjectName refObjName = ObjectName.getInstance(refName);
                Class<?> mxbeanInterface = refField.getType();
                setValue = nullTest ? null :
                    JMX.newMXBeanProxy(mbsc, refObjName, mxbeanInterface);
            } catch (Exception e) {
                // no xObjectName field, setValue == refValue
            }
            boolean ok = true;
            try {
                switch (what) {
                case GET:
                    final Object gotOpen = mbsc.getAttribute(on, name);
                    if (nullTest) {
                        if (gotOpen != null) {
                            failure(mname + " got non-null value " +
                                    gotOpen);
                            ok = false;
                        }
                    } else if (!openType.isValue(gotOpen)) {
                        if (gotOpen instanceof TabularData) {
                            // detail the mismatch
                            TabularData gotTabular = (TabularData) gotOpen;
                            compareTabularType((TabularType) openType,
                                               gotTabular.getTabularType());
                        }
                        failure(mname + " got open data " + gotOpen +
                                " not valid for open type " + openType);
                        ok = false;
                    }
                    final Object got = method.invoke(proxy, (Object[]) null);
                    if (!equal(refValue, got, namedMXBeans)) {
                        failure(mname + " got " + string(got) +
                                ", should be " + string(refValue));
                        ok = false;
                    }
                    break;

                case SET:
                    method.invoke(proxy, new Object[] {setValue});
                    break;

                case OP:
                    final Object opped =
                        method.invoke(proxy, new Object[] {setValue, setValue});
                    if (!equal(refValue, opped, namedMXBeans)) {
                        failure(
                                mname + " got " + string(opped) +
                                ", should be " + string(refValue)
                                );
                        ok = false;
                    }
                    break;

                default:
                    throw new Error();
                }

                if (ok)
                    success(mname);

            } catch (Exception e) {
                failure(mname, e);
            }
        }
    }


    private static void success(String what) {
        System.out.println("OK: " + what);
    }

    private static void failure(String what) {
        System.out.println("FAILED: " + what);
        failures++;
    }

    private static void failure(String what, Exception e) {
        System.out.println("FAILED WITH EXCEPTION: " + what);
        e.printStackTrace(System.out);
        failures++;
    }

    private static class MXBeanImplInvocationHandler
            implements InvocationHandler {
        MXBeanImplInvocationHandler(Class intf, NamedMXBeans namedMXBeans) {
            this.intf = intf;
            this.namedMXBeans = namedMXBeans;
        }

        public Object invoke(Object proxy, Method method, Object[] args)
                throws Throwable {
            final String mname = method.getName();
            final int what = getType(method);
            final String name = getName(method);
            final Field refField = intf.getField(name);
            final Object refValue = getRefValue(refField);

            switch (what) {
            case GET:
                assert args == null;
                return refValue;

            case SET:
                assert args.length == 1;
                Object setValue = args[0];
                if (!equal(refValue, setValue, namedMXBeans)) {
                    final String msg =
                        mname + "(" + string(setValue) +
                        ") does not match ref: " + string(refValue);
                    throw new IllegalArgumentException(msg);
                }
                return null;

            case OP:
                assert args.length == 2;
                Object arg1 = args[0];
                Object arg2 = args[1];
                if (!equal(arg1, arg2, namedMXBeans)) {
                    final String msg =
                        mname + "(" + string(arg1) + ", " + string(arg2) +
                        "): args not equal";
                    throw new IllegalArgumentException(msg);
                }
                if (!equal(refValue, arg1, namedMXBeans)) {
                    final String msg =
                        mname + "(" + string(arg1) + ", " + string(arg2) +
                        "): args do not match ref: " + string(refValue);
                    throw new IllegalArgumentException(msg);
                }
                return refValue;
            default:
                throw new Error();
            }
        }

        Object getRefValue(Field refField) throws Exception {
            return refField.get(null);
        }

        private final Class intf;
        private final NamedMXBeans namedMXBeans;
    }

    private static class MXBeanNullImplInvocationHandler
            extends MXBeanImplInvocationHandler {
        MXBeanNullImplInvocationHandler(Class intf, NamedMXBeans namedMXBeans) {
            super(intf, namedMXBeans);
        }

        @Override
        Object getRefValue(Field refField) throws Exception {
            Class<?> type = refField.getType();
            if (type.isPrimitive())
                return super.getRefValue(refField);
            else
                return null;
        }
    }


    private static final String[] prefixes = {
        "get", "set", "op",
    };
    private static final int GET = 0, SET = 1, OP = 2;

    private static String getName(Method m) {
        return getName(m.getName());
    }

    private static String getName(String n) {
        for (int i = 0; i < prefixes.length; i++) {
            if (n.startsWith(prefixes[i]))
                return n.substring(prefixes[i].length());
        }
        throw new Error();
    }

    private static int getType(Method m) {
        return getType(m.getName());
    }

    private static int getType(String n) {
        for (int i = 0; i < prefixes.length; i++) {
            if (n.startsWith(prefixes[i]))
                return i;
        }
        throw new Error();
    }

    static boolean equal(Object o1, Object o2, NamedMXBeans namedMXBeans) {
        if (o1 == o2)
            return true;
        if (o1 == null || o2 == null)
            return false;
        if (o1.getClass().isArray()) {
            if (!o2.getClass().isArray())
                return false;
            return deepEqual(o1, o2, namedMXBeans);
        }
        if (o1 instanceof Map) {
            if (!(o2 instanceof Map))
                return false;
            return equalMap((Map) o1, (Map) o2, namedMXBeans);
        }
        if (o1 instanceof CompositeData && o2 instanceof CompositeData) {
            return compositeDataEqual((CompositeData) o1, (CompositeData) o2,
                                      namedMXBeans);
        }
        if (Proxy.isProxyClass(o1.getClass())) {
            if (Proxy.isProxyClass(o2.getClass()))
                return proxyEqual(o1, o2, namedMXBeans);
            InvocationHandler ih = Proxy.getInvocationHandler(o1);
//            if (ih instanceof MXBeanInvocationHandler) {
//                return proxyEqualsObject((MXBeanInvocationHandler) ih,
//                                         o2, namedMXBeans);
            if (ih instanceof MBeanServerInvocationHandler) {
                return true;
            } else if (ih instanceof CompositeDataInvocationHandler) {
                return o2.equals(o1);
                // We assume the other object has a reasonable equals method
            }
        } else if (Proxy.isProxyClass(o2.getClass()))
            return equal(o2, o1, namedMXBeans);
        return o1.equals(o2);
    }

    // We'd use Arrays.deepEquals except we want the test to work on 1.4
    // Note this code assumes no selfreferential arrays
    // (as does Arrays.deepEquals)
    private static boolean deepEqual(Object a1, Object a2,
                                     NamedMXBeans namedMXBeans) {
        int len = Array.getLength(a1);
        if (len != Array.getLength(a2))
            return false;
        for (int i = 0; i < len; i++) {
            Object e1 = Array.get(a1, i);
            Object e2 = Array.get(a2, i);
            if (!equal(e1, e2, namedMXBeans))
                return false;
        }
        return true;
    }

    private static boolean equalMap(Map<?,?> m1, Map<?,?> m2,
                                    NamedMXBeans namedMXBeans) {
        if (m1.size() != m2.size())
            return false;
        if ((m1 instanceof SortedMap) != (m2 instanceof SortedMap))
            return false;
        for (Object k1 : m1.keySet()) {
            if (!m2.containsKey(k1))
                return false;
            if (!equal(m1.get(k1), m2.get(k1), namedMXBeans))
                return false;
        }
        return true;
    }

    // This is needed to work around a bug (5095277)
    // in CompositeDataSupport.equals
    private static boolean compositeDataEqual(CompositeData cd1,
                                              CompositeData cd2,
                                              NamedMXBeans namedMXBeans) {
        if (cd1 == cd2)
            return true;
        if (!cd1.getCompositeType().equals(cd2.getCompositeType()))
            return false;
        Collection v1 = cd1.values();
        Collection v2 = cd2.values();
        if (v1.size() != v2.size())
            return false; // should not happen
        for (Iterator i1 = v1.iterator(), i2 = v2.iterator();
             i1.hasNext(); ) {
            if (!equal(i1.next(), i2.next(), namedMXBeans))
                return false;
        }
        return true;
    }

    // Also needed for 5095277
    private static boolean proxyEqual(Object proxy1, Object proxy2,
                                      NamedMXBeans namedMXBeans) {
        if (proxy1.getClass() != proxy2.getClass())
            return proxy1.equals(proxy2);
        InvocationHandler ih1 = Proxy.getInvocationHandler(proxy1);
        InvocationHandler ih2 = Proxy.getInvocationHandler(proxy2);
        if (!(ih1 instanceof CompositeDataInvocationHandler)
            || !(ih2 instanceof CompositeDataInvocationHandler))
            return proxy1.equals(proxy2);
        CompositeData cd1 =
            ((CompositeDataInvocationHandler) ih1).getCompositeData();
        CompositeData cd2 =
            ((CompositeDataInvocationHandler) ih2).getCompositeData();
        return compositeDataEqual(cd1, cd2, namedMXBeans);
    }

//    private static boolean proxyEqualsObject(MXBeanInvocationHandler ih,
//                                             Object o,
//                                             NamedMXBeans namedMXBeans) {
//        if (namedMXBeans.getMBeanServerConnection() !=
//            ih.getMBeanServerConnection())
//            return false;
//
//        ObjectName on = ih.getObjectName();
//        Object named = namedMXBeans.get(on);
//        if (named == null)
//            return false;
//        return (o == named && ih.getMXBeanInterface().isInstance(named));
//    }

    /* I wanted to call this method toString(Object), but oddly enough
       this meant that I couldn't call it from the inner class
       MXBeanImplInvocationHandler, because the inherited Object.toString()
       prevented that.  */
    static String string(Object o) {
        if (o == null)
            return "null";
        if (o instanceof String)
            return '"' + (String) o + '"';
        if (o instanceof Collection)
            return deepToString((Collection) o);
        if (o.getClass().isArray())
            return deepToString(o);
        return o.toString();
    }

    private static String deepToString(Object o) {
        StringBuffer buf = new StringBuffer();
        buf.append("[");
        int len = Array.getLength(o);
        for (int i = 0; i < len; i++) {
            if (i > 0)
                buf.append(", ");
            Object e = Array.get(o, i);
            buf.append(string(e));
        }
        buf.append("]");
        return buf.toString();
    }

    private static String deepToString(Collection c) {
        return deepToString(c.toArray());
    }

    private static void compareTabularType(TabularType t1, TabularType t2) {
        if (t1.equals(t2)) {
            System.out.println("same tabular type");
            return;
        }
        if (t1.getClassName().equals(t2.getClassName()))
            System.out.println("same class name");
        if (t1.getDescription().equals(t2.getDescription()))
            System.out.println("same description");
        else {
            System.out.println("t1 description: " + t1.getDescription());
            System.out.println("t2 description: " + t2.getDescription());
        }
        if (t1.getIndexNames().equals(t2.getIndexNames()))
            System.out.println("same index names");
        if (t1.getRowType().equals(t2.getRowType()))
            System.out.println("same row type");
    }
}