jdk/src/share/classes/com/sun/jmx/mbeanserver/MBeanAnalyzer.java
author emcmanus
Wed, 21 Oct 2009 17:33:18 +0200
changeset 4156 acaa49a2768a
parent 834 dc74d4ddc28e
child 5506 202f599c92aa
permissions -rw-r--r--
6851617: Remove JSR 255 (JMX API 2.0) from JDK 7 Summary: See http://weblogs.java.net/blog/2009/06/16/jsr-255-jmx-api-20-postponed Reviewed-by: dfuchs
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 com.sun.jmx.mbeanserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import static com.sun.jmx.mbeanserver.Util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.NotCompliantMBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>An analyzer for a given MBean interface.  The analyzer can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * be for Standard MBeans or MXBeans, depending on the MBeanIntrospector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * passed at construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>The analyzer can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * visit the attributes and operations of the interface, calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * a caller-supplied visitor method for each one.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @param <M> Method or ConvertingMethod according as this is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Standard MBean or an MXBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
class MBeanAnalyzer<M> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 834
diff changeset
    54
    static interface MBeanVisitor<M> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        public void visitAttribute(String attributeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                M getter,
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 834
diff changeset
    57
                M setter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        public void visitOperation(String operationName,
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 834
diff changeset
    59
                M operation);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 834
diff changeset
    62
    void visit(MBeanVisitor<M> visitor) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // visit attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        for (Map.Entry<String, AttrMethods<M>> entry : attrMap.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            String name = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            AttrMethods<M> am = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            visitor.visitAttribute(name, am.getter, am.setter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // visit operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        for (Map.Entry<String, List<M>> entry : opMap.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            for (M m : entry.getValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                visitor.visitOperation(entry.getKey(), m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /* Map op name to method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private Map<String, List<M>> opMap = newInsertionOrderMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /* Map attr name to getter and/or setter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private Map<String, AttrMethods<M>> attrMap = newInsertionOrderMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static class AttrMethods<M> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        M getter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        M setter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * <p>Return an MBeanAnalyzer for the given MBean interface and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * MBeanIntrospector.  Calling this method twice with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * parameters may return the same object or two different but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * equivalent objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // Currently it's two different but equivalent objects.  This only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // really impacts proxy generation.  For MBean creation, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // cached PerInterface object for an MBean interface means that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // an analyzer will not be recreated for a second MBean using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // same interface.
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
    98
    static <M> MBeanAnalyzer<M> analyzer(Class<?> mbeanType,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            MBeanIntrospector<M> introspector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throws NotCompliantMBeanException {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   101
        return new MBeanAnalyzer<M>(mbeanType, introspector);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   104
    private MBeanAnalyzer(Class<?> mbeanType,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            MBeanIntrospector<M> introspector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throws NotCompliantMBeanException {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 834
diff changeset
   107
        if (!mbeanType.isInterface()) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 834
diff changeset
   108
            throw new NotCompliantMBeanException("Not an interface: " +
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 834
diff changeset
   109
                    mbeanType.getName());
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 834
diff changeset
   110
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        try {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   113
            initMaps(mbeanType, introspector);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } catch (Exception x) {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   115
            throw Introspector.throwException(mbeanType,x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // Introspect the mbeanInterface and initialize this object's maps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    //
287
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   121
    private void initMaps(Class<?> mbeanType,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            MBeanIntrospector<M> introspector) throws Exception {
287
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   123
        final List<Method> methods1 = introspector.getMethods(mbeanType);
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   124
        final List<Method> methods = eliminateCovariantMethods(methods1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        /* Run through the methods to detect inconsistencies and to enable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
           us to give getter and setter together to visitAttribute. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        for (Method m : methods) {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   129
            final String name = m.getName();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   130
            final int nParams = m.getParameterTypes().length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            final M cm = introspector.mFrom(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            String attrName = "";
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 834
diff changeset
   135
            if (name.startsWith("get"))
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 834
diff changeset
   136
                attrName = name.substring(3);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 834
diff changeset
   137
            else if (name.startsWith("is")
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 834
diff changeset
   138
            && m.getReturnType() == boolean.class)
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 834
diff changeset
   139
                attrName = name.substring(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   141
            if (attrName.length() != 0 && nParams == 0
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 834
diff changeset
   142
                    && m.getReturnType() != void.class) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                // It's a getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                // Check we don't have both isX and getX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                AttrMethods<M> am = attrMap.get(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                if (am == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    am = new AttrMethods<M>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    if (am.getter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        final String msg = "Attribute " + attrName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                                " has more than one getter";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        throw new NotCompliantMBeanException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                am.getter = cm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                attrMap.put(attrName, am);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            } else if (name.startsWith("set") && name.length() > 3
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   158
                    && nParams == 1 &&
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 834
diff changeset
   159
                    m.getReturnType() == void.class) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                // It's a setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                attrName = name.substring(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                AttrMethods<M> am = attrMap.get(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                if (am == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    am = new AttrMethods<M>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                else if (am.setter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    final String msg = "Attribute " + attrName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                            " has more than one setter";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    throw new NotCompliantMBeanException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                am.setter = cm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                attrMap.put(attrName, am);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                // It's an operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                List<M> cms = opMap.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (cms == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    cms = newList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                cms.add(cm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                opMap.put(name, cms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        /* Check that getters and setters are consistent. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        for (Map.Entry<String, AttrMethods<M>> entry : attrMap.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            AttrMethods<M> am = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (!introspector.consistent(am.getter, am.setter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                final String msg = "Getter and setter for " + entry.getKey() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        " have inconsistent types";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                throw new NotCompliantMBeanException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * A comparator that defines a total order so that methods have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * same name and identical signatures appear next to each others.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * The methods are sorted in such a way that methods which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * override each other will sit next to each other, with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * overridden method first - e.g. Object getFoo() is placed before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Integer getFoo(). This makes it possible to determine whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * a method overrides another one simply by looking at the method(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * that precedes it in the list. (see eliminateCovariantMethods).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private static class MethodOrder implements Comparator<Method> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        public int compare(Method a, Method b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            final int cmp = a.getName().compareTo(b.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (cmp != 0) return cmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            final Class<?>[] aparams = a.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            final Class<?>[] bparams = b.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (aparams.length != bparams.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                return aparams.length - bparams.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (!Arrays.equals(aparams, bparams)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                return Arrays.toString(aparams).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                        compareTo(Arrays.toString(bparams));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            final Class<?> aret = a.getReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            final Class<?> bret = b.getReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (aret == bret) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            // Super type comes first: Object, Number, Integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            if (aret.isAssignableFrom(bret))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            return +1;      // could assert bret.isAssignableFrom(aret)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        public final static MethodOrder instance = new MethodOrder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /* Eliminate methods that are overridden with a covariant return type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
       Reflection will return both the original and the overriding method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
       but only the overriding one is of interest.  We return the methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
       in the same order they arrived in.  This isn't required by the spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
       but existing code may depend on it and users may be used to seeing
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   232
       operations or attributes appear in a particular order.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   233
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   234
       Because of the way this method works, if the same Method appears
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   235
       more than once in the given List then it will be completely deleted!
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   236
       So don't do that.  */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    static List<Method>
287
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   238
            eliminateCovariantMethods(List<Method> startMethods) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // We are assuming that you never have very many methods with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        // same name, so it is OK to use algorithms that are quadratic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // in the number of methods with the same name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
287
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   243
        final int len = startMethods.size();
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   244
        final Method[] sorted = startMethods.toArray(new Method[len]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        Arrays.sort(sorted,MethodOrder.instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        final Set<Method> overridden = newSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        for (int i=1;i<len;i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            final Method m0 = sorted[i-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            final Method m1 = sorted[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   251
            // Methods that don't have the same name can't override each other
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (!m0.getName().equals(m1.getName())) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            // Methods that have the same name and same signature override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            // each other. In that case, the second method overrides the first,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            // due to the way we have sorted them in MethodOrder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (Arrays.equals(m0.getParameterTypes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    m1.getParameterTypes())) {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   259
                if (!overridden.add(m0))
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   260
                    throw new RuntimeException("Internal error: duplicate Method");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
287
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   264
        final List<Method> methods = newList(startMethods);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        methods.removeAll(overridden);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return methods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
}