本周支持一個系統(tǒng)升級到 JDK8寺董,發(fā)現(xiàn)了一個 JDK6 和 JDK8 的差異的新坑淑掌,具體在 com.sun.xml.internal.stream.events.StartDocumentEvent
這個類的構(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;
}
}
請注意最后的做了一個判斷茫因,傳入的 localtion 的參數(shù)不為 null 的時候痰娱,再把傳入的 location 設置到 fLocation 去,而由于 StartDocuemntEvent 繼承了 DummyEvent箍铲,所以 fLocation 的默認值是 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贮折,導致同樣的 new StartDocumentEvent
的這段代碼,在 1.6 下春畔,fLocation 不為 null脱货,在 1.8 下岛都,fLocation 為 null。
解決的辦法只能是振峻,將 new StartDocumentEvent()
的代碼改成 new StartDocumentEvent("UTF-8", "1.0", true, DummyLocation.INSTANCE)
臼疫,來保證在 JDK6 和 JDK8 下的行為是一致的了。