Fixes #3090 - MBeanContainer throws NPE for arrays.
Corrected places that were making unguarded calls to Class.getPackage(). Added tests for various array cases. Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
parent
b57c05999e
commit
294d1c65f5
|
@ -217,8 +217,11 @@ public class MBeanContainer implements Container.InheritedListener, Dumpable, De
|
|||
|
||||
private static Constructor<?> findConstructor(Class<?> klass)
|
||||
{
|
||||
String pName = klass.getPackage().getName();
|
||||
String cName = klass.getName().substring(pName.length() + 1);
|
||||
Package pkg = klass.getPackage();
|
||||
if (pkg == null)
|
||||
return null;
|
||||
String pName = pkg.getName();
|
||||
String cName = klass.getName().substring(pName.isEmpty() ? 0 : pName.length() + 1);
|
||||
String mName = pName + ".jmx." + cName + "MBean";
|
||||
try
|
||||
{
|
||||
|
@ -310,12 +313,19 @@ public class MBeanContainer implements Container.InheritedListener, Dumpable, De
|
|||
// No override of the mbean's ObjectName, so make a generic one.
|
||||
if (objectName == null)
|
||||
{
|
||||
Class<?> klass = obj.getClass();
|
||||
while (klass.isArray())
|
||||
klass = klass.getComponentType();
|
||||
|
||||
// If no explicit domain, create one.
|
||||
String domain = _domain;
|
||||
if (domain == null)
|
||||
domain = obj.getClass().getPackage().getName();
|
||||
{
|
||||
Package pkg = klass.getPackage();
|
||||
domain = pkg == null ? "" : pkg.getName();
|
||||
}
|
||||
|
||||
String type = obj.getClass().getName().toLowerCase(Locale.ENGLISH);
|
||||
String type = klass.getName().toLowerCase(Locale.ENGLISH);
|
||||
int dot = type.lastIndexOf('.');
|
||||
if (dot >= 0)
|
||||
type = type.substring(dot + 1);
|
||||
|
|
|
@ -24,6 +24,7 @@ import javax.management.Attribute;
|
|||
import javax.management.MBeanInfo;
|
||||
import javax.management.MBeanOperationInfo;
|
||||
import javax.management.MBeanParameterInfo;
|
||||
import javax.management.ObjectName;
|
||||
|
||||
import com.acme.Derived;
|
||||
import com.acme.Managed;
|
||||
|
@ -34,6 +35,7 @@ import org.junit.jupiter.api.Test;
|
|||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
@ -54,6 +56,46 @@ public class ObjectMBeanTest
|
|||
container = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMBeanForNull()
|
||||
{
|
||||
Object mBean = container.mbeanFor(null);
|
||||
assertNull(mBean);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMBeanForString()
|
||||
{
|
||||
String obj = "foo";
|
||||
Object mbean = container.mbeanFor(obj);
|
||||
assertNotNull(mbean);
|
||||
container.beanAdded(null, obj);
|
||||
ObjectName objectName = container.findMBean(obj);
|
||||
assertNotNull(objectName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMBeanForStringArray()
|
||||
{
|
||||
String[] obj = {"a", "b"};
|
||||
Object mbean = container.mbeanFor(obj);
|
||||
assertNotNull(mbean);
|
||||
container.beanAdded(null, obj);
|
||||
ObjectName objectName = container.findMBean(obj);
|
||||
assertNotNull(objectName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMBeanForIntArray()
|
||||
{
|
||||
int[] obj = {0, 1, 2};
|
||||
Object mbean = container.mbeanFor(obj);
|
||||
assertNotNull(mbean);
|
||||
container.beanAdded(null, obj);
|
||||
ObjectName objectName = container.findMBean(obj);
|
||||
assertNotNull(objectName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMetaDataCaching()
|
||||
{
|
||||
|
|
|
@ -70,13 +70,6 @@ public class ObjectMBeanUtilTest
|
|||
assertEquals("Test the mbean extended stuff", objectMBeanInfo.getDescription(), "Mbean description must be equal to : Test the mbean extended stuff");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMbeanForNullCheck()
|
||||
{
|
||||
Object mBean = container.mbeanFor(null);
|
||||
assertNull(mBean, "As we are passing null value the output should be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAttributeMBeanException() throws Exception
|
||||
{
|
||||
|
@ -151,7 +144,7 @@ public class ObjectMBeanUtilTest
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testSetAttributesForCollectionTypeAttribue() throws Exception
|
||||
public void testSetAttributesForCollectionTypeAttribute() throws Exception
|
||||
{
|
||||
ArrayList<Derived> aliasNames = new ArrayList<>(Arrays.asList(getArrayTypeAttribute()));
|
||||
|
||||
|
@ -231,7 +224,7 @@ public class ObjectMBeanUtilTest
|
|||
ReflectionException e = assertThrows(ReflectionException.class, () ->
|
||||
objectMBean.invoke("good", new Object[0], new String[]{"int aone"}));
|
||||
|
||||
assertNotNull(e, "An ReflectionException must have occurred by now as we cannot call a methow with wrong signature");
|
||||
assertNotNull(e, "A ReflectionException must have occurred by now as we cannot call a method with wrong signature");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue