JDKMissionControl / jmc

This mirror is deprecated - please start using https://github.com/openjdk/jmc
197 stars 46 forks source link

Impact of DynamicMBean operations is always "Unknown" #14

Open marschall opened 4 years ago

marschall commented 4 years ago

I have a DynamicMBean with a MBeanOperationInfo with an impact of MBeanOperationInfo#INFO however JMC displays the impact as "Unknown"

Code to reproduce

import java.io.IOException;
import java.lang.management.ManagementFactory;

import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.DynamicMBean;
import javax.management.JMException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.ReflectionException;

public class PrintlnMBean implements DynamicMBean {

  private final MBeanInfo mBeanInfo;

  public PrintlnMBean() {
    this.mBeanInfo = createMBeanInfo();
  }

  private static MBeanInfo createMBeanInfo() {
    String className = PrintlnMBean.class.getName();
    String description = null;
    MBeanAttributeInfo[] attributes = null;
    MBeanConstructorInfo[] constructors = null;
    MBeanOperationInfo println = new MBeanOperationInfo("println", "System.out.println",
        new MBeanParameterInfo[] {new MBeanParameterInfo("x", String.class.getName(), "The String to be printed.")},
        "void", MBeanOperationInfo.INFO);
    MBeanOperationInfo[] operations = new MBeanOperationInfo[] {println};
    MBeanNotificationInfo[] notification = null;
    return new MBeanInfo(className, description, attributes, constructors, operations, notification);
  }

  @Override
  public Object getAttribute(String attribute) throws AttributeNotFoundException {
    throw new AttributeNotFoundException(attribute);
  }

  @Override
  public void setAttribute(Attribute attribute)
      throws AttributeNotFoundException {
    throw new AttributeNotFoundException(attribute.getName());

  }

  @Override
  public AttributeList getAttributes(String[] attributes) {
    return new AttributeList();
  }

  @Override
  public AttributeList setAttributes(AttributeList attributes) {
    return new AttributeList();
  }

  @Override
  public Object invoke(String actionName, Object[] params, String[] signature)
      throws MBeanException, ReflectionException {
    if (actionName.equals("println")) {
      if (params == null || params.length != 1 || !(params[0] instanceof String)) {
        throw new MBeanException(new IllegalArgumentException("parameters must be a single string"));
      }
      String parameter = (String) params[0];
      System.out.println(parameter);
      return null;
    }
    throw new MBeanException(new IllegalArgumentException("unknown action: " + actionName));
  }

  @Override
  public MBeanInfo getMBeanInfo() {
    return this.mBeanInfo;
  }

  public static void main(String[] args) throws IOException, JMException {
    ObjectName objectName = new ObjectName("Demo:name=PrintlnMBean,type=PrintlnMBean");
    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    mBeanServer.registerMBean(new PrintlnMBean(), objectName);
    System.in.read();
  }

}

Screenshot

MBean Operation Impact