问题
I found some strange code in OpenJDK JAXB:
com.sun.xml.internal.bind.v2.model.impl.ModelBuilder
try {
XmlSchema s = null;
s.location();
} catch (NullPointerException e) {
// as expected
} catch (NoSuchMethodError e) {
...
}
Can some explain why they do this? Or this is just a bad code need to fix.
回答1:
They are using this code as a test to determine which version of the JAXB (JSR-222) APIs are being used. The location
parameter was added to @XmlSchema
in JAXB 2.1, if NoSuchMethodError
was thrown then JAXB 2.0 APIs are being used.
See Lines 158-177
- http://grepcode.com/file/repo1.maven.org/maven2/org.jvnet.jaxb.reflection/jaxb2-reflection/2.1.4/org/jvnet/jaxb/reflection/model/impl/ModelBuilder.java
Javadoc - @XmlSchema.location
- http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/XmlSchema.html#location()
来源:https://stackoverflow.com/questions/13693577/strange-nullpointerexception-catch-in-openjdk-jaxb-implementation