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

Add a way to make an ObjectName without having to catch MalformedObjectNameException

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • 7
    • 6
    • core-svc
    • None

      When you need to make an instance of ObjectName, you can use a constructor or you can use one of its static factory methods, but in all cases you must catch MalformedObjectNameException, which is a checked exception. This is a pain, especially when initializing fields. For example, you can't do this:

          private static final ObjectName MY_NAME = new ObjectName("a:b=c");

      Instead you have to do this:

          private static final ObjectName MY_NAME;
          static {
              try {
                  MY_NAME = new ObjectName("a:b=c");
              } catch (MalformedObjectNameException e) {
                  throw new IllegalArgumentException(e);
              }
          }

      In practice, if you need to do this sort of thing a lot, you'll define a utility method that encapsulates the construction and the try/catch. Such a method (or set of overloaded methods) should be part of the API.

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: