FileDocCategorySizeDatePackage
MBeanAnalyzer.javaAPI DocJava SE 6 API9870Tue Jun 10 00:22:02 BST 2008com.sun.jmx.mbeanserver

MBeanAnalyzer

public class MBeanAnalyzer extends Object

An analyzer for a given MBean interface. The analyzer can be for Standard MBeans or MXBeans, depending on the MBeanIntrospector passed at construction.

The analyzer can visit the attributes and operations of the interface, calling a caller-supplied visitor method for each one.

param
Method or ConvertingMethod according as this is a Standard MBean or an MXBean.
since
1.6

Fields Summary
private Map
opMap
private Map
attrMap
Constructors Summary
private MBeanAnalyzer(Class mbeanInterface, MBeanIntrospector introspector)

        if (!mbeanInterface.isInterface()) {
            throw new NotCompliantMBeanException("Not an interface: " +
                    mbeanInterface.getName());
        }

        try {
            initMaps(mbeanInterface, introspector);
        } catch (Exception x) {
            throw Introspector.throwException(mbeanInterface,x);
        }
    
Methods Summary
static com.sun.jmx.mbeanserver.MBeanAnalyzeranalyzer(java.lang.Class mbeanInterface, com.sun.jmx.mbeanserver.MBeanIntrospector introspector)

Return an MBeanAnalyzer for the given MBean interface and MBeanIntrospector. Calling this method twice with the same parameters may return the same object or two different but equivalent objects.

    

                                       
    // Currently it's two different but equivalent objects.  This only
    // really impacts proxy generation.  For MBean creation, the
    // cached PerInterface object for an MBean interface means that
    // an analyzer will not be recreated for a second MBean using the
    // same interface.
        
             
              
        return new MBeanAnalyzer<M>(mbeanInterface, introspector);
    
static java.util.ListeliminateCovariantMethods(java.lang.reflect.Method[] methodArray)

 
    


    /* Eliminate methods that are overridden with a covariant return type.
       Reflection will return both the original and the overriding method
       but only the overriding one is of interest.  We return the methods
       in the same order they arrived in.  This isn't required by the spec
       but existing code may depend on it and users may be used to seeing
       operations or attributes appear in a particular order.  */
     
              
        // We are assuming that you never have very many methods with the
        // same name, so it is OK to use algorithms that are quadratic
        // in the number of methods with the same name.

        final int len = methodArray.length;
        final Method[] sorted = methodArray.clone();
        Arrays.sort(sorted,MethodOrder.instance);
        final Set<Method> overridden = newSet();
        for (int i=1;i<len;i++) {
            final Method m0 = sorted[i-1];
            final Method m1 = sorted[i];
            
            // Methods that don't have the same name can't override each others
            if (!m0.getName().equals(m1.getName())) continue;
            
            // Methods that have the same name and same signature override
            // each other. In that case, the second method overrides the first,
            // due to the way we have sorted them in MethodOrder.
            if (Arrays.equals(m0.getParameterTypes(),
                    m1.getParameterTypes())) {
                overridden.add(m0);
            }
        }
        
        final List<Method> methods = newList(Arrays.asList(methodArray));
        methods.removeAll(overridden);
        return methods;
    
private voidinitMaps(java.lang.Class mbeanInterface, com.sun.jmx.mbeanserver.MBeanIntrospector introspector)

        final Method[] methodArray = mbeanInterface.getMethods();

        final List<Method> methods = eliminateCovariantMethods(methodArray);
        
        /* Run through the methods to detect inconsistencies and to enable
           us to give getter and setter together to visitAttribute. */
        for (Method m : methods) {
            String name = m.getName();

            final M cm = introspector.mFrom(m);

            String attrName = "";
            if (name.startsWith("get"))
                attrName = name.substring(3);
            else if (name.startsWith("is")
            && m.getReturnType() == boolean.class)
                attrName = name.substring(2);

            if (attrName.length() != 0 && m.getParameterTypes().length == 0
                    && m.getReturnType() != void.class) {
                // It's a getter
                // Check we don't have both isX and getX
                AttrMethods<M> am = attrMap.get(attrName);
                if (am == null)
                    am = new AttrMethods<M>();
                else {
                    if (am.getter != null) {
                        final String msg = "Attribute " + attrName +
                                " has more than one getter";
                        throw new NotCompliantMBeanException(msg);
                    }
                }
                am.getter = cm;
                attrMap.put(attrName, am);
            } else if (name.startsWith("set") && name.length() > 3
                    && m.getParameterTypes().length == 1 &&
                    m.getReturnType() == void.class) {
                // It's a setter
                attrName = name.substring(3);
                AttrMethods<M> am = attrMap.get(attrName);
                if (am == null)
                    am = new AttrMethods<M>();
                else if (am.setter != null) {
                    final String msg = "Attribute " + attrName +
                            " has more than one setter";
                    throw new NotCompliantMBeanException(msg);
                }
                am.setter = cm;
                attrMap.put(attrName, am);
            } else {
                // It's an operation
                List<M> cms = opMap.get(name);
                if (cms == null)
                    cms = newList();
                cms.add(cm);
                opMap.put(name, cms);
            }
        }
        /* Check that getters and setters are consistent. */
        for (Map.Entry<String, AttrMethods<M>> entry : attrMap.entrySet()) {
            AttrMethods<M> am = entry.getValue();
            if (!introspector.consistent(am.getter, am.setter)) {
                final String msg = "Getter and setter for " + entry.getKey() +
                        " have inconsistent types";
                throw new NotCompliantMBeanException(msg);
            }
        }
    
voidvisit(com.sun.jmx.mbeanserver.MBeanAnalyzer$MBeanVisitor visitor)

        // visit attributes
        for (Map.Entry<String, AttrMethods<M>> entry : attrMap.entrySet()) {
            String name = entry.getKey();
            AttrMethods<M> am = entry.getValue();
            visitor.visitAttribute(name, am.getter, am.setter);
        }

        // visit operations
        for (Map.Entry<String, List<M>> entry : opMap.entrySet()) {
            for (M m : entry.getValue())
                visitor.visitOperation(entry.getKey(), m);
        }