jdk/src/share/classes/com/sun/jmx/mbeanserver/Introspector.java
author emcmanus
Thu, 05 Jun 2008 13:42:47 +0200
changeset 687 874e25a9844a
parent 34 2d042367885f
child 833 bfa2bef7517c
child 715 f16baef3a20e
permissions -rw-r--r--
6562936: Support custom type mappings in MXBeans Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.jmx.mbeanserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.annotation.Annotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.AnnotatedElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.UndeclaredThrowableException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.Descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.DescriptorKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.DynamicMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.ImmutableDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.MBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.NotCompliantMBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    45
import com.sun.jmx.remote.util.EnvHelp;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    46
import java.beans.BeanInfo;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    47
import java.beans.PropertyDescriptor;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    48
import java.lang.reflect.Array;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    49
import java.lang.reflect.InvocationTargetException;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    50
import javax.management.AttributeNotFoundException;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    51
import javax.management.openmbean.CompositeData;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
    52
import javax.management.openmbean.MXBeanMappingFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * This class contains the methods for performing all the tests needed to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * that a class represents a JMX compliant MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class Introspector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *  PRIVATE CONSTRUCTORS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // private constructor defined to "hide" the default public constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private Introspector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *  PUBLIC METHODS
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
     * Tell whether a MBean of the given class is a Dynamic MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * This method does nothing more than returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * javax.management.DynamicMBean.class.isAssignableFrom(c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * This method does not check for any JMX MBean compliance:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * <ul><li>If <code>true</code> is returned, then instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *     <code>c</code> are DynamicMBean.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *     <li>If <code>false</code> is returned, then no further
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *     assumption can be made on instances of <code>c</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *     In particular, instances of <code>c</code> may, or may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *     be JMX standard MBeans.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param c The class of the MBean under examination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @return <code>true</code> if instances of <code>c</code> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *         Dynamic MBeans, <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public static final boolean isDynamic(final Class c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // Check if the MBean implements the DynamicMBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return javax.management.DynamicMBean.class.isAssignableFrom(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Basic method for testing that a MBean of a given class can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * instantiated by the MBean server.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * This method checks that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <ul><li>The given class is a concrete class.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *     <li>The given class exposes at least one public constructor.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * If these conditions are not met, throws a NotCompliantMBeanException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param c The class of the MBean we want to create.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @exception NotCompliantMBeanException if the MBean class makes it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *            impossible to instantiate the MBean from within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *            MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public static void testCreation(Class c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // Check if the class is a concrete class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        final int mods = c.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (Modifier.isAbstract(mods) || Modifier.isInterface(mods)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new NotCompliantMBeanException("MBean class must be concrete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // Check if the MBean has a public constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        final Constructor[] consList = c.getConstructors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (consList.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new NotCompliantMBeanException("MBean class must have public constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public static void checkCompliance(Class mbeanClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // Is DynamicMBean?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (DynamicMBean.class.isAssignableFrom(mbeanClass))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // Is Standard MBean?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        final Exception mbeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            getStandardMBeanInterface(mbeanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            mbeanException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // Is MXBean?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        final Exception mxbeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            getMXBeanInterface(mbeanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            mxbeanException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            "MBean class " + mbeanClass.getName() + " does not implement " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            "DynamicMBean, neither follows the Standard MBean conventions (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            mbeanException.toString() + ") nor the MXBean conventions (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            mxbeanException.toString() + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        throw new NotCompliantMBeanException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   168
    public static <T> DynamicMBean makeDynamicMBean(T mbean)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   169
    throws NotCompliantMBeanException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   170
        if (mbean == null)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   171
            throw new NotCompliantMBeanException("Null MBean object");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (mbean instanceof DynamicMBean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return (DynamicMBean) mbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        final Class mbeanClass = mbean.getClass();
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   175
        Class<? super T> c = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        try {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   177
            c = Util.cast(getStandardMBeanInterface(mbeanClass));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // Ignore exception - we need to check whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // mbean is an MXBean first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (c != null)
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   183
            return new StandardMBeanSupport(mbean, c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        try {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   186
            c = Util.cast(getMXBeanInterface(mbeanClass));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            // Ignore exception - we cannot decide whether mbean was supposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            // to be an MBean or an MXBean. We will call checkCompliance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            // to generate the appropriate exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   192
        if (c != null) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   193
            MXBeanMappingFactory factory = MXBeanMappingFactory.forInterface(c);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   194
            return new MXBeanSupport(mbean, c, factory);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   195
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        checkCompliance(mbeanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        throw new NotCompliantMBeanException("Not compliant"); // not reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Basic method for testing if a given class is a JMX compliant MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param baseClass The class to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @return <code>null</code> if the MBean is a DynamicMBean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *         the computed {@link javax.management.MBeanInfo} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @exception NotCompliantMBeanException The specified class is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *            JMX compliant MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public static MBeanInfo testCompliance(Class baseClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        // Check if the MBean implements the MBean or the Dynamic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        // MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (isDynamic(baseClass))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return testCompliance(baseClass, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   224
    public static void testComplianceMXBeanInterface(Class interfaceClass,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   225
                                                     MXBeanMappingFactory factory)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            throws NotCompliantMBeanException {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   227
        MXBeanIntrospector.getInstance(factory).getAnalyzer(interfaceClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Basic method for testing if a given class is a JMX compliant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Standard MBean.  This method is only called by the legacy code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * in com.sun.management.jmx.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param baseClass The class to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param mbeanInterface the MBean interface that the class implements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * or null if the interface must be determined by introspection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @return the computed {@link javax.management.MBeanInfo}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @exception NotCompliantMBeanException The specified class is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *            JMX compliant Standard MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public static synchronized MBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            testCompliance(final Class<?> baseClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                           Class<?> mbeanInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (mbeanInterface == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            mbeanInterface = getStandardMBeanInterface(baseClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        MBeanIntrospector<?> introspector = StandardMBeanIntrospector.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return getClassMBeanInfo(introspector, baseClass, mbeanInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    private static <M> MBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            getClassMBeanInfo(MBeanIntrospector<M> introspector,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                              Class<?> baseClass, Class<?> mbeanInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        PerInterface<M> perInterface = introspector.getPerInterface(mbeanInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return introspector.getClassMBeanInfo(baseClass, perInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Get the MBean interface implemented by a JMX Standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * MBean class. This method is only called by the legacy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * code in "com.sun.management.jmx".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param baseClass The class to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @return The MBean interface implemented by the MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *         Return <code>null</code> if the MBean is a DynamicMBean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *         or if no MBean interface is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public static Class getMBeanInterface(Class baseClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // Check if the given class implements the MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // or the Dynamic MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (isDynamic(baseClass)) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return getStandardMBeanInterface(baseClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Get the MBean interface implemented by a JMX Standard MBean class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param baseClass The class to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @return The MBean interface implemented by the Standard MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @throws NotCompliantMBeanException The specified class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * not a JMX compliant Standard MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public static Class getStandardMBeanInterface(Class baseClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        Class current = baseClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        Class mbeanInterface = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        while (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            mbeanInterface =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                findMBeanInterface(current, current.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (mbeanInterface != null) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            current = current.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (mbeanInterface != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return mbeanInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                "Class " + baseClass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                " is not a JMX compliant Standard MBean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            throw new NotCompliantMBeanException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Get the MXBean interface implemented by a JMX MXBean class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @param baseClass The class to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @return The MXBean interface implemented by the MXBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @throws NotCompliantMBeanException The specified class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * not a JMX compliant MXBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public static Class getMXBeanInterface(Class baseClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return MXBeanSupport.findMXBeanInterface(baseClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            throw throwException(baseClass,e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   333
    public static <T> Class<? super T> getStandardOrMXBeanInterface(
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   334
            Class<T> baseClass, boolean mxbean)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   335
    throws NotCompliantMBeanException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   336
        if (mxbean)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   337
            return getMXBeanInterface(baseClass);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   338
        else
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   339
            return getStandardMBeanInterface(baseClass);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   340
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   341
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *  PRIVATE METHODS
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Try to find the MBean interface corresponding to the class aName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * - i.e. <i>aName</i>MBean, from within aClass and its superclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    private static Class findMBeanInterface(Class aClass, String aName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        Class current = aClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        while (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            final Class[] interfaces = current.getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            final int len = interfaces.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            for (int i=0;i<len;i++)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                final Class inter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    implementsMBean(interfaces[i], aName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                if (inter != null) return inter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            current = current.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public static Descriptor descriptorForElement(final AnnotatedElement elmt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (elmt == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return ImmutableDescriptor.EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        final Annotation[] annots = elmt.getAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return descriptorForAnnotations(annots);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public static Descriptor descriptorForAnnotations(Annotation[] annots) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (annots.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            return ImmutableDescriptor.EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        Map<String, Object> descriptorMap = new HashMap<String, Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        for (Annotation a : annots) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            Class<? extends Annotation> c = a.annotationType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            Method[] elements = c.getMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            for (Method element : elements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                DescriptorKey key = element.getAnnotation(DescriptorKey.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    String name = key.value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    Object value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        value = element.invoke(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        // we don't expect this - except for possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                        // security exceptions?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                        // RuntimeExceptions shouldn't be "UndeclaredThrowable".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                        // anyway...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                        throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                        // we don't expect this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                        throw new UndeclaredThrowableException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    value = annotationToField(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    Object oldValue = descriptorMap.put(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    if (oldValue != null && !equals(oldValue, value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                        final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                            "Inconsistent values for descriptor field " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                            " from annotations: " + value + " :: " + oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                        throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (descriptorMap.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            return ImmutableDescriptor.EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            return new ImmutableDescriptor(descriptorMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Throws a NotCompliantMBeanException or a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @param notCompliant the class which was under examination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @param cause the raeson why NotCompliantMBeanException should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *        be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @return nothing - this method always throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *         The return type makes it possible to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *         <pre> throw throwException(clazz,cause); </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @throws SecurityException - if cause is a SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @throws NotCompliantMBeanException otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    static NotCompliantMBeanException throwException(Class<?> notCompliant,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            Throwable cause)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            throws NotCompliantMBeanException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (cause instanceof SecurityException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            throw (SecurityException) cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        if (cause instanceof NotCompliantMBeanException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            throw (NotCompliantMBeanException)cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        final String classname =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                (notCompliant==null)?"null class":notCompliant.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        final String reason =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                (cause==null)?"Not compliant":cause.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        final NotCompliantMBeanException res =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                new NotCompliantMBeanException(classname+": "+reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        res.initCause(cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        throw res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    // Convert a value from an annotation element to a descriptor field value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    // E.g. with @interface Foo {class value()} an annotation @Foo(String.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    // will produce a Descriptor field value "java.lang.String"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    private static Object annotationToField(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        // An annotation element cannot have a null value but never mind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (x == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (x instanceof Number || x instanceof String ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                x instanceof Character || x instanceof Boolean ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                x instanceof String[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        // Remaining possibilities: array of primitive (e.g. int[]),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        // enum, class, array of enum or class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        Class<?> c = x.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        if (c.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            if (c.getComponentType().isPrimitive())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            Object[] xx = (Object[]) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            String[] ss = new String[xx.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            for (int i = 0; i < xx.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                ss[i] = (String) annotationToField(xx[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            return ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (x instanceof Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            return ((Class<?>) x).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if (x instanceof Enum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            return ((Enum) x).name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        // The only other possibility is that the value is another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        // annotation, or that the language has evolved since this code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        // was written.  We don't allow for either of those currently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        throw new IllegalArgumentException("Illegal type for annotation " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                "element: " + x.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    // This must be consistent with the check for duplicate field values in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    // ImmutableDescriptor.union.  But we don't expect to be called very
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    // often so this inefficient check should be enough.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    private static boolean equals(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return Arrays.deepEquals(new Object[] {x}, new Object[] {y});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Returns the XXMBean interface or null if no such interface exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @param c The interface to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @param clName The name of the class implementing this interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    private static Class implementsMBean(Class c, String clName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        String clMBeanName = clName + "MBean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if (c.getName().equals(clMBeanName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        Class[] interfaces = c.getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        for (int i = 0;i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            if (interfaces[i].getName().equals(clMBeanName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                return interfaces[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   506
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   507
    public static Object elementFromComplex(Object complex, String element)
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   508
    throws AttributeNotFoundException {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   509
        try {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   510
            if (complex.getClass().isArray() && element.equals("length")) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   511
                return Array.getLength(complex);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   512
            } else if (complex instanceof CompositeData) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   513
                return ((CompositeData) complex).get(element);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   514
            } else {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   515
                // Java Beans introspection
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   516
                //
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   517
                BeanInfo bi = java.beans.Introspector.getBeanInfo(complex.getClass());
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   518
                PropertyDescriptor[] pds = bi.getPropertyDescriptors();
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   519
                for (PropertyDescriptor pd : pds)
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   520
                    if (pd.getName().equals(element))
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   521
                        return pd.getReadMethod().invoke(complex);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   522
                throw new AttributeNotFoundException(
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   523
                    "Could not find the getter method for the property " +
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   524
                    element + " using the Java Beans introspector");
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   525
            }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   526
        } catch (InvocationTargetException e) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   527
            throw new IllegalArgumentException(e);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   528
        } catch (AttributeNotFoundException e) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   529
            throw e;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   530
        } catch (Exception e) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   531
            throw EnvHelp.initCause(
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   532
                new AttributeNotFoundException(e.getMessage()), e);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   533
        }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   534
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
}