-
Enhancement
-
Resolution: Fixed
-
P3
-
6
-
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.
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.
- duplicates
-
JDK-6336961 Add a way to make an ObjectName without having to catch MalformedObjectNameException
- Closed