RFR: 8364227: MBeanServer registerMBean throws NPE [v2]

Kevin Walls kevinw at openjdk.org
Wed Jul 30 17:54:34 UTC 2025


On Wed, 30 Jul 2025 17:12:20 GMT, Kevin Walls <kevinw at openjdk.org> wrote:

>> A long-standing omission where MBeanServer.registerMBean documents exception behaviour, but we actually throw an NPE.  We should recognise a null object and return the wrapped exception we document.
>> 
>> This wrapping behaviour is common elsewhere in this area, e.g. unregisterMBean which checks for a null name and throws RuntimeOperationsException.
>> 
>> Docs:
>> https://docs.oracle.com/en/java/javase/24/docs/api/java.management/javax/management/MBeanServer.html#registerMBean(java.lang.Object,javax.management.ObjectName)
>> 
>> Throws:
>> RuntimeOperationsException - Wraps a java.lang.IllegalArgumentException: The object passed in parameter is null or no object name is specified.
>> 
>> 
>> Current behaviour:
>> 
>> 
>> $ java MBS_NPE.java
>> Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "object" is null
>>         at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:296)
>>         at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:511)
>>         at MBS_NPE.main(MBS_NPE.java:11)
>> 
>> 
>> Should be changed to:
>> 
>> 
>> $ java MBS_NPE.java
>> javax.management.RuntimeOperationsException: Exception occurred trying to register the MBean
>>         at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:297)
>>         at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:511)
>>         at MBS_NPE.main(MBS_NPE.java:11)
>>         at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
>>         at java.base/java.lang.reflect.Method.invoke(Method.java:565)
>>         at jdk.compiler/com.sun.tools.javac.launcher.SourceLauncher.execute(SourceLauncher.java:258)
>>         at jdk.compiler/com.sun.tools.javac.launcher.SourceLauncher.run(SourceLauncher.java:138)
>>         at jdk.compiler/com.sun.tools.javac.launcher.SourceLauncher.main(SourceLauncher.java:76)
>> Caused by: java.lang.IllegalArgumentException: Object cannot be null
>>         at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:295)
>>         ... 7 more
>
> Kevin Walls has updated the pull request incrementally with two additional commits since the last revision:
> 
>  - comment update
>  - Add test for exceptions thrown in response to null params.

I've populated the CSR.

Added a test, it does 16 tests that methods throw what we expect when given various null params.

e.g.

----------System.out:(60/2902)----------
1
Checking: javax.management.RuntimeOperationsException: Exception occurred during MBean creation
Got expected cause: java.lang.IllegalArgumentException: The class name cannot be null

2
Checking: javax.management.RuntimeOperationsException: Exception occurred during MBean creation
Got expected cause: java.lang.IllegalArgumentException: Invalid name->*:type=Foo

3
Checking: javax.management.RuntimeOperationsException: Exception occurred trying to register the MBean
Got expected cause: java.lang.IllegalArgumentException: Object cannot be null

...etc...

-------------

PR Comment: https://git.openjdk.org/jdk/pull/26548#issuecomment-3137313751


More information about the serviceability-dev mailing list