Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8219904

ClassCastException when calling FlightRecorderMXBean#getRecordings()

    Details

    • Subcomponent:
      jfr
    • Resolved In Build:
      b12
    • CPU:
      x86_64
    • OS:
      windows

      Backports

        Description

        A DESCRIPTION OF THE PROBLEM :
        When accessing FlightRecorderMXBean#getRecordings() through an MBeanServerConnection there will be a ClassCastException when creating the RecordingInfo objects:

        java.lang.ClassCastException: class java.lang.Long cannot be cast to class java.lang.Integer

        The reason is that the constructor in RecordingInfo tries to cast the "id" from the CompositeData, which is a java.lang.Long into an int.



        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Execute this method with a valid JMX Service URL:

        public static List<RecordingInfo> getRecordingInfos(String jmxServiceUrl) throws Exception {
            JMXConnector jmxConnector = JMXConnectorFactory.connect(new JMXServiceURL(jmxServiceUrl));
            MBeanServerConnection mBeanServerConnection = jmxConnector.getMBeanServerConnection();

            ObjectName objectName = new ObjectName("jdk.management.jfr:type=FlightRecorder");
            FlightRecorderMXBean flightRecorder = JMX.newMXBeanProxy(mBeanServerConnection, objectName, FlightRecorderMXBean.class);

            return flightRecorder.getRecordings();
          }

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The above method should return a list of RecordingInfo.
        ACTUAL -
        Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
        at com.sun.proxy.$Proxy0.getRecordings(Unknown Source)
        at com.example.Main.main(Main.java:32)
        Caused by: java.io.InvalidObjectException: Failed to invoke from(CompositeData)
        at java.management/com.sun.jmx.mbeanserver.DefaultMXBeanMappingFactory.invalidObjectException(DefaultMXBeanMappingFactory.java:1437)
        at java.management/com.sun.jmx.mbeanserver.DefaultMXBeanMappingFactory$CompositeBuilderViaFrom.fromCompositeData(DefaultMXBeanMappingFactory.java:1024)
        at java.management/com.sun.jmx.mbeanserver.DefaultMXBeanMappingFactory$CompositeMapping.fromNonNullOpenValue(DefaultMXBeanMappingFactory.java:922)
        at java.management/com.sun.jmx.mbeanserver.DefaultMXBeanMappingFactory$NonNullMXBeanMapping.fromOpenValue(DefaultMXBeanMappingFactory.java:134)
        at java.management/com.sun.jmx.mbeanserver.DefaultMXBeanMappingFactory$CollectionMapping.fromNonNullOpenValue(DefaultMXBeanMappingFactory.java:665)
        at java.management/com.sun.jmx.mbeanserver.DefaultMXBeanMappingFactory$NonNullMXBeanMapping.fromOpenValue(DefaultMXBeanMappingFactory.java:134)
        at java.management/com.sun.jmx.mbeanserver.ConvertingMethod.fromOpenReturnValue(ConvertingMethod.java:131)
        at java.management/com.sun.jmx.mbeanserver.MXBeanProxy.invoke(MXBeanProxy.java:168)
        at java.management/javax.management.MBeanServerInvocationHandler.invoke(MBeanServerInvocationHandler.java:258)
        ... 2 more
        Caused by: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at java.base/sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:260)
        at java.management/com.sun.jmx.mbeanserver.DefaultMXBeanMappingFactory$CompositeBuilderViaFrom.fromCompositeData(DefaultMXBeanMappingFactory.java:1021)
        ... 9 more
        Caused by: java.lang.ClassCastException: class java.lang.Long cannot be cast to class java.lang.Integer (java.lang.Long and java.lang.Integer are in module java.base of loader 'bootstrap')
        at jdk.management.jfr/jdk.management.jfr.RecordingInfo.<init>(RecordingInfo.java:91)
        at jdk.management.jfr/jdk.management.jfr.RecordingInfo.from(RecordingInfo.java:396)
        ... 20 more

        ---------- BEGIN SOURCE ----------
        import java.util.List;
        import javax.management.JMX;
        import javax.management.MBeanServerConnection;
        import javax.management.ObjectName;
        import javax.management.remote.JMXConnector;
        import javax.management.remote.JMXConnectorFactory;
        import javax.management.remote.JMXServiceURL;
        import com.sun.tools.attach.VirtualMachine;
        import jdk.management.jfr.FlightRecorderMXBean;
        import jdk.management.jfr.RecordingInfo;

        public class Main {

          public static void main(String[] args) throws Exception {
            // Use the PID of a running JVM
            String pid = args[0];
            VirtualMachine vm = VirtualMachine.attach(pid);

            String jmxServiceUrl = vm.startLocalManagementAgent();
            List<RecordingInfo> recordings = getRecordingInfos(jmxServiceUrl);
            System.out.println(recordings);
          }

          private static List<RecordingInfo> getRecordingInfos(String jmxServiceUrl) throws Exception {
            JMXConnector jmxConnector = JMXConnectorFactory.connect(new JMXServiceURL(jmxServiceUrl));
            MBeanServerConnection mBeanServerConnection = jmxConnector.getMBeanServerConnection();

            ObjectName objectName = new ObjectName("jdk.management.jfr:type=FlightRecorder");
            FlightRecorderMXBean flightRecorder = JMX.newMXBeanProxy(mBeanServerConnection, objectName, FlightRecorderMXBean.class);

            return flightRecorder.getRecordings();
          }

        }
        ---------- END SOURCE ----------

        FREQUENCY : always


          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  cito Chihiro Ito
                  Reporter:
                  webbuggrp Webbug Group
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  7 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved: