StartDocumentEvent 在 JDK6 和 JDK8 下的差異

本周支持一個(gè)系統(tǒng)升級(jí)到 JDK8,發(fā)現(xiàn)了一個(gè) JDK6 和 JDK8 的差異的新坑,具體在 com.sun.xml.internal.stream.events.StartDocumentEvent 這個(gè)類的構(gòu)造方法上,在 1.6 中:

public StartDocumentEvent(String var1, String var2, boolean var3, Location var4) {
    this.init();
    this.fEncodingScheam = var1;
    this.fVersion = var2;
    this.fStandalone = var3;
    this.fEncodingSchemeSet = false;
    this.fStandaloneSet = false;
    if (var4 != null) {
        this.fLocation = var4;
    }

}

請(qǐng)注意最后的做了一個(gè)判斷,傳入的 localtion 的參數(shù)不為 null 的時(shí)候,再把傳入的 location 設(shè)置到 fLocation 去,而由于 StartDocuemntEvent 繼承了 DummyEvent,所以 fLocation 的默認(rèn)值是 DummyLocation.INSTANCE(見 DummyEvent 的構(gòu)造函數(shù))。

但是到了 1.8 下面,1.6 的部分的構(gòu)造函數(shù)中的代碼移到了 init 方法中,其代碼如下:

public StartDocumentEvent() {
    this.init("UTF-8", "1.0", true, (Location)null);
}

protected void init(String encoding, String version, boolean standalone, Location loc) {
    this.setEventType(7);
    this.fEncodingScheam = encoding;
    this.fVersion = version;
    this.fStandalone = standalone;
    if (encoding != null && !encoding.equals("")) {
        this.fEncodingSchemeSet = true;
    } else {
        this.fEncodingSchemeSet = false;
        this.fEncodingScheam = "UTF-8";
    }

    this.fLocation = loc;
}

從上面的代碼中看到,在 JDK 1.8 中,并沒有做 location 為 null 的判斷,而是直接賦值給了 fLocation,導(dǎo)致同樣的 new StartDocumentEvent 的這段代碼,在 1.6 下,fLocation 不為 null,在 1.8 下,fLocation 為 null。

解決的辦法只能是,將 new StartDocumentEvent() 的代碼改成 new StartDocumentEvent("UTF-8", "1.0", true, DummyLocation.INSTANCE),來保證在 JDK6 和 JDK8 下的行為是一致的了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,948評(píng)論 18 139
  • 原鏈接:http://www.cnblogs.com/langtianya/p/3757993.html JDK各...
    把愛放下會(huì)走更遠(yuǎn)閱讀 1,127評(píng)論 0 10
  • 傳送門 解讀阿里Java開發(fā)手冊(cè)(v1.1.1) - 異常日志 前言 阿里Java開發(fā)手冊(cè)談不上圣經(jīng),但確實(shí)是大量...
    kelgon閱讀 4,385評(píng)論 4 50
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,766評(píng)論 18 399
  • 《親愛的》我們之間發(fā)生了什么我一無所知你也一無所知吧只是影子交匯在地上我決定從此不再說話 《世無可戀》我的戀人在地...
    葺寶閱讀 249評(píng)論 1 4