JDK-8219904 : ClassCastException when calling FlightRecorderMXBean#getRecordings()
  • Type: Bug
  • Component: hotspot
  • Sub-Component: jfr
  • Affected Version: 11,11.0.2,12,13,14
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows
  • CPU: x86_64
  • Submitted: 2019-02-27
  • Updated: 2020-06-15
  • Resolved: 2020-02-25
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11 JDK 13 JDK 15
11.0.8Fixed 13.0.4Fixed 15 b12Fixed
Related Reports
CSR :  
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



Comments
This is caused by an issue on the connecting side (ex. Main's environment above). You should run JMX client application on JDK applied this patch. I tried to test using two environments as following. - unpatched environment - patched environment And I ran two applications (include Main above) on 4 patterns of environments. In these two patterns, the Main stop casing by ClassCastException. I ran an application running on "unpatched" env and connected to it using the Main running on "unpatched" JDK. I ran an application running on "patched" env and connected to it using the Main running on "unpatched" JDK. In these two patterns, the Main is over correctly. I ran an application running on "unpatched" env and connected to it using the Main running on "patched" JDK. I ran an application running on "patched" env and connected to it using the Main running on "patched" JDK. It seems to correct behavior.
09-06-2020

Fix request (13u): The original change applies cleanly, tested with jdk/jfr tests. The reproducer fails without the fix and lists recordings with the fix.
08-06-2020

Fix Request (11u): The patch applied cleanly and the reproducer no longer had an exception after the patch. Tier one tests and jfr tests were ran without issue on Linux. Reproducer output after patch: $ java11-latest Main 14195 [name="JMC_Default" id=1 maxAge=0 maxSize=104857600] For reproducer output before patch, see previous comment.
06-04-2020

Using the reproducer provided by Fairoz, I was able to see this issue on Linux against a Java application running: $ java11-latest -version openjdk version "11.0.8-internal" 2020-07-14 OpenJDK Runtime Environment (build 11.0.8-internal+0-adhoc.jkang.jdk11u-dev) OpenJDK 64-Bit Server VM (build 11.0.8-internal+0-adhoc.jkang.jdk11u-dev, mixed mode) $ java11-latest Main 8029 Exception in thread "main" java.lang.reflect.UndeclaredThrowableException at com.sun.proxy.$Proxy0.getRecordings(Unknown Source) at Main.getRecordingInfos(Main.java:31) at Main.main(Main.java:20) 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) ... 3 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) ... 10 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:90) at jdk.management.jfr/jdk.management.jfr.RecordingInfo.from(RecordingInfo.java:395) ... 21 more
03-04-2020

URL: https://hg.openjdk.java.net/jdk/jdk/rev/f965bceba123 User: egahlin Date: 2020-02-25 02:31:34 +0000
25-02-2020

Additional information from submitter == Please note that the exception, that is mentioned in the comment is not the same as I observed: C:\Users\fmatte\JI\9059565>"c:\Users\fmatte\JAVA\jdk13\openjdk-13-ea+8_windows-x64_bin\jdk-13\bin\java.exe" Main 6100 Exception in thread "main" java.lang.reflect.UndeclaredThrowableException at com.sun.proxy.$Proxy1.getRecordings(Unknown Source) at Main.getRecordingInfos(Main.java:31) at Main.main(Main.java:20) Caused by: javax.management.InstanceNotFoundException: jdk.management.jfr:type=FlightRecorder It seems that the FlightRecorder MXBean is not available in the JVM to which you connect.
04-03-2019

This issue is reproducible on 11.0.2, 12 and 13 ea b08 also This issue observed only on windows. == C:\Users\fmatte\JI\9059565>"c:\Users\fmatte\JAVA\jdk13\openjdk-13-ea+8_windows-x64_bin\jdk-13\bin\java.exe" Main 6100 Exception in thread "main" java.lang.reflect.UndeclaredThrowableException at com.sun.proxy.$Proxy1.getRecordings(Unknown Source) at Main.getRecordingInfos(Main.java:31) at Main.main(Main.java:20) Caused by: javax.management.InstanceNotFoundException: jdk.management.jfr:type=FlightRecorder at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1095) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:643) at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:678) at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1445) at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76) at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309) at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401) at javax.management.remote.rmi.RMIConnectionImpl.getAttribute(RMIConnectionImpl.java:639) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357) at sun.rmi.transport.Transport$1.run(Transport.java:200) at sun.rmi.transport.Transport$1.run(Transport.java:197) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(Transport.java:196) at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) at java.rmi/sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:283) at java.rmi/sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:260) at java.rmi/sun.rmi.server.UnicastRef.invoke(UnicastRef.java:161) at jdk.remoteref/jdk.jmx.remote.internal.rmi.PRef.invoke(Unknown Source) at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl_Stub.getAttribute(Unknown Source) at java.management.rmi/javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection.getAttribute(RMIConnector.java:904) at java.management/com.sun.jmx.mbeanserver.MXBeanProxy$GetHandler.invoke(MXBeanProxy.java:122) at java.management/com.sun.jmx.mbeanserver.MXBeanProxy.invoke(MXBeanProxy.java:167) at java.management/javax.management.MBeanServerInvocationHandler.invoke(MBeanServerInvocationHandler.java:258) ... 3 more
28-02-2019