📄 introspector.java
字号:
/* * @(#)Introspector.java 1.90 06/01/23 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.jmx.mbeanserver;// Java importimport com.sun.jmx.remote.util.EnvHelp;import java.lang.annotation.Annotation;import java.lang.reflect.AnnotatedElement;import java.lang.reflect.Constructor;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.lang.reflect.UndeclaredThrowableException;import java.util.Arrays;import java.util.HashMap;import java.util.Map;import javax.management.Descriptor;import javax.management.DescriptorKey;import javax.management.DynamicMBean;import javax.management.ImmutableDescriptor;import javax.management.MBeanInfo;import javax.management.NotCompliantMBeanException;/** * This class contains the methods for performing all the tests needed to verify * that a class represents a JMX compliant MBean. * * @since 1.5 */public class Introspector { /* * ------------------------------------------ * PRIVATE VARIABLES * ------------------------------------------ */ private static final String attributeDescription = "Attribute exposed for management"; private static final String operationDescription = "Operation exposed for management"; private static final String constructorDescription = "Public constructor of the MBean"; private static final String mbeanInfoDescription = "Information on the management interface of the MBean"; /* * ------------------------------------------ * PRIVATE CONSTRUCTORS * ------------------------------------------ */ // private constructor defined to "hide" the default public constructor private Introspector() { // ------------------------------ // ------------------------------ } /* * ------------------------------------------ * PUBLIC METHODS * ------------------------------------------ */ /** * Tell whether a MBean of the given class is a Dynamic MBean. * This method does nothing more than returning * <pre> * javax.management.DynamicMBean.class.isAssignableFrom(c) * </pre> * This method does not check for any JMX MBean compliance: * <ul><li>If <code>true</code> is returned, then instances of * <code>c</code> are DynamicMBean.</li> * <li>If <code>false</code> is returned, then no further * assumption can be made on instances of <code>c</code>. * In particular, instances of <code>c</code> may, or may not * be JMX standard MBeans.</li> * </ul> * @param c The class of the MBean under examination. * @return <code>true</code> if instances of <code>c</code> are * Dynamic MBeans, <code>false</code> otherwise. * * @since.unbundled JMX RI 1.2 **/ public static final boolean isDynamic(final Class c) { // Check if the MBean implements the DynamicMBean interface return javax.management.DynamicMBean.class.isAssignableFrom(c); } /** * Basic method for testing that a MBean of a given class can be * instantiated by the MBean server.<p> * This method checks that: * <ul><li>The given class is a concrete class.</li> * <li>The given class exposes at least one public constructor.</li> * </ul> * If these conditions are not met, throws a NotCompliantMBeanException. * @param c The class of the MBean we want to create. * @exception NotCompliantMBeanException if the MBean class makes it * impossible to instantiate the MBean from within the * MBeanServer. * * @since.unbundled JMX RI 1.2 **/ public static void testCreation(Class c) throws NotCompliantMBeanException { // Check if the class is a concrete class final int mods = c.getModifiers(); if (Modifier.isAbstract(mods) || Modifier.isInterface(mods)) { throw new NotCompliantMBeanException("MBean class must be concrete"); } // Check if the MBean has a public constructor final Constructor[] consList = c.getConstructors(); if (consList.length == 0) { throw new NotCompliantMBeanException("MBean class must have public constructor"); } } public static void checkCompliance(Class mbeanClass) throws NotCompliantMBeanException { // Is DynamicMBean? // if (DynamicMBean.class.isAssignableFrom(mbeanClass)) return; // Is Standard MBean? // final Exception mbeanException; try { getStandardMBeanInterface(mbeanClass); return; } catch (NotCompliantMBeanException e) { mbeanException = e; } // Is MXBean? // final Exception mxbeanException; try { getMXBeanInterface(mbeanClass); return; } catch (NotCompliantMBeanException e) { mxbeanException = e; } final String msg = "MBean class " + mbeanClass.getName() + " does not implement " + "DynamicMBean, neither follows the Standard MBean conventions (" + mbeanException.toString() + ") nor the MXBean conventions (" + mxbeanException.toString() + ")"; throw new NotCompliantMBeanException(msg); } public static DynamicMBean makeDynamicMBean(Object mbean) throws NotCompliantMBeanException { if (mbean instanceof DynamicMBean) return (DynamicMBean) mbean; final Class mbeanClass = mbean.getClass(); Class c = null; try { c = getStandardMBeanInterface(mbeanClass); } catch (NotCompliantMBeanException e) { // Ignore exception - we need to check whether // mbean is an MXBean first. } if (c != null) return new StandardMBeanSupport(mbean, c); try { c = getMXBeanInterface(mbeanClass); } catch (NotCompliantMBeanException e) { // Ignore exception - we cannot decide whether mbean was supposed // to be an MBean or an MXBean. We will call checkCompliance() // to generate the appropriate exception. } if (c != null) return new MXBeanSupport(mbean, c); checkCompliance(mbeanClass); throw new NotCompliantMBeanException("Not compliant"); // not reached } /** * Basic method for testing if a given class is a JMX compliant MBean. * * @param baseClass The class to be tested * * @return <code>null</code> if the MBean is a DynamicMBean, * the computed {@link javax.management.MBeanInfo} otherwise. * @exception NotCompliantMBeanException The specified class is not a * JMX compliant MBean */ public static MBeanInfo testCompliance(Class baseClass) throws NotCompliantMBeanException { // ------------------------------ // ------------------------------ // Check if the MBean implements the MBean or the Dynamic // MBean interface if (isDynamic(baseClass)) return null; return testCompliance(baseClass, null); } public static void testComplianceMXBeanInterface(Class interfaceClass) throws NotCompliantMBeanException { MXBeanIntrospector.getInstance().getAnalyzer(interfaceClass); } /** * Basic method for testing if a given class is a JMX compliant * Standard MBean. This method is only called by the legacy code * in com.sun.management.jmx. * * @param baseClass The class to be tested. * * @param mbeanInterface the MBean interface that the class implements, * or null if the interface must be determined by introspection. * * @return the computed {@link javax.management.MBeanInfo}. * @exception NotCompliantMBeanException The specified class is not a * JMX compliant Standard MBean */ public static synchronized MBeanInfo testCompliance(final Class<?> baseClass, Class<?> mbeanInterface) throws NotCompliantMBeanException { if (mbeanInterface == null) mbeanInterface = getStandardMBeanInterface(baseClass); MBeanIntrospector introspector = StandardMBeanIntrospector.getInstance(); PerInterface<?> perInterface = introspector.getPerInterface(mbeanInterface); return introspector.getClassMBeanInfo(baseClass, perInterface);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -