jdk/src/share/classes/com/sun/jmx/mbeanserver/Introspector.java
author egahlin
Thu, 21 Mar 2013 13:56:28 +0100
changeset 18221 5cd0fa789013
parent 18218 42b9377e6462
child 18805 b359f8adc8ad
permissions -rw-r--r--
8008611: Better handling of annotations in JMX Reviewed-by: skoivu, dholmes, jfdenise
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4328
diff changeset
     2
 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4328
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4328
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4328
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4328
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4328
diff changeset
    23
 * questions.
2
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 java.lang.annotation.Annotation;
4328
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
    29
import java.lang.ref.SoftReference;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.AnnotatedElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.Modifier;
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
    34
import java.lang.reflect.Proxy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.reflect.UndeclaredThrowableException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Arrays;
4328
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
    37
import java.util.Collections;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.HashMap;
4328
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
    39
import java.util.List;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
    40
import java.util.LinkedList;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
    41
import java.util.Locale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Map;
4328
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
    43
import java.util.WeakHashMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.Descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.DescriptorKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.DynamicMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.ImmutableDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.MBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.NotCompliantMBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    52
import com.sun.jmx.remote.util.EnvHelp;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    53
import java.lang.reflect.Array;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    54
import java.lang.reflect.InvocationTargetException;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    55
import javax.management.AttributeNotFoundException;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    56
import javax.management.openmbean.CompositeData;
16086
0a1053497df1 8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents: 5506
diff changeset
    57
import sun.reflect.misc.MethodUtil;
0a1053497df1 8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents: 5506
diff changeset
    58
import sun.reflect.misc.ReflectUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * This class contains the methods for performing all the tests needed to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * that a class represents a JMX compliant MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public class Introspector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
    68
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *  PRIVATE CONSTRUCTORS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // private constructor defined to "hide" the default public constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private Introspector() {
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *  PUBLIC METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Tell whether a MBean of the given class is a Dynamic MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * This method does nothing more than returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * javax.management.DynamicMBean.class.isAssignableFrom(c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * This method does not check for any JMX MBean compliance:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <ul><li>If <code>true</code> is returned, then instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *     <code>c</code> are DynamicMBean.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *     <li>If <code>false</code> is returned, then no further
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *     assumption can be made on instances of <code>c</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *     In particular, instances of <code>c</code> may, or may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *     be JMX standard MBeans.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param c The class of the MBean under examination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @return <code>true</code> if instances of <code>c</code> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *         Dynamic MBeans, <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     **/
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1322
diff changeset
   108
    public static final boolean isDynamic(final Class<?> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // Check if the MBean implements the DynamicMBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return javax.management.DynamicMBean.class.isAssignableFrom(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Basic method for testing that a MBean of a given class can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * instantiated by the MBean server.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * This method checks that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <ul><li>The given class is a concrete class.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *     <li>The given class exposes at least one public constructor.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * If these conditions are not met, throws a NotCompliantMBeanException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param c The class of the MBean we want to create.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @exception NotCompliantMBeanException if the MBean class makes it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *            impossible to instantiate the MBean from within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *            MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     **/
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1322
diff changeset
   127
    public static void testCreation(Class<?> c)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // Check if the class is a concrete class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        final int mods = c.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (Modifier.isAbstract(mods) || Modifier.isInterface(mods)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new NotCompliantMBeanException("MBean class must be concrete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // Check if the MBean has a public constructor
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1322
diff changeset
   136
        final Constructor<?>[] consList = c.getConstructors();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (consList.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new NotCompliantMBeanException("MBean class must have public constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   142
    public static void checkCompliance(Class<?> mbeanClass)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   143
    throws NotCompliantMBeanException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // Is DynamicMBean?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (DynamicMBean.class.isAssignableFrom(mbeanClass))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // Is Standard MBean?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        final Exception mbeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            getStandardMBeanInterface(mbeanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            mbeanException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // Is MXBean?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        final Exception mxbeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            getMXBeanInterface(mbeanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            mxbeanException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            "MBean class " + mbeanClass.getName() + " does not implement " +
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   168
            "DynamicMBean, and neither follows the Standard MBean conventions (" +
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   169
            mbeanException.toString() + ") nor the MXBean conventions (" +
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   170
            mxbeanException.toString() + ")";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        throw new NotCompliantMBeanException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   174
    public static <T> DynamicMBean makeDynamicMBean(T mbean)
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   175
        throws NotCompliantMBeanException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (mbean instanceof DynamicMBean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return (DynamicMBean) mbean;
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   178
        final Class<?> mbeanClass = mbean.getClass();
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   179
        Class<? super T> c = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        try {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   181
            c = Util.cast(getStandardMBeanInterface(mbeanClass));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            // Ignore exception - we need to check whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            // mbean is an MXBean first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (c != null)
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   187
            return new StandardMBeanSupport(mbean, c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        try {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   190
            c = Util.cast(getMXBeanInterface(mbeanClass));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            // Ignore exception - we cannot decide whether mbean was supposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            // to be an MBean or an MXBean. We will call checkCompliance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            // to generate the appropriate exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   196
        if (c != null)
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   197
            return new MXBeanSupport(mbean, c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        checkCompliance(mbeanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        throw new NotCompliantMBeanException("Not compliant"); // not reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Basic method for testing if a given class is a JMX compliant MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param baseClass The class to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return <code>null</code> if the MBean is a DynamicMBean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *         the computed {@link javax.management.MBeanInfo} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @exception NotCompliantMBeanException The specified class is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *            JMX compliant MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1322
diff changeset
   212
    public static MBeanInfo testCompliance(Class<?> baseClass)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        // Check if the MBean implements the MBean or the Dynamic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        // MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (isDynamic(baseClass))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return testCompliance(baseClass, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   226
    public static void testComplianceMXBeanInterface(Class<?> interfaceClass)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            throws NotCompliantMBeanException {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   228
        MXBeanIntrospector.getInstance().getAnalyzer(interfaceClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
18213
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 16107
diff changeset
   231
    public static void testComplianceMBeanInterface(Class<?> interfaceClass)
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 16107
diff changeset
   232
            throws NotCompliantMBeanException{
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 16107
diff changeset
   233
        StandardMBeanIntrospector.getInstance().getAnalyzer(interfaceClass);
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 16107
diff changeset
   234
    }
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 16107
diff changeset
   235
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Basic method for testing if a given class is a JMX compliant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Standard MBean.  This method is only called by the legacy code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * in com.sun.management.jmx.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param baseClass The class to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param mbeanInterface the MBean interface that the class implements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * or null if the interface must be determined by introspection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @return the computed {@link javax.management.MBeanInfo}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @exception NotCompliantMBeanException The specified class is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *            JMX compliant Standard MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public static synchronized MBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            testCompliance(final Class<?> baseClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                           Class<?> mbeanInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (mbeanInterface == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            mbeanInterface = getStandardMBeanInterface(baseClass);
18218
42b9377e6462 8008124: Better compliance testing
dsamersoff
parents: 18213
diff changeset
   256
        ReflectUtil.checkPackageAccess(mbeanInterface);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        MBeanIntrospector<?> introspector = StandardMBeanIntrospector.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return getClassMBeanInfo(introspector, baseClass, mbeanInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private static <M> MBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            getClassMBeanInfo(MBeanIntrospector<M> introspector,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                              Class<?> baseClass, Class<?> mbeanInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        PerInterface<M> perInterface = introspector.getPerInterface(mbeanInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return introspector.getClassMBeanInfo(baseClass, perInterface);
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
     * Get the MBean interface implemented by a JMX Standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * MBean class. This method is only called by the legacy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * code in "com.sun.management.jmx".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param baseClass The class to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @return The MBean interface implemented by the MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *         Return <code>null</code> if the MBean is a DynamicMBean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *         or if no MBean interface is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   280
    public static Class<?> getMBeanInterface(Class<?> baseClass) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // Check if the given class implements the MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // or the Dynamic MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (isDynamic(baseClass)) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return getStandardMBeanInterface(baseClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Get the MBean interface implemented by a JMX Standard MBean class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param baseClass The class to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @return The MBean interface implemented by the Standard MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @throws NotCompliantMBeanException The specified class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * not a JMX compliant Standard MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   301
    public static <T> Class<? super T> getStandardMBeanInterface(Class<T> baseClass)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   302
    throws NotCompliantMBeanException {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   303
        Class<? super T> current = baseClass;
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   304
        Class<? super T> mbeanInterface = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        while (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            mbeanInterface =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                findMBeanInterface(current, current.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            if (mbeanInterface != null) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            current = current.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (mbeanInterface != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            return mbeanInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                "Class " + baseClass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                " is not a JMX compliant Standard MBean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            throw new NotCompliantMBeanException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Get the MXBean interface implemented by a JMX MXBean class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @param baseClass The class to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @return The MXBean interface implemented by the MXBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @throws NotCompliantMBeanException The specified class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * not a JMX compliant MXBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   331
    public static <T> Class<? super T> getMXBeanInterface(Class<T> baseClass)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return MXBeanSupport.findMXBeanInterface(baseClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            throw throwException(baseClass,e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *  PRIVATE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Try to find the MBean interface corresponding to the class aName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * - i.e. <i>aName</i>MBean, from within aClass and its superclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     **/
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   351
    private static <T> Class<? super T> findMBeanInterface(
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   352
            Class<T> aClass, String aName) {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   353
        Class<? super T> current = aClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        while (current != null) {
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   355
            final Class<?>[] interfaces = current.getInterfaces();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            final int len = interfaces.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            for (int i=0;i<len;i++)  {
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   358
                Class<? super T> inter = Util.cast(interfaces[i]);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   359
                inter = implementsMBean(inter, aName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                if (inter != null) return inter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            current = current.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   367
    public static Descriptor descriptorForElement(final AnnotatedElement elmt) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (elmt == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            return ImmutableDescriptor.EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        final Annotation[] annots = elmt.getAnnotations();
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   371
        return descriptorForAnnotations(annots);
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   372
    }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   373
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public static Descriptor descriptorForAnnotations(Annotation[] annots) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (annots.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            return ImmutableDescriptor.EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        Map<String, Object> descriptorMap = new HashMap<String, Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        for (Annotation a : annots) {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   379
            Class<? extends Annotation> c = a.annotationType();
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   380
            Method[] elements = c.getMethods();
18221
5cd0fa789013 8008611: Better handling of annotations in JMX
egahlin
parents: 18218
diff changeset
   381
            boolean packageAccess = false;
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   382
            for (Method element : elements) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   383
                DescriptorKey key = element.getAnnotation(DescriptorKey.class);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   384
                if (key != null) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   385
                    String name = key.value();
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   386
                    Object value;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   387
                    try {
18221
5cd0fa789013 8008611: Better handling of annotations in JMX
egahlin
parents: 18218
diff changeset
   388
                        // Avoid checking access more than once per annotation
5cd0fa789013 8008611: Better handling of annotations in JMX
egahlin
parents: 18218
diff changeset
   389
                        if (!packageAccess) {
5cd0fa789013 8008611: Better handling of annotations in JMX
egahlin
parents: 18218
diff changeset
   390
                            ReflectUtil.checkPackageAccess(c);
5cd0fa789013 8008611: Better handling of annotations in JMX
egahlin
parents: 18218
diff changeset
   391
                            packageAccess = true;
5cd0fa789013 8008611: Better handling of annotations in JMX
egahlin
parents: 18218
diff changeset
   392
                        }
5cd0fa789013 8008611: Better handling of annotations in JMX
egahlin
parents: 18218
diff changeset
   393
                        value = MethodUtil.invoke(element, a, null);
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   394
                    } catch (RuntimeException e) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   395
                        // we don't expect this - except for possibly
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   396
                        // security exceptions?
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   397
                        // RuntimeExceptions shouldn't be "UndeclaredThrowable".
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   398
                        // anyway...
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   399
                        //
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   400
                        throw e;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   401
                    } catch (Exception e) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   402
                        // we don't expect this
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   403
                        throw new UndeclaredThrowableException(e);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   404
                    }
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   405
                    value = annotationToField(value);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   406
                    Object oldValue = descriptorMap.put(name, value);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   407
                    if (oldValue != null && !equals(oldValue, value)) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   408
                        final String msg =
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   409
                            "Inconsistent values for descriptor field " + name +
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   410
                            " from annotations: " + value + " :: " + oldValue;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   411
                        throw new IllegalArgumentException(msg);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   412
                    }
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   413
                }
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   414
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (descriptorMap.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            return ImmutableDescriptor.EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            return new ImmutableDescriptor(descriptorMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
1699
3611e5fd6da5 6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents: 1510
diff changeset
   423
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Throws a NotCompliantMBeanException or a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @param notCompliant the class which was under examination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @param cause the raeson why NotCompliantMBeanException should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *        be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @return nothing - this method always throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *         The return type makes it possible to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *         <pre> throw throwException(clazz,cause); </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @throws SecurityException - if cause is a SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @throws NotCompliantMBeanException otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    static NotCompliantMBeanException throwException(Class<?> notCompliant,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            Throwable cause)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            throws NotCompliantMBeanException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (cause instanceof SecurityException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            throw (SecurityException) cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (cause instanceof NotCompliantMBeanException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            throw (NotCompliantMBeanException)cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        final String classname =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                (notCompliant==null)?"null class":notCompliant.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        final String reason =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                (cause==null)?"Not compliant":cause.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        final NotCompliantMBeanException res =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                new NotCompliantMBeanException(classname+": "+reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        res.initCause(cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        throw res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    // Convert a value from an annotation element to a descriptor field value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    // E.g. with @interface Foo {class value()} an annotation @Foo(String.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    // will produce a Descriptor field value "java.lang.String"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    private static Object annotationToField(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        // An annotation element cannot have a null value but never mind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (x == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (x instanceof Number || x instanceof String ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                x instanceof Character || x instanceof Boolean ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                x instanceof String[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        // Remaining possibilities: array of primitive (e.g. int[]),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        // enum, class, array of enum or class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        Class<?> c = x.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (c.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            if (c.getComponentType().isPrimitive())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            Object[] xx = (Object[]) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            String[] ss = new String[xx.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            for (int i = 0; i < xx.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                ss[i] = (String) annotationToField(xx[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            return ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1322
diff changeset
   474
        if (x instanceof Class<?>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            return ((Class<?>) x).getName();
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1322
diff changeset
   476
        if (x instanceof Enum<?>)
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1322
diff changeset
   477
            return ((Enum<?>) x).name();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        // The only other possibility is that the value is another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        // annotation, or that the language has evolved since this code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        // was written.  We don't allow for either of those currently.
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   481
        // If it is indeed another annotation, then x will be a proxy
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   482
        // with an unhelpful name like $Proxy2.  So we extract the
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   483
        // proxy's interface to use that in the exception message.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   484
        if (Proxy.isProxyClass(c))
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   485
            c = c.getInterfaces()[0];  // array "can't be empty"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        throw new IllegalArgumentException("Illegal type for annotation " +
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   487
                "element using @DescriptorKey: " + c.getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    // This must be consistent with the check for duplicate field values in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    // ImmutableDescriptor.union.  But we don't expect to be called very
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    // often so this inefficient check should be enough.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    private static boolean equals(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        return Arrays.deepEquals(new Object[] {x}, new Object[] {y});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * Returns the XXMBean interface or null if no such interface exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @param c The interface to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @param clName The name of the class implementing this interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   503
    private static <T> Class<? super T> implementsMBean(Class<T> c, String clName) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        String clMBeanName = clName + "MBean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (c.getName().equals(clMBeanName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   508
        Class<?>[] interfaces = c.getInterfaces();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        for (int i = 0;i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            if (interfaces[i].getName().equals(clMBeanName))
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   511
                return Util.cast(interfaces[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   516
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   517
    public static Object elementFromComplex(Object complex, String element)
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   518
    throws AttributeNotFoundException {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   519
        try {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   520
            if (complex.getClass().isArray() && element.equals("length")) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   521
                return Array.getLength(complex);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   522
            } else if (complex instanceof CompositeData) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   523
                return ((CompositeData) complex).get(element);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   524
            } else {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   525
                // Java Beans introspection
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   526
                //
4328
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   527
                Class<?> clazz = complex.getClass();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   528
                Method readMethod = null;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   529
                if (BeansHelper.isAvailable()) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   530
                    Object bi = BeansHelper.getBeanInfo(clazz);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   531
                    Object[] pds = BeansHelper.getPropertyDescriptors(bi);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   532
                    for (Object pd: pds) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   533
                        if (BeansHelper.getPropertyName(pd).equals(element)) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   534
                            readMethod = BeansHelper.getReadMethod(pd);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   535
                            break;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   536
                        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   537
                    }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   538
                } else {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   539
                    // Java Beans not available so use simple introspection
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   540
                    // to locate method
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   541
                    readMethod = SimpleIntrospector.getReadMethod(clazz, element);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   542
                }
16086
0a1053497df1 8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents: 5506
diff changeset
   543
                if (readMethod != null) {
0a1053497df1 8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents: 5506
diff changeset
   544
                    ReflectUtil.checkPackageAccess(readMethod.getDeclaringClass());
0a1053497df1 8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents: 5506
diff changeset
   545
                    return MethodUtil.invoke(readMethod, complex, new Class[0]);
0a1053497df1 8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents: 5506
diff changeset
   546
                }
4328
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   547
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   548
                throw new AttributeNotFoundException(
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   549
                    "Could not find the getter method for the property " +
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   550
                    element + " using the Java Beans introspector");
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   551
            }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   552
        } catch (InvocationTargetException e) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   553
            throw new IllegalArgumentException(e);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   554
        } catch (AttributeNotFoundException e) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   555
            throw e;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   556
        } catch (Exception e) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   557
            throw EnvHelp.initCause(
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   558
                new AttributeNotFoundException(e.getMessage()), e);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   559
        }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   560
    }
4328
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   561
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   562
    /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   563
     * A simple introspector that uses reflection to analyze a class and
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   564
     * identify its "getter" methods. This class is intended for use only when
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   565
     * Java Beans is not present (which implies that there isn't explicit
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   566
     * information about the bean available).
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   567
     */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   568
    private static class SimpleIntrospector {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   569
        private SimpleIntrospector() { }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   570
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   571
        private static final String GET_METHOD_PREFIX = "get";
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   572
        private static final String IS_METHOD_PREFIX = "is";
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   573
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   574
        // cache to avoid repeated lookups
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   575
        private static final Map<Class<?>,SoftReference<List<Method>>> cache =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   576
            Collections.synchronizedMap(
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   577
                new WeakHashMap<Class<?>,SoftReference<List<Method>>> ());
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   578
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   579
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   580
         * Returns the list of methods cached for the given class, or {@code null}
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   581
         * if not cached.
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   582
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   583
        private static List<Method> getCachedMethods(Class<?> clazz) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   584
            // return cached methods if possible
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   585
            SoftReference<List<Method>> ref = cache.get(clazz);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   586
            if (ref != null) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   587
                List<Method> cached = ref.get();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   588
                if (cached != null)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   589
                    return cached;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   590
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   591
            return null;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   592
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   593
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   594
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   595
         * Returns {@code true} if the given method is a "getter" method (where
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   596
         * "getter" method is a public method of the form getXXX or "boolean
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   597
         * isXXX")
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   598
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   599
        static boolean isReadMethod(Method method) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   600
            // ignore static methods
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   601
            int modifiers = method.getModifiers();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   602
            if (Modifier.isStatic(modifiers))
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   603
                return false;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   604
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   605
            String name = method.getName();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   606
            Class<?>[] paramTypes = method.getParameterTypes();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   607
            int paramCount = paramTypes.length;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   608
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   609
            if (paramCount == 0 && name.length() > 2) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   610
                // boolean isXXX()
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   611
                if (name.startsWith(IS_METHOD_PREFIX))
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   612
                    return (method.getReturnType() == boolean.class);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   613
                // getXXX()
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   614
                if (name.length() > 3 && name.startsWith(GET_METHOD_PREFIX))
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   615
                    return (method.getReturnType() != void.class);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   616
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   617
            return false;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   618
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   619
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   620
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   621
         * Returns the list of "getter" methods for the given class. The list
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   622
         * is ordered so that isXXX methods appear before getXXX methods - this
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   623
         * is for compatability with the JavaBeans Introspector.
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   624
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   625
        static List<Method> getReadMethods(Class<?> clazz) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   626
            // return cached result if available
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   627
            List<Method> cachedResult = getCachedMethods(clazz);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   628
            if (cachedResult != null)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   629
                return cachedResult;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   630
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   631
            // get list of public methods, filtering out methods that have
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   632
            // been overridden to return a more specific type.
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   633
            List<Method> methods =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   634
                StandardMBeanIntrospector.getInstance().getMethods(clazz);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   635
            methods = MBeanAnalyzer.eliminateCovariantMethods(methods);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   636
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   637
            // filter out the non-getter methods
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   638
            List<Method> result = new LinkedList<Method>();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   639
            for (Method m: methods) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   640
                if (isReadMethod(m)) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   641
                    // favor isXXX over getXXX
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   642
                    if (m.getName().startsWith(IS_METHOD_PREFIX)) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   643
                        result.add(0, m);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   644
                    } else {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   645
                        result.add(m);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   646
                    }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   647
                }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   648
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   649
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   650
            // add result to cache
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   651
            cache.put(clazz, new SoftReference<List<Method>>(result));
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   652
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   653
            return result;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   654
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   655
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   656
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   657
         * Returns the "getter" to read the given property from the given class or
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   658
         * {@code null} if no method is found.
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   659
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   660
        static Method getReadMethod(Class<?> clazz, String property) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   661
            // first character in uppercase (compatability with JavaBeans)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   662
            property = property.substring(0, 1).toUpperCase(Locale.ENGLISH) +
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   663
                property.substring(1);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   664
            String getMethod = GET_METHOD_PREFIX + property;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   665
            String isMethod = IS_METHOD_PREFIX + property;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   666
            for (Method m: getReadMethods(clazz)) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   667
                String name = m.getName();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   668
                if (name.equals(isMethod) || name.equals(getMethod)) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   669
                    return m;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   670
                }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   671
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   672
            return null;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   673
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   674
    }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   675
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   676
    /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   677
     * A class that provides access to the JavaBeans Introspector and
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   678
     * PropertyDescriptors without creating a static dependency on java.beans.
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   679
     */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   680
    private static class BeansHelper {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   681
        private static final Class<?> introspectorClass =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   682
            getClass("java.beans.Introspector");
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   683
        private static final Class<?> beanInfoClass =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   684
            (introspectorClass == null) ? null : getClass("java.beans.BeanInfo");
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   685
        private static final Class<?> getPropertyDescriptorClass =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   686
            (beanInfoClass == null) ? null : getClass("java.beans.PropertyDescriptor");
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   687
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   688
        private static final Method getBeanInfo =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   689
            getMethod(introspectorClass, "getBeanInfo", Class.class);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   690
        private static final Method getPropertyDescriptors =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   691
            getMethod(beanInfoClass, "getPropertyDescriptors");
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   692
        private static final Method getPropertyName =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   693
            getMethod(getPropertyDescriptorClass, "getName");
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   694
        private static final Method getReadMethod =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   695
            getMethod(getPropertyDescriptorClass, "getReadMethod");
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   696
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   697
        private static Class<?> getClass(String name) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   698
            try {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   699
                return Class.forName(name, true, null);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   700
            } catch (ClassNotFoundException e) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   701
                return null;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   702
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   703
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   704
        private static Method getMethod(Class<?> clazz,
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   705
                                        String name,
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   706
                                        Class<?>... paramTypes)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   707
        {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   708
            if (clazz != null) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   709
                try {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   710
                    return clazz.getMethod(name, paramTypes);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   711
                } catch (NoSuchMethodException e) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   712
                    throw new AssertionError(e);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   713
                }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   714
            } else {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   715
                return null;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   716
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   717
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   718
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   719
        private BeansHelper() { }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   720
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   721
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   722
         * Returns {@code true} if java.beans is available.
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   723
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   724
        static boolean isAvailable() {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   725
            return introspectorClass != null;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   726
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   727
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   728
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   729
         * Invokes java.beans.Introspector.getBeanInfo(Class)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   730
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   731
        static Object getBeanInfo(Class<?> clazz) throws Exception {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   732
            try {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   733
                return getBeanInfo.invoke(null, clazz);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   734
            } catch (InvocationTargetException e) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   735
                Throwable cause = e.getCause();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   736
                if (cause instanceof Exception)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   737
                    throw (Exception)cause;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   738
                throw new AssertionError(e);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   739
            } catch (IllegalAccessException iae) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   740
                throw new AssertionError(iae);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   741
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   742
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   743
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   744
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   745
         * Invokes java.beans.BeanInfo.getPropertyDescriptors()
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   746
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   747
        static Object[] getPropertyDescriptors(Object bi) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   748
            try {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   749
                return (Object[])getPropertyDescriptors.invoke(bi);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   750
            } catch (InvocationTargetException e) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   751
                Throwable cause = e.getCause();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   752
                if (cause instanceof RuntimeException)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   753
                    throw (RuntimeException)cause;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   754
                throw new AssertionError(e);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   755
            } catch (IllegalAccessException iae) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   756
                throw new AssertionError(iae);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   757
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   758
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   759
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   760
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   761
         * Invokes java.beans.PropertyDescriptor.getName()
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   762
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   763
        static String getPropertyName(Object pd) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   764
            try {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   765
                return (String)getPropertyName.invoke(pd);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   766
            } catch (InvocationTargetException e) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   767
                Throwable cause = e.getCause();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   768
                if (cause instanceof RuntimeException)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   769
                    throw (RuntimeException)cause;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   770
                throw new AssertionError(e);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   771
            } catch (IllegalAccessException iae) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   772
                throw new AssertionError(iae);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   773
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   774
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   775
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   776
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   777
         * Invokes java.beans.PropertyDescriptor.getReadMethod()
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   778
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   779
        static Method getReadMethod(Object pd) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   780
            try {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   781
                return (Method)getReadMethod.invoke(pd);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   782
            } catch (InvocationTargetException e) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   783
                Throwable cause = e.getCause();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   784
                if (cause instanceof RuntimeException)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   785
                    throw (RuntimeException)cause;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   786
                throw new AssertionError(e);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   787
            } catch (IllegalAccessException iae) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   788
                throw new AssertionError(iae);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   789
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   790
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   791
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
}