jdk/src/share/classes/com/sun/jmx/mbeanserver/MBeanAnalyzer.java
changeset 4156 acaa49a2768a
parent 834 dc74d4ddc28e
child 5506 202f599c92aa
equal deleted inserted replaced
4155:460e37d40f12 4156:acaa49a2768a
    31 import java.util.Arrays;
    31 import java.util.Arrays;
    32 import java.util.Comparator;
    32 import java.util.Comparator;
    33 import java.util.List;
    33 import java.util.List;
    34 import java.util.Map;
    34 import java.util.Map;
    35 import java.util.Set;
    35 import java.util.Set;
    36 import javax.management.MBean;
       
    37 import javax.management.MXBean;
       
    38 import javax.management.ManagedAttribute;
       
    39 import javax.management.ManagedOperation;
       
    40 import javax.management.NotCompliantMBeanException;
    36 import javax.management.NotCompliantMBeanException;
    41 
    37 
    42 /**
    38 /**
    43  * <p>An analyzer for a given MBean interface.  The analyzer can
    39  * <p>An analyzer for a given MBean interface.  The analyzer can
    44  * be for Standard MBeans or MXBeans, depending on the MBeanIntrospector
    40  * be for Standard MBeans or MXBeans, depending on the MBeanIntrospector
    53  *
    49  *
    54  * @since 1.6
    50  * @since 1.6
    55  */
    51  */
    56 class MBeanAnalyzer<M> {
    52 class MBeanAnalyzer<M> {
    57 
    53 
    58     static interface MBeanVisitor<M, X extends Exception> {
    54     static interface MBeanVisitor<M> {
    59         public void visitAttribute(String attributeName,
    55         public void visitAttribute(String attributeName,
    60                 M getter,
    56                 M getter,
    61                 M setter) throws X;
    57                 M setter);
    62         public void visitOperation(String operationName,
    58         public void visitOperation(String operationName,
    63                 M operation) throws X;
    59                 M operation);
    64     }
    60     }
    65 
    61 
    66     <X extends Exception> void visit(MBeanVisitor<M, X> visitor) throws X {
    62     void visit(MBeanVisitor<M> visitor) {
    67         // visit attributes
    63         // visit attributes
    68         for (Map.Entry<String, AttrMethods<M>> entry : attrMap.entrySet()) {
    64         for (Map.Entry<String, AttrMethods<M>> entry : attrMap.entrySet()) {
    69             String name = entry.getKey();
    65             String name = entry.getKey();
    70             AttrMethods<M> am = entry.getValue();
    66             AttrMethods<M> am = entry.getValue();
    71             visitor.visitAttribute(name, am.getter, am.setter);
    67             visitor.visitAttribute(name, am.getter, am.setter);
   106     }
   102     }
   107 
   103 
   108     private MBeanAnalyzer(Class<?> mbeanType,
   104     private MBeanAnalyzer(Class<?> mbeanType,
   109             MBeanIntrospector<M> introspector)
   105             MBeanIntrospector<M> introspector)
   110             throws NotCompliantMBeanException {
   106             throws NotCompliantMBeanException {
   111         introspector.checkCompliance(mbeanType);
   107         if (!mbeanType.isInterface()) {
       
   108             throw new NotCompliantMBeanException("Not an interface: " +
       
   109                     mbeanType.getName());
       
   110         }
   112 
   111 
   113         try {
   112         try {
   114             initMaps(mbeanType, introspector);
   113             initMaps(mbeanType, introspector);
   115         } catch (Exception x) {
   114         } catch (Exception x) {
   116             throw Introspector.throwException(mbeanType,x);
   115             throw Introspector.throwException(mbeanType,x);
   127         /* Run through the methods to detect inconsistencies and to enable
   126         /* Run through the methods to detect inconsistencies and to enable
   128            us to give getter and setter together to visitAttribute. */
   127            us to give getter and setter together to visitAttribute. */
   129         for (Method m : methods) {
   128         for (Method m : methods) {
   130             final String name = m.getName();
   129             final String name = m.getName();
   131             final int nParams = m.getParameterTypes().length;
   130             final int nParams = m.getParameterTypes().length;
   132             final boolean managedOp = m.isAnnotationPresent(ManagedOperation.class);
       
   133             final boolean managedAttr = m.isAnnotationPresent(ManagedAttribute.class);
       
   134             if (managedOp && managedAttr) {
       
   135                 throw new NotCompliantMBeanException("Method " + name +
       
   136                         " has both @ManagedOperation and @ManagedAttribute");
       
   137             }
       
   138 
   131 
   139             final M cm = introspector.mFrom(m);
   132             final M cm = introspector.mFrom(m);
   140 
   133 
   141             String attrName = "";
   134             String attrName = "";
   142             if (!managedOp) {
   135             if (name.startsWith("get"))
   143                 if (name.startsWith("get"))
   136                 attrName = name.substring(3);
   144                     attrName = name.substring(3);
   137             else if (name.startsWith("is")
   145                 else if (name.startsWith("is")
   138             && m.getReturnType() == boolean.class)
   146                          && m.getReturnType() == boolean.class)
   139                 attrName = name.substring(2);
   147                     attrName = name.substring(2);
       
   148             }
       
   149 
   140 
   150             if (attrName.length() != 0 && nParams == 0
   141             if (attrName.length() != 0 && nParams == 0
   151                     && m.getReturnType() != void.class && !managedOp) {
   142                     && m.getReturnType() != void.class) {
   152                 // It's a getter
   143                 // It's a getter
   153                 // Check we don't have both isX and getX
   144                 // Check we don't have both isX and getX
   154                 AttrMethods<M> am = attrMap.get(attrName);
   145                 AttrMethods<M> am = attrMap.get(attrName);
   155                 if (am == null)
   146                 if (am == null)
   156                     am = new AttrMethods<M>();
   147                     am = new AttrMethods<M>();
   163                 }
   154                 }
   164                 am.getter = cm;
   155                 am.getter = cm;
   165                 attrMap.put(attrName, am);
   156                 attrMap.put(attrName, am);
   166             } else if (name.startsWith("set") && name.length() > 3
   157             } else if (name.startsWith("set") && name.length() > 3
   167                     && nParams == 1 &&
   158                     && nParams == 1 &&
   168                     m.getReturnType() == void.class && !managedOp) {
   159                     m.getReturnType() == void.class) {
   169                 // It's a setter
   160                 // It's a setter
   170                 attrName = name.substring(3);
   161                 attrName = name.substring(3);
   171                 AttrMethods<M> am = attrMap.get(attrName);
   162                 AttrMethods<M> am = attrMap.get(attrName);
   172                 if (am == null)
   163                 if (am == null)
   173                     am = new AttrMethods<M>();
   164                     am = new AttrMethods<M>();
   176                             " has more than one setter";
   167                             " has more than one setter";
   177                     throw new NotCompliantMBeanException(msg);
   168                     throw new NotCompliantMBeanException(msg);
   178                 }
   169                 }
   179                 am.setter = cm;
   170                 am.setter = cm;
   180                 attrMap.put(attrName, am);
   171                 attrMap.put(attrName, am);
   181             } else if (managedAttr) {
       
   182                 throw new NotCompliantMBeanException("Method " + name +
       
   183                         " has @ManagedAttribute but is not a valid getter or setter");
       
   184             } else {
   172             } else {
   185                 // It's an operation
   173                 // It's an operation
   186                 List<M> cms = opMap.get(name);
   174                 List<M> cms = opMap.get(name);
   187                 if (cms == null)
   175                 if (cms == null)
   188                     cms = newList();
   176                     cms = newList();