jdk/src/share/classes/javax/management/openmbean/CompositeDataInvocationHandler.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 687 874e25a9844a
child 1510 e747d3193ef2
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 687
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  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.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.management.openmbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jmx.mbeanserver.MXBeanLookup;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    29
import com.sun.jmx.mbeanserver.DefaultMXBeanMappingFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.InvocationHandler;
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.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
   <p>An {@link InvocationHandler} that forwards getter methods to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
   {@link CompositeData}.  If you have an interface that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
   only getter methods (such as {@code String getName()} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
   {@code boolean isActive()}) then you can use this class in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
   conjunction with the {@link Proxy} class to produce an implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
   of the interface where each getter returns the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
   corresponding item in a {@code CompositeData}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
   <p>For example, suppose you have an interface like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
   <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
   <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
   public interface NamedNumber {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
       public int getNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
       public String getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
   </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
   </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
   and a {@code CompositeData} constructed like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
   <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
   <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
   CompositeData cd =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
       new {@link CompositeDataSupport}(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
           someCompositeType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
           new String[] {"number", "name"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
           new Object[] {<b>5</b>, "five"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
       );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
   </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
   </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
   then you can construct an object implementing {@code NamedNumber}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
   and backed by the object {@code cd} like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
   <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
   <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
   InvocationHandler handler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
       new CompositeDataInvocationHandler(cd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
   NamedNumber nn = (NamedNumber)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
       Proxy.newProxyInstance(NamedNumber.class.getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                              new Class[] {NamedNumber.class},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                              handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
   </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
   </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
   A call to {@code nn.getNumber()} will then return <b>5</b>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
   <p>If the first letter of the property defined by a getter is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
   capital, then this handler will look first for an item in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
   {@code CompositeData} beginning with a capital, then, if that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
   not found, for an item beginning with the corresponding lowercase
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
   letter or code point.  For a getter called {@code getNumber()}, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
   handler will first look for an item called {@code Number}, then for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
   {@code number}.  If the getter is called {@code getnumber()}, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
   the item must be called {@code number}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
   <p>If the method given to {@link #invoke invoke} is the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
   {@code boolean equals(Object)} inherited from {@code Object}, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
   it will return true if and only if the argument is a {@code Proxy}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
   whose {@code InvocationHandler} is also a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
   CompositeDataInvocationHandler} and whose backing {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
   CompositeData} is equal (not necessarily identical) to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
   object's.  If the method given to {@code invoke} is the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
   {@code int hashCode()} inherited from {@code Object}, then it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
   return a value that is consistent with this definition of {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
   equals}: if two objects are equal according to {@code equals}, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
   they will have the same {@code hashCode}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
   @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
public class CompositeDataInvocationHandler implements InvocationHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
       <p>Construct a handler backed by the given {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
       CompositeData}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
       @param compositeData the {@code CompositeData} that will supply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
       information to getters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
       @throws IllegalArgumentException if {@code compositeData}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
       is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public CompositeDataInvocationHandler(CompositeData compositeData) {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   118
        this(compositeData, MXBeanMappingFactory.DEFAULT);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   119
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   120
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   121
    public CompositeDataInvocationHandler(CompositeData compositeData,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   122
                                          MXBeanMappingFactory mappingFactory) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   123
        this(compositeData, mappingFactory, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
       <p>Construct a handler backed by the given {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
       CompositeData}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
       @param mbsc the {@code MBeanServerConnection} related to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
       {@code CompositeData}.  This is only relevant if a method in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
       the interface for which this is an invocation handler returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
       a type that is an MXBean interface.  Otherwise, it can be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
       @param compositeData the {@code CompositeData} that will supply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
       information to getters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
       @throws IllegalArgumentException if {@code compositeData}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
       is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    CompositeDataInvocationHandler(CompositeData compositeData,
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   142
                                   MXBeanMappingFactory mappingFactory,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                   MXBeanLookup lookup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (compositeData == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new IllegalArgumentException("compositeData");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this.compositeData = compositeData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        this.lookup = lookup;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   148
        this.mappingFactory = mappingFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
       Return the {@code CompositeData} that was supplied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
       constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
       @return the {@code CompositeData} that this handler is backed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
       by.  This is never null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public CompositeData getCompositeData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        assert compositeData != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return compositeData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public Object invoke(Object proxy, Method method, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        final String methodName = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // Handle the methods from java.lang.Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (method.getDeclaringClass() == Object.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            if (methodName.equals("toString") && args == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                return "Proxy[" + compositeData + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            else if (methodName.equals("hashCode") && args == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                return compositeData.hashCode() + 0x43444948;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            else if (methodName.equals("equals") && args.length == 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                && method.getParameterTypes()[0] == Object.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                return equals(proxy, args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                /* Either someone is calling invoke by hand, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                   it is a non-final method from Object overriden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                   by the generated Proxy.  At the time of writing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                   the only non-final methods in Object that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                   handled above are finalize and clone, and these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                   are not overridden in generated proxies.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                return method.invoke(this, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   186
        String propertyName = DefaultMXBeanMappingFactory.propertyName(method);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (propertyName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            throw new IllegalArgumentException("Method is not getter: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                               method.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        Object openValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (compositeData.containsKey(propertyName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            openValue = compositeData.get(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        else {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   195
            String decap = DefaultMXBeanMappingFactory.decapitalize(propertyName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if (compositeData.containsKey(decap))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                openValue = compositeData.get(decap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    "No CompositeData item " + propertyName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    (decap.equals(propertyName) ? "" : " or " + decap) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    " to match " + methodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   206
        MXBeanMapping mapping =
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   207
            mappingFactory.mappingForType(method.getGenericReturnType(),
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   208
                                   MXBeanMappingFactory.DEFAULT);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   209
        return mapping.fromOpenValue(openValue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /* This method is called when equals(Object) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * called on our proxy and hence forwarded to us.  For example, if we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * are a proxy for an interface like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * public interface GetString {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *     public String string();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * then we must compare equal to another CompositeDataInvocationHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * proxy for the same interface and where string() returns the same value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * You might think that we should also compare equal to another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * object that implements GetString directly rather than using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Proxy, provided that its string() returns the same result as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * ours, and in fact an earlier version of this class did that (by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * converting the other object into a CompositeData and comparing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * that with ours).  But in fact that doesn't make a great deal of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * sense because there's absolutely no guarantee that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * resulting equals would be reflexive (otherObject.equals(this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * might be false even if this.equals(otherObject) is true), and,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * especially, there's no way we could generate a hashCode() that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * would be equal to otherObject.hashCode() when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * this.equals(otherObject), because we don't know how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * otherObject.hashCode() is computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    private boolean equals(Object proxy, Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (other == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        final Class proxyClass = proxy.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        final Class otherClass = other.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (proxyClass != otherClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        InvocationHandler otherih = Proxy.getInvocationHandler(other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (!(otherih instanceof CompositeDataInvocationHandler))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        CompositeDataInvocationHandler othercdih =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            (CompositeDataInvocationHandler) otherih;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return compositeData.equals(othercdih.compositeData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private final CompositeData compositeData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private final MXBeanLookup lookup;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   253
    private final MXBeanMappingFactory mappingFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
}