/** * Return the class to be used for the JMX descriptor field "class". * Only applied when the "exposeClassDescriptor" property is "true". * <p>The default implementation returns the first implemented interface * for a JDK proxy, and the target class else. * @param managedBean the bean instance (might be an AOP proxy) * @return the class to expose in the descriptor field "class" * @see #setExposeClassDescriptor * @see #getClassToExpose(Class) * @see org.springframework.aop.framework.AopProxyUtils#proxiedUserInterfaces(Object) */ protected Class<?> getClassForDescriptor(Object managedBean) { if (AopUtils.isJdkDynamicProxy(managedBean)) { return AopProxyUtils.proxiedUserInterfaces(managedBean)[0]; } return getClassToExpose(managedBean); }
@Test(expected = IllegalArgumentException.class) public void testProxiedUserInterfacesWithNoInterface() { Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[0], (proxy1, method, args) -> null); AopProxyUtils.proxiedUserInterfaces(proxy); }
/** * Return the class to be used for the JMX descriptor field "class". * Only applied when the "exposeClassDescriptor" property is "true". * <p>The default implementation returns the first implemented interface * for a JDK proxy, and the target class else. * @param managedBean the bean instance (might be an AOP proxy) * @return the class to expose in the descriptor field "class" * @see #setExposeClassDescriptor * @see #getClassToExpose(Class) * @see org.springframework.aop.framework.AopProxyUtils#proxiedUserInterfaces(Object) */ protected Class<?> getClassForDescriptor(Object managedBean) { if (AopUtils.isJdkDynamicProxy(managedBean)) { return AopProxyUtils.proxiedUserInterfaces(managedBean)[0]; } return getClassToExpose(managedBean); }
@Test public void testProxiedUserInterfacesWithSingleInterface() { ProxyFactory pf = new ProxyFactory(); pf.setTarget(new TestBean()); pf.addInterface(ITestBean.class); Object proxy = pf.getProxy(); Class<?>[] userInterfaces = AopProxyUtils.proxiedUserInterfaces(proxy); assertEquals(1, userInterfaces.length); assertEquals(ITestBean.class, userInterfaces[0]); }
@Test public void testProxiedUserInterfacesWithMultipleInterfaces() { ProxyFactory pf = new ProxyFactory(); pf.setTarget(new TestBean()); pf.addInterface(ITestBean.class); pf.addInterface(Comparable.class); Object proxy = pf.getProxy(); Class<?>[] userInterfaces = AopProxyUtils.proxiedUserInterfaces(proxy); assertEquals(2, userInterfaces.length); assertEquals(ITestBean.class, userInterfaces[0]); assertEquals(Comparable.class, userInterfaces[1]); }
/** * Return the class to be used for the JMX descriptor field "class". * Only applied when the "exposeClassDescriptor" property is "true". * <p>The default implementation returns the first implemented interface * for a JDK proxy, and the target class else. * @param managedBean the bean instance (might be an AOP proxy) * @return the class to expose in the descriptor field "class" * @see #setExposeClassDescriptor * @see #getClassToExpose(Class) * @see org.springframework.aop.framework.AopProxyUtils#proxiedUserInterfaces(Object) */ protected Class<?> getClassForDescriptor(Object managedBean) { if (AopUtils.isJdkDynamicProxy(managedBean)) { return AopProxyUtils.proxiedUserInterfaces(managedBean)[0]; } return getClassToExpose(managedBean); }