6734813: Provide a way to construct an ObjectName without checked exceptions
authoremcmanus
Wed, 10 Sep 2008 13:36:47 +0200
changeset 1225 6ef6227d36eb
parent 1224 8c1bc7c5dd00
child 1226 eba24d3b9c0c
6734813: Provide a way to construct an ObjectName without checked exceptions 6746649: ObjectName constructors and methods declare unchecked exceptions in throws clauses Reviewed-by: dfuchs
jdk/src/share/classes/com/sun/jmx/interceptor/DefaultMBeanServerInterceptor.java
jdk/src/share/classes/com/sun/jmx/mbeanserver/Repository.java
jdk/src/share/classes/com/sun/jmx/mbeanserver/Util.java
jdk/src/share/classes/com/sun/jmx/namespace/DomainInterceptor.java
jdk/src/share/classes/java/lang/management/PlatformComponent.java
jdk/src/share/classes/java/util/logging/Logging.java
jdk/src/share/classes/javax/management/MBeanServerDelegate.java
jdk/src/share/classes/javax/management/ObjectName.java
jdk/src/share/classes/javax/management/QueryNotificationFilter.java
jdk/src/share/classes/javax/management/event/EventClientDelegateMBean.java
jdk/src/share/classes/javax/management/namespace/MBeanServerSupport.java
jdk/src/share/classes/sun/management/Util.java
jdk/test/javax/management/ObjectName/ValueOfTest.java
--- a/jdk/src/share/classes/com/sun/jmx/interceptor/DefaultMBeanServerInterceptor.java	Tue Sep 09 15:20:07 2008 -0700
+++ b/jdk/src/share/classes/com/sun/jmx/interceptor/DefaultMBeanServerInterceptor.java	Wed Sep 10 13:36:47 2008 +0200
@@ -613,8 +613,7 @@
             List<String> result = new ArrayList<String>(domains.length);
             for (int i = 0; i < domains.length; i++) {
                 try {
-                    ObjectName dom =
-                            Util.newObjectName(domains[i] + ":x=x");
+                    ObjectName dom = ObjectName.valueOf(domains[i] + ":x=x");
                     checkMBeanPermission(mbeanServerName, (String) null, null, dom, "getDomains");
                     result.add(domains[i]);
                 } catch (SecurityException e) {
@@ -1170,7 +1169,7 @@
            if one is supplied where it shouldn't be).  */
         final String completeName = domain + name;
 
-        return Util.newObjectName(completeName);
+        return ObjectName.valueOf(completeName);
     }
 
     public String getDefaultDomain()  {
--- a/jdk/src/share/classes/com/sun/jmx/mbeanserver/Repository.java	Tue Sep 09 15:20:07 2008 -0700
+++ b/jdk/src/share/classes/com/sun/jmx/mbeanserver/Repository.java	Wed Sep 10 13:36:47 2008 +0200
@@ -396,7 +396,7 @@
 
         // Set domain to default if domain is empty and not already set
         if (dom.length() == 0)
-            name = Util.newObjectName(domain + name.toString());
+            name = ObjectName.valueOf(domain + name.toString());
 
         // Do we have default domain ?
         if (dom == domain) {  // ES: OK (dom & domain are interned)
--- a/jdk/src/share/classes/com/sun/jmx/mbeanserver/Util.java	Tue Sep 09 15:20:07 2008 -0700
+++ b/jdk/src/share/classes/com/sun/jmx/mbeanserver/Util.java	Wed Sep 10 13:36:47 2008 +0200
@@ -110,14 +110,6 @@
         return new ArrayList<E>(c);
     }
 
-    public static ObjectName newObjectName(String s) {
-        try {
-            return new ObjectName(s);
-        } catch (MalformedObjectNameException e) {
-            throw new IllegalArgumentException(e);
-        }
-    }
-
     /* This method can be used by code that is deliberately violating the
      * allowed checked casts.  Rather than marking the whole method containing
      * the code with @SuppressWarnings, you can use a call to this method for
--- a/jdk/src/share/classes/com/sun/jmx/namespace/DomainInterceptor.java	Tue Sep 09 15:20:07 2008 -0700
+++ b/jdk/src/share/classes/com/sun/jmx/namespace/DomainInterceptor.java	Wed Sep 10 13:36:47 2008 +0200
@@ -110,7 +110,7 @@
         super(handler);
         this.domainName = domainName;
         this.serverName = serverName;
-        ALL = Util.newObjectName(domainName+":*");
+        ALL = ObjectName.valueOf(domainName+":*");
     }
 
     @Override
@@ -437,7 +437,7 @@
          int count=0;
          for (int i=0;i<domains.length;i++) {
              try {
-                 check(Util.newObjectName(domains[i]+":x=x"),"-",
+                 check(ObjectName.valueOf(domains[i]+":x=x"),"-",
                          "-","getDomains");
              } catch (SecurityException x) { // DLS: OK
                  count++;
--- a/jdk/src/share/classes/java/lang/management/PlatformComponent.java	Tue Sep 09 15:20:07 2008 -0700
+++ b/jdk/src/share/classes/java/lang/management/PlatformComponent.java	Wed Sep 10 13:36:47 2008 +0200
@@ -388,7 +388,7 @@
             // if there are more than 1 key properties (i.e. other than "type")
             domainAndType += ",*";
         }
-        ObjectName on = com.sun.jmx.mbeanserver.Util.newObjectName(domainAndType);
+        ObjectName on = ObjectName.valueOf(domainAndType);
         Set<ObjectName> set =  mbs.queryNames(on, null);
         for (PlatformComponent pc : subComponents) {
             set.addAll(pc.getObjectNames(mbs));
--- a/jdk/src/share/classes/java/util/logging/Logging.java	Tue Sep 09 15:20:07 2008 -0700
+++ b/jdk/src/share/classes/java/util/logging/Logging.java	Wed Sep 10 13:36:47 2008 +0200
@@ -118,6 +118,6 @@
     }
 
     public ObjectName getObjectName() {
-        return com.sun.jmx.mbeanserver.Util.newObjectName(LogManager.LOGGING_MXBEAN_NAME);
+        return ObjectName.valueOf(LogManager.LOGGING_MXBEAN_NAME);
     }
 }
--- a/jdk/src/share/classes/javax/management/MBeanServerDelegate.java	Tue Sep 09 15:20:07 2008 -0700
+++ b/jdk/src/share/classes/javax/management/MBeanServerDelegate.java	Wed Sep 10 13:36:47 2008 +0200
@@ -304,7 +304,7 @@
      * @since 1.6
      */
     public static final ObjectName DELEGATE_NAME =
-            Util.newObjectName("JMImplementation:type=MBeanServerDelegate");
+            ObjectName.valueOf("JMImplementation:type=MBeanServerDelegate");
 
     /* Return a timestamp that is monotonically increasing even if
        System.currentTimeMillis() isn't (for example, if you call this
--- a/jdk/src/share/classes/javax/management/ObjectName.java	Tue Sep 09 15:20:07 2008 -0700
+++ b/jdk/src/share/classes/javax/management/ObjectName.java	Wed Sep 10 13:36:47 2008 +0200
@@ -413,7 +413,7 @@
     }
 
     private void copyToOtherDomain(String domain, ObjectName aname)
-        throws MalformedObjectNameException, NullPointerException {
+        throws MalformedObjectNameException {
 
         // The domain cannot be null
         if (domain == null)
@@ -467,7 +467,7 @@
      * is null.
      */
     private void construct(String name)
-        throws MalformedObjectNameException, NullPointerException {
+        throws MalformedObjectNameException {
 
         // The name cannot be null
         if (name == null)
@@ -729,7 +729,7 @@
      * @exception NullPointerException One of the parameters is null.
      */
     private void construct(String domain, Map<String,String> props)
-        throws MalformedObjectNameException, NullPointerException {
+        throws MalformedObjectNameException {
 
         // The domain cannot be null
         if (domain == null)
@@ -1071,7 +1071,7 @@
      * Check if the supplied key is a valid key.
      */
     private static void checkKey(String key)
-        throws MalformedObjectNameException, NullPointerException {
+        throws MalformedObjectNameException {
 
         if (key == null) throw new
             NullPointerException("Invalid key (null)");
@@ -1359,9 +1359,10 @@
      * @exception NullPointerException The <code>name</code> parameter
      * is null.
      *
+     * @see #valueOf(String)
      */
     public static ObjectName getInstance(String name)
-            throws MalformedObjectNameException, NullPointerException {
+            throws MalformedObjectNameException {
         return new ObjectName(name);
     }
 
@@ -1386,10 +1387,11 @@
      * follow the rules for quoting.
      * @exception NullPointerException One of the parameters is null.
      *
+     * @see #valueOf(String, String, String)
      */
     public static ObjectName getInstance(String domain, String key,
                                          String value)
-            throws MalformedObjectNameException, NullPointerException {
+            throws MalformedObjectNameException {
         return new ObjectName(domain, key, value);
     }
 
@@ -1417,10 +1419,11 @@
      * quoting.
      * @exception NullPointerException One of the parameters is null.
      *
+     * @see #valueOf(String, Hashtable)
      */
     public static ObjectName getInstance(String domain,
                                          Hashtable<String,String> table)
-        throws MalformedObjectNameException, NullPointerException {
+        throws MalformedObjectNameException {
         return new ObjectName(domain, table);
     }
 
@@ -1453,11 +1456,120 @@
      * @exception NullPointerException The <code>name</code> is null.
      *
      */
-    public static ObjectName getInstance(ObjectName name)
-            throws NullPointerException {
+    public static ObjectName getInstance(ObjectName name) {
         if (name.getClass().equals(ObjectName.class))
             return name;
-        return Util.newObjectName(name.getSerializedNameString());
+        return valueOf(name.getSerializedNameString());
+    }
+
+    /**
+     * <p>Return an instance of ObjectName that can be used anywhere
+     * an object obtained with {@link #ObjectName(String) new
+     * ObjectName(name)} can be used.  The returned object may be of
+     * a subclass of ObjectName.  Calling this method twice with the
+     * same parameters may return the same object or two equal but
+     * not identical objects.</p>
+     *
+     * <p>This method is equivalent to {@link #getInstance(String)} except that
+     * it does not throw any checked exceptions.</p>
+     *
+     * @param name  A string representation of the object name.
+     *
+     * @return an ObjectName corresponding to the given String.
+     *
+     * @exception IllegalArgumentException The string passed as a
+     * parameter does not have the right format.  The {@linkplain
+     * Throwable#getCause() cause} of this exception will be a
+     * {@link MalformedObjectNameException}.
+     * @exception NullPointerException The <code>name</code> parameter
+     * is null.
+     *
+     * @since 1.7
+     */
+    public static ObjectName valueOf(String name) {
+        try {
+            return getInstance(name);
+        } catch (MalformedObjectNameException e) {
+            throw new IllegalArgumentException(e.getMessage(), e);
+            // Just plain IllegalArgumentException(e) produces an exception
+            // message "javax.management.MalformedObjectNameException: ..."
+            // which is distracting.
+        }
+    }
+
+    /**
+     * <p>Return an instance of ObjectName that can be used anywhere
+     * an object obtained with {@link #ObjectName(String, String,
+     * String) new ObjectName(domain, key, value)} can be used.  The
+     * returned object may be of a subclass of ObjectName.  Calling
+     * this method twice with the same parameters may return the same
+     * object or two equal but not identical objects.</p>
+     *
+     * <p>This method is equivalent to {@link #getInstance(String, String,
+     * String)} except that it does not throw any checked exceptions.</p>
+     *
+     * @param domain  The domain part of the object name.
+     * @param key  The attribute in the key property of the object name.
+     * @param value The value in the key property of the object name.
+     *
+     * @return an ObjectName corresponding to the given domain,
+     * key, and value.
+     *
+     * @exception IllegalArgumentException The
+     * <code>domain</code>, <code>key</code>, or <code>value</code>
+     * contains an illegal character, or <code>value</code> does not
+     * follow the rules for quoting.  The {@linkplain
+     * Throwable#getCause() cause} of this exception will be a
+     * {@link MalformedObjectNameException}.
+     * @exception NullPointerException One of the parameters is null.
+     *
+     * @since 1.7
+     */
+    public static ObjectName valueOf(String domain, String key, String value) {
+        try {
+            return getInstance(domain, key, value);
+        } catch (MalformedObjectNameException e) {
+            throw new IllegalArgumentException(e.getMessage(), e);
+        }
+    }
+
+    /**
+     * <p>Return an instance of ObjectName that can be used anywhere
+     * an object obtained with {@link #ObjectName(String, Hashtable)
+     * new ObjectName(domain, table)} can be used.  The returned
+     * object may be of a subclass of ObjectName.  Calling this method
+     * twice with the same parameters may return the same object or
+     * two equal but not identical objects.</p>
+     *
+     * <p>This method is equivalent to {@link #getInstance(String, Hashtable)}
+     * except that it does not throw any checked exceptions.</p>
+     *
+     * @param domain  The domain part of the object name.
+     * @param table A hash table containing one or more key
+     * properties.  The key of each entry in the table is the key of a
+     * key property in the object name.  The associated value in the
+     * table is the associated value in the object name.
+     *
+     * @return an ObjectName corresponding to the given domain and
+     * key mappings.
+     *
+     * @exception IllegalArgumentException The <code>domain</code>
+     * contains an illegal character, or one of the keys or values in
+     * <code>table</code> contains an illegal character, or one of the
+     * values in <code>table</code> does not follow the rules for
+     * quoting.  The {@linkplain Throwable#getCause() cause} of this exception
+     * will be a {@link MalformedObjectNameException}.
+     * @exception NullPointerException One of the parameters is null.
+     *
+     * @since 1.7
+     */
+    public static ObjectName valueOf(String domain,
+                                     Hashtable<String,String> table) {
+        try {
+            return new ObjectName(domain, table);
+        } catch (MalformedObjectNameException e) {
+            throw new IllegalArgumentException(e.getMessage(), e);
+        }
     }
 
     /**
@@ -1477,7 +1589,7 @@
      * @since 1.7
      **/
     public final ObjectName withDomain(String newDomain)
-            throws NullPointerException, MalformedObjectNameException {
+            throws MalformedObjectNameException {
         return new ObjectName(newDomain, this);
     }
 
@@ -1490,9 +1602,11 @@
      * parameter does not have the right format.
      * @exception NullPointerException The <code>name</code> parameter
      * is null.
+     *
+     * @see #valueOf(String)
      */
     public ObjectName(String name)
-        throws MalformedObjectNameException, NullPointerException {
+        throws MalformedObjectNameException {
         construct(name);
     }
 
@@ -1508,9 +1622,11 @@
      * contains an illegal character, or <code>value</code> does not
      * follow the rules for quoting.
      * @exception NullPointerException One of the parameters is null.
+     *
+     * @see #valueOf(String, String, String)
      */
     public ObjectName(String domain, String key, String value)
-        throws MalformedObjectNameException, NullPointerException {
+        throws MalformedObjectNameException {
         // If key or value are null a NullPointerException
         // will be thrown by the put method in Hashtable.
         //
@@ -1533,9 +1649,11 @@
      * values in <code>table</code> does not follow the rules for
      * quoting.
      * @exception NullPointerException One of the parameters is null.
+     *
+     * @see #valueOf(String, Hashtable)
      */
     public ObjectName(String domain, Hashtable<String,String> table)
-            throws MalformedObjectNameException, NullPointerException {
+            throws MalformedObjectNameException {
         construct(domain, table);
         /* The exception for when a key or value in the table is not a
            String is now ClassCastException rather than
@@ -1629,8 +1747,7 @@
      *
      * @since 1.6
      */
-    public boolean isPropertyValuePattern(String property)
-        throws NullPointerException, IllegalArgumentException {
+    public boolean isPropertyValuePattern(String property) {
         if (property == null)
             throw new NullPointerException("key property can't be null");
         for (int i = 0; i < _ca_array.length; i++) {
@@ -1691,7 +1808,7 @@
      *
      * @exception NullPointerException If <code>property</code> is null.
      */
-    public String getKeyProperty(String property) throws NullPointerException {
+    public String getKeyProperty(String property) {
         return _getKeyPropertyList().get(property);
     }
 
@@ -1950,8 +2067,7 @@
      * @exception NullPointerException if <code>s</code> is null.
      *
      */
-    public static String quote(String s)
-            throws NullPointerException {
+    public static String quote(String s) {
         final StringBuilder buf = new StringBuilder("\"");
         final int len = s.length();
         for (int i = 0; i < len; i++) {
@@ -1995,8 +2111,7 @@
      * @exception NullPointerException if <code>q</code> is null.
      *
      */
-    public static String unquote(String q)
-            throws IllegalArgumentException, NullPointerException {
+    public static String unquote(String q) {
         final StringBuilder buf = new StringBuilder();
         final int len = q.length();
         if (len < 2 || q.charAt(0) != '"' || q.charAt(len - 1) != '"')
@@ -2041,7 +2156,7 @@
      *
      * @since 1.6
      */
-    public static final ObjectName WILDCARD = Util.newObjectName("*:*");
+    public static final ObjectName WILDCARD = valueOf("*:*");
 
     // Category : Utilities <===================================
 
@@ -2064,7 +2179,7 @@
      * @exception NullPointerException if <code>name</code> is null.
      *
      */
-    public boolean apply(ObjectName name) throws NullPointerException {
+    public boolean apply(ObjectName name) {
 
         if (name == null) throw new NullPointerException();
 
--- a/jdk/src/share/classes/javax/management/QueryNotificationFilter.java	Tue Sep 09 15:20:07 2008 -0700
+++ b/jdk/src/share/classes/javax/management/QueryNotificationFilter.java	Wed Sep 10 13:36:47 2008 +0200
@@ -170,7 +170,7 @@
     private static final long serialVersionUID = -8408613922660635231L;
 
     private static final ObjectName DEFAULT_NAME =
-            Util.newObjectName(":type=Notification");
+            ObjectName.valueOf(":type=Notification");
     private static final QueryExp trueQuery;
     static {
         ValueExp zero = Query.value(0);
--- a/jdk/src/share/classes/javax/management/event/EventClientDelegateMBean.java	Tue Sep 09 15:20:07 2008 -0700
+++ b/jdk/src/share/classes/javax/management/event/EventClientDelegateMBean.java	Wed Sep 10 13:36:47 2008 +0200
@@ -96,7 +96,7 @@
      * <code>{@value #OBJECT_NAME_STRING}</code>.
      */
     public final static ObjectName OBJECT_NAME =
-            Util.newObjectName(OBJECT_NAME_STRING);
+            ObjectName.valueOf(OBJECT_NAME_STRING);
 
     /**
      * A unique listener identifier specified for an EventClient.
--- a/jdk/src/share/classes/javax/management/namespace/MBeanServerSupport.java	Tue Sep 09 15:20:07 2008 -0700
+++ b/jdk/src/share/classes/javax/management/namespace/MBeanServerSupport.java	Wed Sep 10 13:36:47 2008 +0200
@@ -193,14 +193,6 @@
  * }
  *
  * <a name="PropsMBS"></a>public class PropsMBS extends MBeanServerSupport {
- *     private static ObjectName newObjectName(String name) {
- *         try {
- *             return new ObjectName(name);
- *         } catch (MalformedObjectNameException e) {
- *             throw new AssertionError(e);
- *         }
- *     }
- *
  *     public static class PropertyImpl implements PropertyMBean {
  *         private final String name;
  *
@@ -219,7 +211,7 @@
  *             throws InstanceNotFoundException {
  *
  *         // Check that the name is a legal one for a Property MBean
- *         ObjectName namePattern = newObjectName(
+ *         ObjectName namePattern = ObjectName.valueOf(
  *                     "com.example:type=Property,name=\"*\"");
  *         if (!namePattern.apply(name))
  *             throw new InstanceNotFoundException(name);
@@ -239,7 +231,7 @@
  *         {@code Set<ObjectName> names = new TreeSet<ObjectName>();}
  *         Properties props = System.getProperties();
  *         for (String propName : props.stringPropertyNames()) {
- *             ObjectName objectName = newObjectName(
+ *             ObjectName objectName = ObjectName.valueOf(
  *                     "com.example:type=Property,name=" +
  *                     ObjectName.quote(propName));
  *             names.add(objectName);
@@ -278,7 +270,7 @@
  *     }
  *
  *     public void propertyChanged(String name, String newValue) {
- *         ObjectName objectName = newObjectName(
+ *         ObjectName objectName = ObjectName.valueOf(
  *                 "com.example:type=Property,name=" + ObjectName.quote(name));
  *         Notification n = new Notification(
  *                 "com.example.property.changed", objectName, 0L,
--- a/jdk/src/share/classes/sun/management/Util.java	Tue Sep 09 15:20:07 2008 -0700
+++ b/jdk/src/share/classes/sun/management/Util.java	Wed Sep 10 13:36:47 2008 +0200
@@ -43,12 +43,8 @@
         return (String[]) list.toArray(EMPTY_STRING_ARRAY);
     }
 
-    static ObjectName newObjectName(String name) {
-        return com.sun.jmx.mbeanserver.Util.newObjectName(name);
-    }
-
     public static ObjectName newObjectName(String domainAndType, String name) {
-        return newObjectName(domainAndType + ",name=" + name);
+        return ObjectName.valueOf(domainAndType + ",name=" + name);
     }
 
     private static ManagementPermission monitorPermission =
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/management/ObjectName/ValueOfTest.java	Wed Sep 10 13:36:47 2008 +0200
@@ -0,0 +1,175 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6734813
+ * @summary Test the ObjectName.valueOf methods
+ * @author Eamonn McManus
+ */
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.Hashtable;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+
+public class ValueOfTest {
+    public static void main(String[] args) throws Exception {
+        // Calls that should work
+        testPositive("d:foo=bar,baz=buh");
+        testPositive("foo", "bar", "baz");
+        Hashtable<String, String> h = new Hashtable<String, String>();
+        h.put("foo", "bar");
+        h.put("baz", "buh");
+        testPositive("domain", h);
+
+        // Calls that should not work
+        testNegative("d");
+        testNegative("d:");
+        testNegative("d::foo=bar");
+        testNegative("d:", "foo", "bar");
+        testNegative("d", "foo=", "bar");
+        testNegative("d:", h);
+        testNegative("d", new Hashtable<String, String>());
+    }
+
+    private static void testPositive(Object... args) throws Exception {
+        Method valueOf = valueOfMethod(args);
+        Method getInstance = getInstanceMethod(args);
+        Constructor<?> constructor = constructor(args);
+
+        Object valueOfValue = valueOf.invoke(null, args);
+        Object getInstanceValue = getInstance.invoke(null, args);
+        Object constructorValue = constructor.newInstance(args);
+
+        String argString =
+                Arrays.toString(args).replace('[', '(').replace(']', ')');
+
+        if (!valueOfValue.equals(getInstanceValue)) {
+            throw new Exception(
+                    "valueOf" + argString + " differs from getInstance" +
+                    argString);
+        }
+
+        if (!valueOfValue.equals(constructorValue)) {
+            throw new Exception(
+                    "valueOf" + argString + " differs from new ObjectName " +
+                    argString);
+        }
+
+        System.out.println("OK: valueOf" + argString);
+    }
+
+    private static void testNegative(Object... args) throws Exception {
+        Method valueOf = valueOfMethod(args);
+        Method getInstance = getInstanceMethod(args);
+
+        String argString =
+                Arrays.toString(args).replace('[', '(').replace(']', ')');
+
+        final Throwable valueOfException;
+        try {
+            valueOf.invoke(null, args);
+            throw new Exception("valueOf" + argString + " did not fail but should");
+        } catch (InvocationTargetException e) {
+            valueOfException = e.getCause();
+        }
+        if (!(valueOfException instanceof IllegalArgumentException)) {
+            throw new Exception(
+                    "valueOf" + argString + " threw " +
+                    valueOfException.getClass().getName() + " instead of " +
+                    "IllegalArgumentException", valueOfException);
+        }
+
+        final Throwable valueOfCause = valueOfException.getCause();
+        if (!(valueOfCause instanceof MalformedObjectNameException)) {
+            throw new Exception(
+                    "valueOf" + argString + " threw exception with wrong " +
+                    "type of cause", valueOfCause);
+        }
+
+        if (!valueOfException.getMessage().equals(valueOfCause.getMessage())) {
+            // The IllegalArgumentException should have the same message as
+            // the MalformedObjectNameException it wraps.
+            // This isn't specified but is desirable.
+            throw new Exception(
+                    "valueOf" + argString + ": message in wrapping " +
+                    "IllegalArgumentException (" + valueOfException.getMessage() +
+                    ") differs from message in wrapped " +
+                    "MalformedObjectNameException (" + valueOfCause.getMessage() +
+                    ")");
+        }
+
+        final Throwable getInstanceException;
+        try {
+            getInstance.invoke(null, args);
+            throw new Exception("getInstance" + argString + " did not fail but should");
+        } catch (InvocationTargetException e) {
+            getInstanceException = e.getCause();
+        }
+        if (!(getInstanceException instanceof MalformedObjectNameException)) {
+            throw new Exception(
+                    "getInstance" + argString + " threw wrong exception",
+                    getInstanceException);
+        }
+
+        if (!valueOfException.getMessage().equals(getInstanceException.getMessage())) {
+            // Again this is not specified.
+            throw new Exception(
+                    "Exception message from valueOf" + argString + " (" +
+                    valueOfException.getMessage() + ") differs from message " +
+                    "from getInstance" + argString + " (" +
+                    getInstanceException.getMessage() + ")");
+        }
+
+        System.out.println("OK (correct exception): valueOf" + argString);
+    }
+
+    private static Method valueOfMethod(Object[] args) throws Exception {
+        return method("valueOf", args);
+    }
+
+    private static Method getInstanceMethod(Object[] args) throws Exception {
+        return method("getInstance", args);
+    }
+
+    private static Method method(String name, Object[] args) throws Exception {
+        Class<?>[] argTypes = argTypes(args);
+        return ObjectName.class.getMethod(name, argTypes);
+    }
+
+    private static Constructor<?> constructor(Object[] args) throws Exception {
+        Class<?>[] argTypes = argTypes(args);
+        return ObjectName.class.getConstructor(argTypes);
+    }
+
+    private static Class<?>[] argTypes(Object[] args) {
+        Class<?>[] argTypes = new Class<?>[args.length];
+        for (int i = 0; i < args.length; i++)
+            argTypes[i] = args[i].getClass();
+        return argTypes;
+    }
+}