src/java.management/share/classes/com/sun/jmx/mbeanserver/Introspector.java
author igerasim
Wed, 16 Oct 2019 18:47:11 -0700
changeset 58659 4113f16d5109
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231859: Extra dash after the exception name in @throws clause of javadoc Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58659
4113f16d5109 8231859: Extra dash after the exception name in @throws clause of javadoc
igerasim
parents: 47216
diff changeset
     2
 * Copyright (c) 1999, 2019, 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;
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
    55
import java.security.AccessController;
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    56
import javax.management.AttributeNotFoundException;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    57
import javax.management.openmbean.CompositeData;
23581
0ca496340112 8038343: Eliminate use of reflection to access JavaBeans Introspector
alanb
parents: 23010
diff changeset
    58
16086
0a1053497df1 8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents: 5506
diff changeset
    59
import sun.reflect.misc.MethodUtil;
0a1053497df1 8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents: 5506
diff changeset
    60
import sun.reflect.misc.ReflectUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * This class contains the methods for performing all the tests needed to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * that a class represents a JMX compliant MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public class Introspector {
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
    69
    final public static boolean ALLOW_NONPUBLIC_MBEAN;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
    70
    static {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
    71
        String val = AccessController.doPrivileged(new GetPropertyAction("jdk.jmx.mbeans.allowNonPublic"));
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
    72
        ALLOW_NONPUBLIC_MBEAN = Boolean.parseBoolean(val);
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
    73
    }
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
    74
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *  PRIVATE CONSTRUCTORS
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
    // private constructor defined to "hide" the default public constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private Introspector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *  PUBLIC METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Tell whether a MBean of the given class is a Dynamic MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * This method does nothing more than returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * javax.management.DynamicMBean.class.isAssignableFrom(c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * This method does not check for any JMX MBean compliance:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <ul><li>If <code>true</code> is returned, then instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *     <code>c</code> are DynamicMBean.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *     <li>If <code>false</code> is returned, then no further
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *     assumption can be made on instances of <code>c</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *     In particular, instances of <code>c</code> may, or may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *     be JMX standard MBeans.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param c The class of the MBean under examination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @return <code>true</code> if instances of <code>c</code> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *         Dynamic MBeans, <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     **/
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1322
diff changeset
   114
    public static final boolean isDynamic(final Class<?> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // Check if the MBean implements the DynamicMBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return javax.management.DynamicMBean.class.isAssignableFrom(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Basic method for testing that a MBean of a given class can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * instantiated by the MBean server.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * This method checks that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <ul><li>The given class is a concrete class.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *     <li>The given class exposes at least one public constructor.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * If these conditions are not met, throws a NotCompliantMBeanException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param c The class of the MBean we want to create.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @exception NotCompliantMBeanException if the MBean class makes it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *            impossible to instantiate the MBean from within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *            MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     **/
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1322
diff changeset
   133
    public static void testCreation(Class<?> c)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // Check if the class is a concrete class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        final int mods = c.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (Modifier.isAbstract(mods) || Modifier.isInterface(mods)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new NotCompliantMBeanException("MBean class must be concrete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // Check if the MBean has a public constructor
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1322
diff changeset
   142
        final Constructor<?>[] consList = c.getConstructors();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (consList.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            throw new NotCompliantMBeanException("MBean class must have public constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   148
    public static void checkCompliance(Class<?> mbeanClass)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   149
    throws NotCompliantMBeanException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // Is DynamicMBean?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (DynamicMBean.class.isAssignableFrom(mbeanClass))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // Is Standard MBean?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        final Exception mbeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            getStandardMBeanInterface(mbeanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            mbeanException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        // Is MXBean?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        final Exception mxbeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            getMXBeanInterface(mbeanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            mxbeanException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            "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
   174
            "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
   175
            mbeanException.toString() + ") nor the MXBean conventions (" +
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   176
            mxbeanException.toString() + ")";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        throw new NotCompliantMBeanException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   180
    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
   181
        throws NotCompliantMBeanException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (mbean instanceof DynamicMBean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return (DynamicMBean) mbean;
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   184
        final Class<?> mbeanClass = mbean.getClass();
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   185
        Class<? super T> c = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        try {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   187
            c = Util.cast(getStandardMBeanInterface(mbeanClass));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            // Ignore exception - we need to check whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            // mbean is an MXBean first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (c != null)
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   193
            return new StandardMBeanSupport(mbean, c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        try {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 34
diff changeset
   196
            c = Util.cast(getMXBeanInterface(mbeanClass));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // Ignore exception - we cannot decide whether mbean was supposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            // to be an MBean or an MXBean. We will call checkCompliance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            // to generate the appropriate exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   202
        if (c != null)
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   203
            return new MXBeanSupport(mbean, c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        checkCompliance(mbeanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        throw new NotCompliantMBeanException("Not compliant"); // not reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Basic method for testing if a given class is a JMX compliant MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param baseClass The class to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @return <code>null</code> if the MBean is a DynamicMBean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *         the computed {@link javax.management.MBeanInfo} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @exception NotCompliantMBeanException The specified class is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *            JMX compliant MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1322
diff changeset
   218
    public static MBeanInfo testCompliance(Class<?> baseClass)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // Check if the MBean implements the MBean or the Dynamic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        // MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (isDynamic(baseClass))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return testCompliance(baseClass, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   232
    /**
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   233
     * Tests the given interface class for being a compliant MXBean interface.
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   234
     * A compliant MXBean interface is any publicly accessible interface
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   235
     * following the {@link MXBean} conventions.
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   236
     * @param interfaceClass An interface class to test for the MXBean compliance
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   237
     * @throws NotCompliantMBeanException Thrown when the tested interface
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   238
     * is not public or contradicts the {@link MXBean} conventions.
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   239
     */
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   240
    public static void testComplianceMXBeanInterface(Class<?> interfaceClass)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throws NotCompliantMBeanException {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   242
        MXBeanIntrospector.getInstance().getAnalyzer(interfaceClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   245
    /**
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   246
     * Tests the given interface class for being a compliant MBean interface.
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   247
     * A compliant MBean interface is any publicly accessible interface
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   248
     * following the {@code MBean} conventions.
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   249
     * @param interfaceClass An interface class to test for the MBean compliance
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   250
     * @throws NotCompliantMBeanException Thrown when the tested interface
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   251
     * is not public or contradicts the {@code MBean} conventions.
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   252
     */
18213
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 16107
diff changeset
   253
    public static void testComplianceMBeanInterface(Class<?> interfaceClass)
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 16107
diff changeset
   254
            throws NotCompliantMBeanException{
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 16107
diff changeset
   255
        StandardMBeanIntrospector.getInstance().getAnalyzer(interfaceClass);
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 16107
diff changeset
   256
    }
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 16107
diff changeset
   257
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Basic method for testing if a given class is a JMX compliant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Standard MBean.  This method is only called by the legacy code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * in com.sun.management.jmx.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param baseClass The class to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param mbeanInterface the MBean interface that the class implements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * or null if the interface must be determined by introspection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @return the computed {@link javax.management.MBeanInfo}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @exception NotCompliantMBeanException The specified class is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *            JMX compliant Standard MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public static synchronized MBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            testCompliance(final Class<?> baseClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                           Class<?> mbeanInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (mbeanInterface == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            mbeanInterface = getStandardMBeanInterface(baseClass);
18218
42b9377e6462 8008124: Better compliance testing
dsamersoff
parents: 18213
diff changeset
   278
        ReflectUtil.checkPackageAccess(mbeanInterface);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        MBeanIntrospector<?> introspector = StandardMBeanIntrospector.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return getClassMBeanInfo(introspector, baseClass, mbeanInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private static <M> MBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            getClassMBeanInfo(MBeanIntrospector<M> introspector,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                              Class<?> baseClass, Class<?> mbeanInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        PerInterface<M> perInterface = introspector.getPerInterface(mbeanInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return introspector.getClassMBeanInfo(baseClass, perInterface);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * MBean class. This method is only called by the legacy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * code in "com.sun.management.jmx".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @param baseClass The class to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @return The MBean interface implemented by the MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *         Return <code>null</code> if the MBean is a DynamicMBean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *         or if no MBean interface is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   302
    public static Class<?> getMBeanInterface(Class<?> baseClass) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        // Check if the given class implements the MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        // or the Dynamic MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (isDynamic(baseClass)) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return getStandardMBeanInterface(baseClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
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
     * Get the MBean interface implemented by a JMX Standard MBean class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @param baseClass The class to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @return The MBean interface implemented by the Standard MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @throws NotCompliantMBeanException The specified class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * not a JMX compliant Standard MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   323
    public static <T> Class<? super T> getStandardMBeanInterface(Class<T> baseClass)
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   324
        throws NotCompliantMBeanException {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   325
            Class<? super T> current = baseClass;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   326
            Class<? super T> mbeanInterface = null;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   327
            while (current != null) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   328
                mbeanInterface =
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   329
                    findMBeanInterface(current, current.getName());
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   330
                if (mbeanInterface != null) break;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   331
                current = current.getSuperclass();
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   332
            }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   333
                if (mbeanInterface != null) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   334
                    return mbeanInterface;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   335
            } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                "Class " + baseClass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                " is not a JMX compliant Standard MBean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            throw new NotCompliantMBeanException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Get the MXBean interface implemented by a JMX MXBean class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @param baseClass The class to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @return The MXBean interface implemented by the MXBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @throws NotCompliantMBeanException The specified class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * not a JMX compliant MXBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   353
    public static <T> Class<? super T> getMXBeanInterface(Class<T> baseClass)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            return MXBeanSupport.findMXBeanInterface(baseClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            throw throwException(baseClass,e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *  PRIVATE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Try to find the MBean interface corresponding to the class aName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * - i.e. <i>aName</i>MBean, from within aClass and its superclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     **/
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   373
    private static <T> Class<? super T> findMBeanInterface(
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   374
            Class<T> aClass, String aName) {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   375
        Class<? super T> current = aClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        while (current != null) {
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   377
            final Class<?>[] interfaces = current.getInterfaces();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            final int len = interfaces.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            for (int i=0;i<len;i++)  {
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   380
                Class<? super T> inter = Util.cast(interfaces[i]);
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   381
                inter = implementsMBean(inter, aName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                if (inter != null) return inter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            current = current.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   389
    public static Descriptor descriptorForElement(final AnnotatedElement elmt) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if (elmt == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            return ImmutableDescriptor.EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        final Annotation[] annots = elmt.getAnnotations();
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   393
        return descriptorForAnnotations(annots);
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   394
    }
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   395
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public static Descriptor descriptorForAnnotations(Annotation[] annots) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (annots.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            return ImmutableDescriptor.EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        Map<String, Object> descriptorMap = new HashMap<String, Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        for (Annotation a : annots) {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   401
            Class<? extends Annotation> c = a.annotationType();
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   402
            Method[] elements = c.getMethods();
18221
5cd0fa789013 8008611: Better handling of annotations in JMX
egahlin
parents: 18218
diff changeset
   403
            boolean packageAccess = false;
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   404
            for (Method element : elements) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   405
                DescriptorKey key = element.getAnnotation(DescriptorKey.class);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   406
                if (key != null) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   407
                    String name = key.value();
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   408
                    Object value;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   409
                    try {
18221
5cd0fa789013 8008611: Better handling of annotations in JMX
egahlin
parents: 18218
diff changeset
   410
                        // Avoid checking access more than once per annotation
5cd0fa789013 8008611: Better handling of annotations in JMX
egahlin
parents: 18218
diff changeset
   411
                        if (!packageAccess) {
5cd0fa789013 8008611: Better handling of annotations in JMX
egahlin
parents: 18218
diff changeset
   412
                            ReflectUtil.checkPackageAccess(c);
5cd0fa789013 8008611: Better handling of annotations in JMX
egahlin
parents: 18218
diff changeset
   413
                            packageAccess = true;
5cd0fa789013 8008611: Better handling of annotations in JMX
egahlin
parents: 18218
diff changeset
   414
                        }
5cd0fa789013 8008611: Better handling of annotations in JMX
egahlin
parents: 18218
diff changeset
   415
                        value = MethodUtil.invoke(element, a, null);
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   416
                    } catch (RuntimeException e) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   417
                        // 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
   418
                        // security exceptions?
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   419
                        // RuntimeExceptions shouldn't be "UndeclaredThrowable".
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   420
                        // anyway...
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   421
                        //
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   422
                        throw e;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   423
                    } catch (Exception e) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   424
                        // we don't expect this
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   425
                        throw new UndeclaredThrowableException(e);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   426
                    }
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   427
                    value = annotationToField(value);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   428
                    Object oldValue = descriptorMap.put(name, value);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   429
                    if (oldValue != null && !equals(oldValue, value)) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   430
                        final String msg =
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   431
                            "Inconsistent values for descriptor field " + name +
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   432
                            " from annotations: " + value + " :: " + oldValue;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   433
                        throw new IllegalArgumentException(msg);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   434
                    }
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   435
                }
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   436
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (descriptorMap.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            return ImmutableDescriptor.EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            return new ImmutableDescriptor(descriptorMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
1699
3611e5fd6da5 6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents: 1510
diff changeset
   445
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * Throws a NotCompliantMBeanException or a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @param notCompliant the class which was under examination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @param cause the raeson why NotCompliantMBeanException should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *        be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @return nothing - this method always throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *         The return type makes it possible to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *         <pre> throw throwException(clazz,cause); </pre>
58659
4113f16d5109 8231859: Extra dash after the exception name in @throws clause of javadoc
igerasim
parents: 47216
diff changeset
   453
     * @throws SecurityException   if cause is a SecurityException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @throws NotCompliantMBeanException otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    static NotCompliantMBeanException throwException(Class<?> notCompliant,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            Throwable cause)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            throws NotCompliantMBeanException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if (cause instanceof SecurityException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            throw (SecurityException) cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if (cause instanceof NotCompliantMBeanException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            throw (NotCompliantMBeanException)cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        final String classname =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                (notCompliant==null)?"null class":notCompliant.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        final String reason =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                (cause==null)?"Not compliant":cause.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        final NotCompliantMBeanException res =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                new NotCompliantMBeanException(classname+": "+reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        res.initCause(cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        throw res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    // Convert a value from an annotation element to a descriptor field value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    // E.g. with @interface Foo {class value()} an annotation @Foo(String.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    // will produce a Descriptor field value "java.lang.String"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    private static Object annotationToField(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        // An annotation element cannot have a null value but never mind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        if (x == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (x instanceof Number || x instanceof String ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                x instanceof Character || x instanceof Boolean ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                x instanceof String[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        // Remaining possibilities: array of primitive (e.g. int[]),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        // enum, class, array of enum or class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        Class<?> c = x.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (c.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            if (c.getComponentType().isPrimitive())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            Object[] xx = (Object[]) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            String[] ss = new String[xx.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            for (int i = 0; i < xx.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                ss[i] = (String) annotationToField(xx[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            return ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1322
diff changeset
   496
        if (x instanceof Class<?>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            return ((Class<?>) x).getName();
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1322
diff changeset
   498
        if (x instanceof Enum<?>)
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1322
diff changeset
   499
            return ((Enum<?>) x).name();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        // The only other possibility is that the value is another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        // annotation, or that the language has evolved since this code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        // was written.  We don't allow for either of those currently.
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   503
        // If it is indeed another annotation, then x will be a proxy
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   504
        // with an unhelpful name like $Proxy2.  So we extract the
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   505
        // proxy's interface to use that in the exception message.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   506
        if (Proxy.isProxyClass(c))
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   507
            c = c.getInterfaces()[0];  // array "can't be empty"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        throw new IllegalArgumentException("Illegal type for annotation " +
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   509
                "element using @DescriptorKey: " + c.getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    // This must be consistent with the check for duplicate field values in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    // ImmutableDescriptor.union.  But we don't expect to be called very
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    // often so this inefficient check should be enough.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    private static boolean equals(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        return Arrays.deepEquals(new Object[] {x}, new Object[] {y});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * Returns the XXMBean interface or null if no such interface exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @param c The interface to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @param clName The name of the class implementing this interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   525
    private static <T> Class<? super T> implementsMBean(Class<T> c, String clName) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        String clMBeanName = clName + "MBean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        if (c.getName().equals(clMBeanName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   530
        Class<?>[] interfaces = c.getInterfaces();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        for (int i = 0;i < interfaces.length; i++) {
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   532
            if (interfaces[i].getName().equals(clMBeanName) &&
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   533
                (Modifier.isPublic(interfaces[i].getModifiers()) ||
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   534
                 ALLOW_NONPUBLIC_MBEAN)) {
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   535
                return Util.cast(interfaces[i]);
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18221
diff changeset
   536
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   541
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   542
    public static Object elementFromComplex(Object complex, String element)
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   543
    throws AttributeNotFoundException {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   544
        try {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   545
            if (complex.getClass().isArray() && element.equals("length")) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   546
                return Array.getLength(complex);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   547
            } else if (complex instanceof CompositeData) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   548
                return ((CompositeData) complex).get(element);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   549
            } else {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   550
                // Java Beans introspection
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   551
                //
4328
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   552
                Class<?> clazz = complex.getClass();
32101
d7400a695eec 8129215: com.sun.jmx.mbeanserver.Introspector may provide results inconsistent with the JavaBeans Introspector
jbachorik
parents: 30906
diff changeset
   553
                Method readMethod;
d7400a695eec 8129215: com.sun.jmx.mbeanserver.Introspector may provide results inconsistent with the JavaBeans Introspector
jbachorik
parents: 30906
diff changeset
   554
                if (JavaBeansAccessor.isAvailable()) {
d7400a695eec 8129215: com.sun.jmx.mbeanserver.Introspector may provide results inconsistent with the JavaBeans Introspector
jbachorik
parents: 30906
diff changeset
   555
                    readMethod = JavaBeansAccessor.getReadMethod(clazz, element);
d7400a695eec 8129215: com.sun.jmx.mbeanserver.Introspector may provide results inconsistent with the JavaBeans Introspector
jbachorik
parents: 30906
diff changeset
   556
                } else {
4328
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   557
                    // 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
   558
                    // to locate method
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   559
                    readMethod = SimpleIntrospector.getReadMethod(clazz, element);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   560
                }
16086
0a1053497df1 8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents: 5506
diff changeset
   561
                if (readMethod != null) {
0a1053497df1 8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents: 5506
diff changeset
   562
                    ReflectUtil.checkPackageAccess(readMethod.getDeclaringClass());
24871
224e298c3978 8044865: Fix raw and unchecked lint warnings in management-related code
sjiang
parents: 23581
diff changeset
   563
                    return MethodUtil.invoke(readMethod, complex, new Class<?>[0]);
16086
0a1053497df1 8000539: JMX implementation allows invocation of methods of a system class
dsamersoff
parents: 5506
diff changeset
   564
                }
4328
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   565
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   566
                throw new AttributeNotFoundException(
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   567
                    "Could not find the getter method for the property " +
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   568
                    element + " using the Java Beans introspector");
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   569
            }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   570
        } catch (InvocationTargetException e) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   571
            throw new IllegalArgumentException(e);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   572
        } catch (AttributeNotFoundException e) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   573
            throw e;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   574
        } catch (Exception e) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   575
            throw EnvHelp.initCause(
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   576
                new AttributeNotFoundException(e.getMessage()), e);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   577
        }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   578
    }
4328
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   579
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   580
    /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   581
     * 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
   582
     * 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
   583
     * 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
   584
     * information about the bean available).
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   585
     */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   586
    private static class SimpleIntrospector {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   587
        private SimpleIntrospector() { }
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
        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
   590
        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
   591
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   592
        // cache to avoid repeated lookups
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   593
        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
   594
            Collections.synchronizedMap(
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   595
                new WeakHashMap<Class<?>,SoftReference<List<Method>>> ());
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   596
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   597
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   598
         * 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
   599
         * if not cached.
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   600
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   601
        private static List<Method> getCachedMethods(Class<?> clazz) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   602
            // return cached methods if possible
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   603
            SoftReference<List<Method>> ref = cache.get(clazz);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   604
            if (ref != null) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   605
                List<Method> cached = ref.get();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   606
                if (cached != null)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   607
                    return cached;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   608
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   609
            return null;
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
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
         * 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
   614
         * "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
   615
         * isXXX")
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   616
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   617
        static boolean isReadMethod(Method method) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   618
            // ignore static methods
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   619
            int modifiers = method.getModifiers();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   620
            if (Modifier.isStatic(modifiers))
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   621
                return false;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   622
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   623
            String name = method.getName();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   624
            Class<?>[] paramTypes = method.getParameterTypes();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   625
            int paramCount = paramTypes.length;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   626
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   627
            if (paramCount == 0 && name.length() > 2) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   628
                // boolean isXXX()
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   629
                if (name.startsWith(IS_METHOD_PREFIX))
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   630
                    return (method.getReturnType() == boolean.class);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   631
                // getXXX()
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   632
                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
   633
                    return (method.getReturnType() != void.class);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   634
            }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   635
            return false;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   636
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   637
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   638
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   639
         * 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
   640
         * is ordered so that isXXX methods appear before getXXX methods - this
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 18805
diff changeset
   641
         * is for compatibility with the JavaBeans Introspector.
4328
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
        static List<Method> getReadMethods(Class<?> clazz) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   644
            // return cached result if available
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   645
            List<Method> cachedResult = getCachedMethods(clazz);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   646
            if (cachedResult != null)
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   647
                return cachedResult;
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
            // 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
   650
            // 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
   651
            List<Method> methods =
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   652
                StandardMBeanIntrospector.getInstance().getMethods(clazz);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   653
            methods = MBeanAnalyzer.eliminateCovariantMethods(methods);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   654
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   655
            // filter out the non-getter methods
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   656
            List<Method> result = new LinkedList<Method>();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   657
            for (Method m: methods) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   658
                if (isReadMethod(m)) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   659
                    // favor isXXX over getXXX
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   660
                    if (m.getName().startsWith(IS_METHOD_PREFIX)) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   661
                        result.add(0, m);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   662
                    } else {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   663
                        result.add(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
            }
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
            // add result to cache
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   669
            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
   670
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   671
            return result;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   672
        }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   673
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   674
        /**
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   675
         * 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
   676
         * {@code null} if no method is found.
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   677
         */
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   678
        static Method getReadMethod(Class<?> clazz, String property) {
32101
d7400a695eec 8129215: com.sun.jmx.mbeanserver.Introspector may provide results inconsistent with the JavaBeans Introspector
jbachorik
parents: 30906
diff changeset
   679
            if (Character.isUpperCase(property.charAt(0))) {
d7400a695eec 8129215: com.sun.jmx.mbeanserver.Introspector may provide results inconsistent with the JavaBeans Introspector
jbachorik
parents: 30906
diff changeset
   680
                // the property name must start with a lower-case letter
d7400a695eec 8129215: com.sun.jmx.mbeanserver.Introspector may provide results inconsistent with the JavaBeans Introspector
jbachorik
parents: 30906
diff changeset
   681
                return null;
d7400a695eec 8129215: com.sun.jmx.mbeanserver.Introspector may provide results inconsistent with the JavaBeans Introspector
jbachorik
parents: 30906
diff changeset
   682
            }
d7400a695eec 8129215: com.sun.jmx.mbeanserver.Introspector may provide results inconsistent with the JavaBeans Introspector
jbachorik
parents: 30906
diff changeset
   683
            // first character after 'get/is' prefix must be in uppercase
d7400a695eec 8129215: com.sun.jmx.mbeanserver.Introspector may provide results inconsistent with the JavaBeans Introspector
jbachorik
parents: 30906
diff changeset
   684
            // (compatibility with JavaBeans)
4328
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   685
            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
   686
                property.substring(1);
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   687
            String getMethod = GET_METHOD_PREFIX + property;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   688
            String isMethod = IS_METHOD_PREFIX + property;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   689
            for (Method m: getReadMethods(clazz)) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   690
                String name = m.getName();
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   691
                if (name.equals(isMethod) || name.equals(getMethod)) {
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   692
                    return m;
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   693
                }
9591511c1d88 6888171: JMX Monitor API should not require JavaBeans to be present
alanb
parents: 4156
diff changeset
   694
            }
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
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
}