-
Bug
-
Resolution: Fixed
-
P4
-
6
-
None
-
b17
-
generic
-
generic
If a Standard MBean throws a checked exception from a getter or setter, that exception is wrapped in an MBeanException by the MBean Server. A proxy created with MBeanServerInvocationHandler.newProxyInstance should unwrap the exception from its MBeanException before throwing it. Otherwise, the caller of the proxy will see an UndeclaredThrowableException unless it has declared MBeanException in the throws clause of the getter.
The following test code illustrates the problem:
import java.io.IOException;
import javax.management.*;
public class ExceptionTest {
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
ObjectName name = new ObjectName("a:b=c");
mbs.registerMBean(new Test(), name);
TestMBean proxy = (TestMBean)
MBeanServerInvocationHandler.newProxyInstance(mbs,
name,
TestMBean.class,
false);
try {
proxy.getIOException();
} catch (IOException e) {
System.out.println("Test passed: got expected exception:");
e.printStackTrace(System.out);
} catch (Throwable t) {
System.out.println("Test failed: got wrong exception:");
t.printStackTrace(System.out);
}
}
public static interface TestMBean {
public String getIOException() throws IOException;
}
public static class Test implements TestMBean {
public String getIOException() throws IOException {
throw new IOException("oops");
}
}
}
The output is:
Test failed: got wrong exception:
java.lang.reflect.UndeclaredThrowableException
at $Proxy0.getIOException(Unknown Source)
at ExceptionTest.main(ExceptionTest.java:15)
Caused by: javax.management.MBeanException: Exception thrown in the getter for the attribute IOException
at com.sun.jmx.mbeanserver.StandardMetaDataImpl.getAttribute(StandardMetaDataImpl.java:653)
at com.sun.jmx.mbeanserver.StandardMetaDataImpl.getAttribute(StandardMetaDataImpl.java:265)
at com.sun.jmx.mbeanserver.MetaDataImpl.getAttribute(MetaDataImpl.java:181)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:638)
at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:659)
at javax.management.MBeanServerInvocationHandler.invoke(MBeanServerInvocationHandler.java:175)
... 2 more
Caused by: java.io.IOException: oops
at ExceptionTest$Test.getIOException(ExceptionTest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.jmx.mbeanserver.StandardMetaDataImpl.getAttribute(StandardMetaDataImpl.java:637)
... 7 more
The following test code illustrates the problem:
import java.io.IOException;
import javax.management.*;
public class ExceptionTest {
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
ObjectName name = new ObjectName("a:b=c");
mbs.registerMBean(new Test(), name);
TestMBean proxy = (TestMBean)
MBeanServerInvocationHandler.newProxyInstance(mbs,
name,
TestMBean.class,
false);
try {
proxy.getIOException();
} catch (IOException e) {
System.out.println("Test passed: got expected exception:");
e.printStackTrace(System.out);
} catch (Throwable t) {
System.out.println("Test failed: got wrong exception:");
t.printStackTrace(System.out);
}
}
public static interface TestMBean {
public String getIOException() throws IOException;
}
public static class Test implements TestMBean {
public String getIOException() throws IOException {
throw new IOException("oops");
}
}
}
The output is:
Test failed: got wrong exception:
java.lang.reflect.UndeclaredThrowableException
at $Proxy0.getIOException(Unknown Source)
at ExceptionTest.main(ExceptionTest.java:15)
Caused by: javax.management.MBeanException: Exception thrown in the getter for the attribute IOException
at com.sun.jmx.mbeanserver.StandardMetaDataImpl.getAttribute(StandardMetaDataImpl.java:653)
at com.sun.jmx.mbeanserver.StandardMetaDataImpl.getAttribute(StandardMetaDataImpl.java:265)
at com.sun.jmx.mbeanserver.MetaDataImpl.getAttribute(MetaDataImpl.java:181)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:638)
at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:659)
at javax.management.MBeanServerInvocationHandler.invoke(MBeanServerInvocationHandler.java:175)
... 2 more
Caused by: java.io.IOException: oops
at ExceptionTest$Test.getIOException(ExceptionTest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.jmx.mbeanserver.StandardMetaDataImpl.getAttribute(StandardMetaDataImpl.java:637)
... 7 more