lundi 14 septembre 2015

Why does Sun prefer NoSuchMethodError + NullPointerException instead of Class inspection?

I did a small debug session and stumbled over the following code snippet in Sun/Oracle's code:

try {
        XmlSchema s = null;
        s.location();
    } catch (NullPointerException e) {
        // as epxected
    } catch (NoSuchMethodError e) {
         // this is not a 2.1 API. Where is it being loaded from?
         ...
    }

I think this has several flaws by first of all expecting a NullPointerException, using ((XmlSchema)null).location(); and expecting a NoSuchMethodError. If I would do the code review I would flag this as unappropriated code by triggering an exception as a regular event.

In my opinion using XmlSchema.getClass().getMethod("location", new Object[0]) would be a some what better idea.

Since this code is used in the official API implementation I wonder if there are edge cases, I am not aware of which prevent class inspection being a good solution and one has to use this code snippet as a fall back.





Aucun commentaire:

Enregistrer un commentaire