This fixes a bug in setAttribute, whereby it can only
currently handle mutators with a parameter of a type that
exactly matches the type of the new value.  This adds supports
for primitive types and subtyping.

Changelog:

2007-03-28  Andrew John Hughes  <[EMAIL PROTECTED]>

        * javax/management/StandardMBean.java:
        (setAttribute(Attribute)): Add handling of primitive
        types and subtype parameters.
        (getMutator(String,Class<?>)): New helper method to
        return the mutator.

-- 
Andrew :-)

Escape the Java Trap with GNU Classpath!
http://www.gnu.org/philosophy/java-trap.html
public class gcj extends Freedom implements Java { ... }
Index: javax/management/StandardMBean.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/management/StandardMBean.java,v
retrieving revision 1.8
diff -u -3 -p -u -r1.8 StandardMBean.java
--- javax/management/StandardMBean.java	5 Mar 2007 00:22:41 -0000	1.8
+++ javax/management/StandardMBean.java	28 Mar 2007 21:03:49 -0000
@@ -854,24 +854,12 @@ public class StandardMBean
     throws AttributeNotFoundException, InvalidAttributeValueException,
 	   MBeanException, ReflectionException
   {
-    Method setter;
     String name = attribute.getName();
-    Object val = attribute.getValue();
-    try 
-      {
-	setter = iface.getMethod("set" +
-				 name.substring(0, 1).toUpperCase() +
-				 name.substring(1), val.getClass());
-      }
-    catch (NoSuchMethodException e)
-      {
-	throw ((AttributeNotFoundException) 
-	       new AttributeNotFoundException("The attribute, " + name +
-					      ", was not found.").initCause(e));
-      }
+    String attName = name.substring(0, 1).toUpperCase() + name.substring(1);
+    Object val = attribute.getValue();   
     try
       {
-	setter.invoke(impl, new Object[] { val });
+	getMutator(attName, val.getClass()).invoke(impl, new Object[] { val });
       }
     catch (IllegalAccessException e)
       {
@@ -956,4 +944,142 @@ public class StandardMBean
     this.impl = impl;
   }
 
+  /**
+   * Returns the mutator method for a particular attribute name
+   * with a parameter type matching that of the given value.
+   *
+   * @param name the name of the attribute.
+   * @param type the type of the parameter.
+   * @return the appropriate mutator method.
+   * @throws AttributeNotFoundException if a method can't be found.
+   */
+  private Method getMutator(String name, Class<?> type)
+    throws AttributeNotFoundException
+  {
+    String mutator = "set" + name;
+    Exception ex = null;
+    try 
+      {
+	return iface.getMethod(mutator, type);
+      }
+    catch (NoSuchMethodException e)
+      {
+	/* Ignored; we'll try harder instead */
+	ex = e;
+      }
+    /* Special cases */
+    if (type == Boolean.class)
+      try
+	{
+	  return iface.getMethod(mutator, Boolean.TYPE);
+	}
+      catch (NoSuchMethodException e)
+	{
+	  throw ((AttributeNotFoundException) 
+		 new AttributeNotFoundException("The attribute, " + name +
+						", was not found.").initCause(e));
+	}
+    if (type == Byte.class)
+      try
+	{
+	  return iface.getMethod(mutator, Byte.TYPE);
+	}
+      catch (NoSuchMethodException e)
+	{
+	  throw ((AttributeNotFoundException) 
+		 new AttributeNotFoundException("The attribute, " + name +
+						", was not found.").initCause(e));
+	}
+    if (type == Character.class)
+      try
+	{
+	  return iface.getMethod(mutator, Character.TYPE);
+	}
+      catch (NoSuchMethodException e)
+	{
+	  throw ((AttributeNotFoundException) 
+		 new AttributeNotFoundException("The attribute, " + name +
+						", was not found.").initCause(e));
+	}
+    if (type == Double.class)
+      try
+	{
+	  return iface.getMethod(mutator, Double.TYPE);
+	}
+      catch (NoSuchMethodException e)
+	{
+	  throw ((AttributeNotFoundException) 
+		 new AttributeNotFoundException("The attribute, " + name +
+						", was not found.").initCause(e));
+	}
+    if (type == Float.class)
+      try
+	{
+	  return iface.getMethod(mutator, Float.TYPE);
+	}
+      catch (NoSuchMethodException e)
+	{
+	  throw ((AttributeNotFoundException) 
+		 new AttributeNotFoundException("The attribute, " + name +
+						", was not found.").initCause(e));
+	}
+    if (type == Integer.class)
+      try
+	{
+	  return iface.getMethod(mutator, Integer.TYPE);
+	}
+      catch (NoSuchMethodException e)
+	{
+	  throw ((AttributeNotFoundException) 
+		 new AttributeNotFoundException("The attribute, " + name +
+						", was not found.").initCause(e));
+	}
+    if (type == Long.class)
+      try
+	{
+	  return iface.getMethod(mutator, Long.TYPE);
+	}
+      catch (NoSuchMethodException e)
+	{
+	  throw ((AttributeNotFoundException) 
+		 new AttributeNotFoundException("The attribute, " + name +
+						", was not found.").initCause(e));
+	}
+    if (type == Short.class)
+      try
+	{
+	  return iface.getMethod(mutator, Short.TYPE);
+	}
+      catch (NoSuchMethodException e)
+	{
+	  throw ((AttributeNotFoundException) 
+		 new AttributeNotFoundException("The attribute, " + name +
+						", was not found.").initCause(e));
+	}
+    /* Superclasses and interfaces */
+    for (Class<?> i : type.getInterfaces())
+      try
+	{
+	  return getMutator(name, i);
+	}
+      catch (AttributeNotFoundException e)
+	{
+	  ex = e;
+	}
+    Class<?> sclass = type.getSuperclass();
+    if (sclass != null && sclass != Object.class)
+      try
+	{
+	  return getMutator(name, sclass);
+	}
+      catch (AttributeNotFoundException e)
+	{
+	  ex = e;
+	}
+    /* If we get this far, give up */
+    throw ((AttributeNotFoundException) 
+	   new AttributeNotFoundException("The attribute, " + name +
+					  ", was not found.").initCause(ex)); 
+  }
+
 }

Attachment: signature.asc
Description: Digital signature

Reply via email to