jdk/src/share/classes/com/sun/jmx/mbeanserver/Introspector.java
author dsamersoff
Sun, 31 Mar 2013 23:47:55 +0400
changeset 18218 42b9377e6462
parent 18213 45c8ed869a1b
child 18221 5cd0fa789013
permissions -rw-r--r--
8008124: Better compliance testing Summary: Better compliance testing Reviewed-by: dfuchs, jfdenise, skoivu, alanb
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();
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   381
            for (Method element : elements) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   382
                DescriptorKey key = element.getAnnotation(DescriptorKey.class);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   383
                if (key != null) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   384
                    String name = key.value();
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   385
                    Object value;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   386
                    try {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   387
                        value = element.invoke(a);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   388
                    } catch (RuntimeException e) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   389
                        // 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
   390
                        // security exceptions?
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   391
                        // RuntimeExceptions shouldn't be "UndeclaredThrowable".
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   392
                        // anyway...
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   393
                        //
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   394
                        throw e;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   395
                    } catch (Exception e) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   396
                        // we don't expect this
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   397
                        throw new UndeclaredThrowableException(e);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   398
                    }
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   399
                    value = annotationToField(value);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   400
                    Object oldValue = descriptorMap.put(name, value);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   401
                    if (oldValue != null && !equals(oldValue, value)) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   402
                        final String msg =
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   403
                            "Inconsistent values for descriptor field " + name +
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   404
                            " from annotations: " + value + " :: " + oldValue;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   405
                        throw new IllegalArgumentException(msg);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   406
                    }
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   407
                }
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   408
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (descriptorMap.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            return ImmutableDescriptor.EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            return new ImmutableDescriptor(descriptorMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
1699
3611e5fd6da5 6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents: 1510
diff changeset
   417
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Throws a NotCompliantMBeanException or a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @param notCompliant the class which was under examination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @param cause the raeson why NotCompliantMBeanException should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *        be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @return nothing - this method always throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *         The return type makes it possible to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *         <pre> throw throwException(clazz,cause); </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @throws SecurityException - if cause is a SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @throws NotCompliantMBeanException otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    static NotCompliantMBeanException throwException(Class<?> notCompliant,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            Throwable cause)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            throws NotCompliantMBeanException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        if (cause instanceof SecurityException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            throw (SecurityException) cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (cause instanceof NotCompliantMBeanException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            throw (NotCompliantMBeanException)cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        final String classname =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                (notCompliant==null)?"null class":notCompliant.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        final String reason =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                (cause==null)?"Not compliant":cause.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        final NotCompliantMBeanException res =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                new NotCompliantMBeanException(classname+": "+reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        res.initCause(cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        throw res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    // Convert a value from an annotation element to a descriptor field value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    // E.g. with @interface Foo {class value()} an annotation @Foo(String.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    // will produce a Descriptor field value "java.lang.String"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    private static Object annotationToField(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        // An annotation element cannot have a null value but never mind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (x == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (x instanceof Number || x instanceof String ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                x instanceof Character || x instanceof Boolean ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                x instanceof String[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        // Remaining possibilities: array of primitive (e.g. int[]),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        // enum, class, array of enum or class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        Class<?> c = x.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if (c.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            if (c.getComponentType().isPrimitive())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            Object[] xx = (Object[]) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            String[] ss = new String[xx.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            for (int i = 0; i < xx.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                ss[i] = (String) annotationToField(xx[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            return ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1322
diff changeset
   468
        if (x instanceof Class<?>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            return ((Class<?>) x).getName();
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1322
diff changeset
   470
        if (x instanceof Enum<?>)
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1322
diff changeset
   471
            return ((Enum<?>) x).name();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        // The only other possibility is that the value is another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        // annotation, or that the language has evolved since this code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        // was written.  We don't allow for either of those currently.
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   475
        // If it is indeed another annotation, then x will be a proxy
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   476
        // with an unhelpful name like $Proxy2.  So we extract the
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   477
        // proxy's interface to use that in the exception message.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   478
        if (Proxy.isProxyClass(c))
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   479
            c = c.getInterfaces()[0];  // array "can't be empty"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        throw new IllegalArgumentException("Illegal type for annotation " +
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   481
                "element using @DescriptorKey: " + c.getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    // This must be consistent with the check for duplicate field values in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    // ImmutableDescriptor.union.  But we don't expect to be called very
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    // often so this inefficient check should be enough.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    private static boolean equals(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        return Arrays.deepEquals(new Object[] {x}, new Object[] {y});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * Returns the XXMBean interface or null if no such interface exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @param c The interface to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @param clName The name of the class implementing this interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   497
    private static <T> Class<? super T> implementsMBean(Class<T> c, String clName) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        String clMBeanName = clName + "MBean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (c.getName().equals(clMBeanName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   502
        Class<?>[] interfaces = c.getInterfaces();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        for (int i = 0;i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            if (interfaces[i].getName().equals(clMBeanName))
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   505
                return Util.cast(interfaces[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   510
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   511
    public static Object elementFromComplex(Object complex, String element)
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   512
    throws AttributeNotFoundException {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   513
        try {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   514
            if (complex.getClass().isArray() && element.equals("length")) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   515
                return Array.getLength(complex);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   516
            } else if (complex instanceof CompositeData) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   517
                return ((CompositeData) complex).get(element);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   518
            } else {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   519
                // Java Beans introspection
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   520
                //
4328
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   521
                Class<?> clazz = complex.getClass();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   522
                Method readMethod = null;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   523
                if (BeansHelper.isAvailable()) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   524
                    Object bi = BeansHelper.getBeanInfo(clazz);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   525
                    Object[] pds = BeansHelper.getPropertyDescriptors(bi);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   526
                    for (Object pd: pds) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   527
                        if (BeansHelper.getPropertyName(pd).equals(element)) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   528
                            readMethod = BeansHelper.getReadMethod(pd);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   529
                            break;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   530
                        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   531
                    }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   532
                } else {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   533
                    // 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
   534
                    // to locate method
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   535
                    readMethod = SimpleIntrospector.getReadMethod(clazz, element);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   536
                }
16086
0a1053497df1 8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents: 5506
diff changeset
   537
                if (readMethod != null) {
0a1053497df1 8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents: 5506
diff changeset
   538
                    ReflectUtil.checkPackageAccess(readMethod.getDeclaringClass());
0a1053497df1 8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents: 5506
diff changeset
   539
                    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
   540
                }
4328
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   541
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   542
                throw new AttributeNotFoundException(
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   543
                    "Could not find the getter method for the property " +
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   544
                    element + " using the Java Beans introspector");
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   545
            }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   546
        } catch (InvocationTargetException e) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   547
            throw new IllegalArgumentException(e);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   548
        } catch (AttributeNotFoundException e) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   549
            throw e;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   550
        } catch (Exception e) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   551
            throw EnvHelp.initCause(
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   552
                new AttributeNotFoundException(e.getMessage()), e);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   553
        }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   554
    }
4328
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   555
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   556
    /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   557
     * 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
   558
     * 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
   559
     * 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
   560
     * information about the bean available).
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
    private static class SimpleIntrospector {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   563
        private SimpleIntrospector() { }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   564
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   565
        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
   566
        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
   567
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   568
        // cache to avoid repeated lookups
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   569
        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
   570
            Collections.synchronizedMap(
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   571
                new WeakHashMap<Class<?>,SoftReference<List<Method>>> ());
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   572
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
         * 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
   575
         * if not cached.
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   576
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   577
        private static List<Method> getCachedMethods(Class<?> clazz) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   578
            // return cached methods if possible
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   579
            SoftReference<List<Method>> ref = cache.get(clazz);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   580
            if (ref != null) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   581
                List<Method> cached = ref.get();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   582
                if (cached != null)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   583
                    return cached;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   584
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   585
            return null;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   586
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   587
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   588
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   589
         * 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
   590
         * "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
   591
         * isXXX")
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
        static boolean isReadMethod(Method method) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   594
            // ignore static methods
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   595
            int modifiers = method.getModifiers();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   596
            if (Modifier.isStatic(modifiers))
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   597
                return false;
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
            String name = method.getName();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   600
            Class<?>[] paramTypes = method.getParameterTypes();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   601
            int paramCount = paramTypes.length;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   602
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   603
            if (paramCount == 0 && name.length() > 2) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   604
                // boolean isXXX()
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   605
                if (name.startsWith(IS_METHOD_PREFIX))
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   606
                    return (method.getReturnType() == boolean.class);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   607
                // getXXX()
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   608
                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
   609
                    return (method.getReturnType() != void.class);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   610
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   611
            return false;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   612
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   613
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   614
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   615
         * 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
   616
         * 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
   617
         * is for compatability with the JavaBeans Introspector.
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
        static List<Method> getReadMethods(Class<?> clazz) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   620
            // return cached result if available
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   621
            List<Method> cachedResult = getCachedMethods(clazz);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   622
            if (cachedResult != null)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   623
                return cachedResult;
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
            // 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
   626
            // 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
   627
            List<Method> methods =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   628
                StandardMBeanIntrospector.getInstance().getMethods(clazz);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   629
            methods = MBeanAnalyzer.eliminateCovariantMethods(methods);
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
            // filter out the non-getter methods
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   632
            List<Method> result = new LinkedList<Method>();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   633
            for (Method m: methods) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   634
                if (isReadMethod(m)) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   635
                    // favor isXXX over getXXX
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   636
                    if (m.getName().startsWith(IS_METHOD_PREFIX)) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   637
                        result.add(0, m);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   638
                    } else {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   639
                        result.add(m);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   640
                    }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   641
                }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   642
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   643
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   644
            // add result to cache
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   645
            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
   646
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   647
            return result;
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
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   651
         * 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
   652
         * {@code null} if no method is found.
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   653
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   654
        static Method getReadMethod(Class<?> clazz, String property) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   655
            // first character in uppercase (compatability with JavaBeans)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   656
            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
   657
                property.substring(1);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   658
            String getMethod = GET_METHOD_PREFIX + property;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   659
            String isMethod = IS_METHOD_PREFIX + property;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   660
            for (Method m: getReadMethods(clazz)) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   661
                String name = m.getName();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   662
                if (name.equals(isMethod) || name.equals(getMethod)) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   663
                    return m;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   664
                }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   665
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   666
            return null;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   667
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   668
    }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   669
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
     * 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
   672
     * 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
   673
     */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   674
    private static class BeansHelper {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   675
        private static final Class<?> introspectorClass =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   676
            getClass("java.beans.Introspector");
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   677
        private static final Class<?> beanInfoClass =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   678
            (introspectorClass == null) ? null : getClass("java.beans.BeanInfo");
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   679
        private static final Class<?> getPropertyDescriptorClass =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   680
            (beanInfoClass == null) ? null : getClass("java.beans.PropertyDescriptor");
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   681
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   682
        private static final Method getBeanInfo =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   683
            getMethod(introspectorClass, "getBeanInfo", Class.class);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   684
        private static final Method getPropertyDescriptors =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   685
            getMethod(beanInfoClass, "getPropertyDescriptors");
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   686
        private static final Method getPropertyName =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   687
            getMethod(getPropertyDescriptorClass, "getName");
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   688
        private static final Method getReadMethod =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   689
            getMethod(getPropertyDescriptorClass, "getReadMethod");
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   690
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   691
        private static Class<?> getClass(String name) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   692
            try {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   693
                return Class.forName(name, true, null);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   694
            } catch (ClassNotFoundException e) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   695
                return null;
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
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   698
        private static Method getMethod(Class<?> clazz,
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   699
                                        String name,
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   700
                                        Class<?>... paramTypes)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   701
        {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   702
            if (clazz != null) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   703
                try {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   704
                    return clazz.getMethod(name, paramTypes);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   705
                } catch (NoSuchMethodException e) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   706
                    throw new AssertionError(e);
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
            } else {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   709
                return null;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   710
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   711
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   712
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   713
        private BeansHelper() { }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   714
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   715
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   716
         * 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
   717
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   718
        static boolean isAvailable() {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   719
            return introspectorClass != null;
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
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   723
         * Invokes java.beans.Introspector.getBeanInfo(Class)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   724
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   725
        static Object getBeanInfo(Class<?> clazz) throws Exception {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   726
            try {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   727
                return getBeanInfo.invoke(null, clazz);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   728
            } catch (InvocationTargetException e) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   729
                Throwable cause = e.getCause();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   730
                if (cause instanceof Exception)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   731
                    throw (Exception)cause;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   732
                throw new AssertionError(e);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   733
            } catch (IllegalAccessException iae) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   734
                throw new AssertionError(iae);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   735
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   736
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   737
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   738
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   739
         * Invokes java.beans.BeanInfo.getPropertyDescriptors()
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   740
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   741
        static Object[] getPropertyDescriptors(Object bi) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   742
            try {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   743
                return (Object[])getPropertyDescriptors.invoke(bi);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   744
            } catch (InvocationTargetException e) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   745
                Throwable cause = e.getCause();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   746
                if (cause instanceof RuntimeException)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   747
                    throw (RuntimeException)cause;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   748
                throw new AssertionError(e);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   749
            } catch (IllegalAccessException iae) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   750
                throw new AssertionError(iae);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   751
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   752
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   753
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   754
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   755
         * Invokes java.beans.PropertyDescriptor.getName()
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   756
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   757
        static String getPropertyName(Object pd) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   758
            try {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   759
                return (String)getPropertyName.invoke(pd);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   760
            } catch (InvocationTargetException e) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   761
                Throwable cause = e.getCause();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   762
                if (cause instanceof RuntimeException)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   763
                    throw (RuntimeException)cause;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   764
                throw new AssertionError(e);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   765
            } catch (IllegalAccessException iae) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   766
                throw new AssertionError(iae);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   767
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   768
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   769
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   770
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   771
         * Invokes java.beans.PropertyDescriptor.getReadMethod()
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   772
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   773
        static Method getReadMethod(Object pd) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   774
            try {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   775
                return (Method)getReadMethod.invoke(pd);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   776
            } catch (InvocationTargetException e) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   777
                Throwable cause = e.getCause();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   778
                if (cause instanceof RuntimeException)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   779
                    throw (RuntimeException)cause;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   780
                throw new AssertionError(e);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   781
            } catch (IllegalAccessException iae) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   782
                throw new AssertionError(iae);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   783
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   784
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   785
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
}