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