Strange NullPointerException Catch in OpenJDK JAXB Implementation

拟墨画扇 提交于 2019-12-12 13:47:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!