jdk/src/share/classes/com/sun/jmx/mbeanserver/MBeanAnalyzer.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 687 874e25a9844a
child 834 dc74d4ddc28e
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 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
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
    54
    static interface MBeanVisitor<M, X extends Exception> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        public void visitAttribute(String attributeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                M getter,
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
    57
                M setter) throws X;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        public void visitOperation(String operationName,
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
    59
                M operation) throws X;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
    62
    <X extends Exception> void visit(MBeanVisitor<M, X> visitor) throws X {
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 {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   107
        introspector.checkCompliance(mbeanType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        try {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   110
            initMaps(mbeanType, introspector);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } catch (Exception x) {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   112
            throw Introspector.throwException(mbeanType,x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // Introspect the mbeanInterface and initialize this object's maps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    //
287
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   118
    private void initMaps(Class<?> mbeanType,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            MBeanIntrospector<M> introspector) throws Exception {
287
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   120
        final List<Method> methods1 = introspector.getMethods(mbeanType);
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   121
        final List<Method> methods = eliminateCovariantMethods(methods1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        /* Run through the methods to detect inconsistencies and to enable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
           us to give getter and setter together to visitAttribute. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        for (Method m : methods) {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   126
            final String name = m.getName();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   127
            final int nParams = m.getParameterTypes().length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            final M cm = introspector.mFrom(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            String attrName = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (name.startsWith("get"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                attrName = name.substring(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            else if (name.startsWith("is")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            && m.getReturnType() == boolean.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                attrName = name.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   138
            if (attrName.length() != 0 && nParams == 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    && m.getReturnType() != void.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                // It's a getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                // Check we don't have both isX and getX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                AttrMethods<M> am = attrMap.get(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if (am == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    am = new AttrMethods<M>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    if (am.getter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        final String msg = "Attribute " + attrName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                " has more than one getter";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        throw new NotCompliantMBeanException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                am.getter = cm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                attrMap.put(attrName, am);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            } else if (name.startsWith("set") && name.length() > 3
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   155
                    && nParams == 1 &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    m.getReturnType() == void.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                // It's a setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                attrName = name.substring(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                AttrMethods<M> am = attrMap.get(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                if (am == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    am = new AttrMethods<M>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                else if (am.setter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    final String msg = "Attribute " + attrName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                            " has more than one setter";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    throw new NotCompliantMBeanException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                am.setter = cm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                attrMap.put(attrName, am);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                // It's an operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                List<M> cms = opMap.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                if (cms == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    cms = newList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                cms.add(cm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                opMap.put(name, cms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        /* Check that getters and setters are consistent. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        for (Map.Entry<String, AttrMethods<M>> entry : attrMap.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            AttrMethods<M> am = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (!introspector.consistent(am.getter, am.setter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                final String msg = "Getter and setter for " + entry.getKey() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        " have inconsistent types";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                throw new NotCompliantMBeanException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * A comparator that defines a total order so that methods have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * same name and identical signatures appear next to each others.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * The methods are sorted in such a way that methods which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * override each other will sit next to each other, with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * overridden method first - e.g. Object getFoo() is placed before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Integer getFoo(). This makes it possible to determine whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * a method overrides another one simply by looking at the method(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * that precedes it in the list. (see eliminateCovariantMethods).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private static class MethodOrder implements Comparator<Method> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        public int compare(Method a, Method b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            final int cmp = a.getName().compareTo(b.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (cmp != 0) return cmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            final Class<?>[] aparams = a.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            final Class<?>[] bparams = b.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (aparams.length != bparams.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                return aparams.length - bparams.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (!Arrays.equals(aparams, bparams)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                return Arrays.toString(aparams).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        compareTo(Arrays.toString(bparams));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            final Class<?> aret = a.getReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            final Class<?> bret = b.getReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (aret == bret) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            // Super type comes first: Object, Number, Integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (aret.isAssignableFrom(bret))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return +1;      // could assert bret.isAssignableFrom(aret)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        public final static MethodOrder instance = new MethodOrder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /* Eliminate methods that are overridden with a covariant return type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
       Reflection will return both the original and the overriding method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
       but only the overriding one is of interest.  We return the methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
       in the same order they arrived in.  This isn't required by the spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
       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
   229
       operations or attributes appear in a particular order.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   230
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   231
       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
   232
       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
   233
       So don't do that.  */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    static List<Method>
287
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   235
            eliminateCovariantMethods(List<Method> startMethods) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        // We are assuming that you never have very many methods with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        // same name, so it is OK to use algorithms that are quadratic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        // in the number of methods with the same name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
287
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   240
        final int len = startMethods.size();
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   241
        final Method[] sorted = startMethods.toArray(new Method[len]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        Arrays.sort(sorted,MethodOrder.instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        final Set<Method> overridden = newSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        for (int i=1;i<len;i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            final Method m0 = sorted[i-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            final Method m1 = sorted[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   248
            // Methods that don't have the same name can't override each other
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (!m0.getName().equals(m1.getName())) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            // Methods that have the same name and same signature override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // each other. In that case, the second method overrides the first,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            // due to the way we have sorted them in MethodOrder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            if (Arrays.equals(m0.getParameterTypes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    m1.getParameterTypes())) {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   256
                if (!overridden.add(m0))
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   257
                    throw new RuntimeException("Internal error: duplicate Method");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
287
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   261
        final List<Method> methods = newList(startMethods);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        methods.removeAll(overridden);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return methods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
}