Skip to main content

Closures and language-level XML support are all good but ...

Posted by kohsuke on October 16, 2006 at 4:01 PM PDT

I just had to write this in the JAX-WS RI:

try {
    return ir.getConstructor(Class.class).newInstance(clazz);
} catch (InstantiationException e) {
    throw new WebServiceException(ServerMessages.FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER(
        ir.getName(),a.annotationType(),clazz.getName()));
} catch (IllegalAccessException e) {
    throw new WebServiceException(ServerMessages.FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER(
        ir.getName(),a.annotationType(),clazz.getName()));
} catch (InvocationTargetException e) {
    throw new WebServiceException(ServerMessages.FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER(
        ir.getName(),a.annotationType(),clazz.getName()));
} catch (NoSuchMethodException e) {
    throw new WebServiceException(ServerMessages.FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER(
        ir.getName(),a.annotationType(),clazz.getName()));
}

The closures and language-level XML support are all good, but it would be really nice if the JavaSE can fix those things that we all face in day-to-day programming. For example, I think it's already filed somewhere that we should be able to write the above as the following, where e is typed as the common base class (Exception in this case):

try {
    return ir.getConstructor(Class.class).newInstance(clazz);
} catch (InstantiationException,IllegalAccessException,InvocationTargetException,NoSuchMethodException e) {
    throw new WebServiceException(ServerMessages.FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER(
        ir.getName(),a.annotationType(),clazz.getName()));
}

(Although I guess this one could argue that this particular example is a library design problem — if only we had something like ReflectionException where all the other reflection related exceptions derive.)

Related Topics >>