private static Class<Object> guessMBeanInterface(Object instance) {
    Class<Object> mbeanInterface = null;
    Class<Object>[] interfaces = (Class<Object>[])instance.getClass().getInterfaces();
    for (Class<Object> anInterface : interfaces) {
      if (anInterface.getName().endsWith("MBean")) {
        mbeanInterface = anInterface;
        break;
      }
    }
    if (mbeanInterface == null) {
      throw new IllegalArgumentException("Can not find the MBean interface of class " + instance.getClass().getName());
    }
    return mbeanInterface;
  }
private static Class < Object > guessMBeanInterface ( Object instance ) { Class < Object > mbeanInterface = null ; Class < Object > [ ] interfaces = ( Class < Object > [ ] ) instance . getClass ( ) . getInterfaces ( ) ; for ( Class < Object > anInterface : interfaces ) { if ( anInterface . getName ( ) . endsWith ( "MBean" ) ) { mbeanInterface = anInterface ; break ; } } if ( mbeanInterface == null ) { throw new IllegalArgumentException ( "Can not find the MBean interface of class " + instance . getClass ( ) . getName ( ) ) ; } return mbeanInterface ; }
MBeans have multiple conventions, including:
1. name of interface is suffixed by "MBean"
2. name of implementation is the name of the interface without "MBean"
3. implementation and interface must be in the same package
To avoid the last convention, we wrap the mbean within a StandardMBean. That
requires to find the related interface.