Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6734813

Provide a way to construct an ObjectName without checked exceptions

XMLWordPrintable

    • 5.0
    • b37
    • generic
    • generic
    • Verified

      All of the constructors of ObjectName and all of the static ObjectName.getInstance factory methods are declared to throw MalformedObjectNameException, which is a checked exception. This is very irritating, because it means you can't write for example:

      public class Whatever {
          private static final ObjectName mbeanName = new ObjectName("d:type=Foo");
          ...
      }

      You have to write the much more verbose:

      public class Whatever {
          private static final ObjectName mbeanName;
          static {
              try {
                  mbeanName = new ObjectName("d:type=Foo");
              } catch (MalformedObjectNameException e) {
                  throw new AssertionError(e);
              }
          }
          ...
      }

      Most people who run into this problem probably define their own factory method that does what the static{} block in the second example does:

          private static final ObjectName mbeanName = Util.newObjectName("d:type=Foo");

      but it's stupid that they should have to.

            emcmanus Eamonn McManus
            emcmanus Eamonn McManus
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: