前言
這周QA報(bào)了一個(gè)小bug,頁(yè)面A傳給頁(yè)面B的數(shù)據(jù)順序不對(duì),查了一下代碼,原來(lái)頁(yè)面A中數(shù)據(jù)存儲(chǔ)容器用的是HashMap,而HasMap存取是無(wú)序的,所以傳給B去讀數(shù)據(jù)的時(shí)候,自然順序不對(duì)。
解決
既然HashMap是無(wú)序的,那我直接用LinkedHashMap來(lái)代替不就行了,大多數(shù)人估計(jì)看到這個(gè)bug時(shí),開(kāi)始都是這么想的。于是我就順手在HashMap前加了一個(gè)Linked,點(diǎn)了一下run,泯上一口茶,靜靜等待著奇跡的發(fā)生。
然而奇跡沒(méi)有來(lái)臨,奇怪的事反倒是發(fā)生了,B頁(yè)面收到數(shù)據(jù)后,居然報(bào)了一個(gè)類(lèi)型強(qiáng)轉(zhuǎn)錯(cuò)誤,B收到的是HashMap,而不是LinkedHashMap,怎么可能!!!!我趕緊放下茶杯,review了一下代碼,沒(méi)錯(cuò)啊,A頁(yè)面?zhèn)鬟f的確實(shí)是LinkedHashMap,但是B拿到就是HashMap,真是活見(jiàn)鬼了。
我立馬Google了一下,遇到這個(gè)錯(cuò)誤的人還真不少,評(píng)論區(qū)給出的一種解決方案就是用Gson將LinkedHashMap序列化成String,再進(jìn)行傳遞。。。由于bug催的緊,我也沒(méi)有去嘗試這種方法了,直接就放棄了傳遞Map,改用ArrayList了。不過(guò)后來(lái)看源碼,又發(fā)現(xiàn)了另外一種方式,稍后再說(shuō)。
原因
Bug倒是解決了,但是Intent無(wú)法傳遞LinkedHashMap的問(wèn)題還在我腦海里縈繞,我就稍微翻看了一下源碼,恍然大悟!
HashMap實(shí)現(xiàn)了Serializable接口,而LinkedHashMap是繼承自HashMap的,所以用Intent傳遞是沒(méi)有問(wèn)題的,我們先來(lái)追一下A頁(yè)面?zhèn)鬟f的地方:
intent.putExtra("map",new LinkedHashMap<>());
接著往里看:
public Intent putExtra(String name, Serializable value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putSerializable(name, value);
return this;
}
intent是直接構(gòu)造了一個(gè)Bundle,將數(shù)據(jù)傳遞到Bundle里,Bundle.putSerializable()里其實(shí)也是直接調(diào)用了父類(lèi)BaseBundle.putSerializable():
void putSerializable(@Nullable String key, @Nullable Serializable value) {
unparcel();
mMap.put(key, value);
}
這里直接將value放入了一個(gè)ArrayMap中,并沒(méi)有做什么特殊處理。
事情到這似乎沒(méi)有了下文,那么這個(gè)LinkedHashMap又是何時(shí)轉(zhuǎn)為HashMap的呢?有沒(méi)有可能是在startActivity()中做的處理呢?
了解activity啟動(dòng)流程的工程師應(yīng)該清楚,startActivity()最后調(diào)的是:
ActivityManagerNative.getDefault().startActivity()
ActivityManagerNative是個(gè)Binder對(duì)象,其功能實(shí)現(xiàn)是在ActivityManagerService中,而其在app進(jìn)程中的代理對(duì)象則為ActivityManagerProxy。所以上面的startActivity()最后調(diào)用的是ActivityManagerProxy.startActivity(),我們來(lái)看看這個(gè)方法的源碼:
public int startActivity(IApplicationThread caller, String callingPackage, Intent intent,
String resolvedType, IBinder resultTo, String resultWho, int requestCode,
int startFlags, ProfilerInfo profilerInfo, Bundle options) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
......
intent.writeToParcel(data, 0);
......
int result = reply.readInt();
reply.recycle();
data.recycle();
return result;
}
注意到方法中調(diào)用了intent.writeToParcel(data, 0),難道這里做了什么特殊處理?
public void writeToParcel(Parcel out, int flags) {
out.writeString(mAction);
Uri.writeToParcel(out, mData);
out.writeString(mType);
out.writeInt(mFlags);
out.writeString(mPackage);
......
out.writeBundle(mExtras);
}
最后一行調(diào)用了Parcel.writeBundle()方法,傳參為mExtras,而之前的LinkedHashMap就放在這mExtras中。
public final void writeBundle(Bundle val) {
if (val == null) {
writeInt(-1);
return;
}
val.writeToParcel(this, 0);
}
這里最后調(diào)用了Bundle.writeToParcel(),最終會(huì)調(diào)用到其父類(lèi)BaseBundle的writeToParcelInner():
void writeToParcelInner(Parcel parcel, int flags) {
// Keep implementation in sync with writeToParcel() in
// frameworks/native/libs/binder/PersistableBundle.cpp.
final Parcel parcelledData;
synchronized (this) {
parcelledData = mParcelledData;
}
if (parcelledData != null) {
......
} else {
// Special case for empty bundles.
if (mMap == null || mMap.size() <= 0) {
parcel.writeInt(0);
return;
}
......
parcel.writeArrayMapInternal(mMap);
......
}
}
可見(jiàn)最后else分支里,會(huì)調(diào)用Parcel.writeArrayMapInternal(mMap),這個(gè)mMap即為Bundle中存儲(chǔ)K-V的ArrayMap,看看這里有沒(méi)有對(duì)mMap做特殊處理:
void writeArrayMapInternal(ArrayMap<String, Object> val) {
if (val == null) {
writeInt(-1);
return;
}
// Keep the format of this Parcel in sync with writeToParcelInner() in
// frameworks/native/libs/binder/PersistableBundle.cpp.
final int N = val.size();
writeInt(N);
if (DEBUG_ARRAY_MAP) {
RuntimeException here = new RuntimeException("here");
here.fillInStackTrace();
Log.d(TAG, "Writing " + N + " ArrayMap entries", here);
}
int startPos;
for (int i=0; i<N; i++) {
if (DEBUG_ARRAY_MAP) startPos = dataPosition();
writeString(val.keyAt(i));
writeValue(val.valueAt(i));
if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " "
+ (dataPosition()-startPos) + " bytes: key=0x"
+ Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0)
+ " " + val.keyAt(i));
}
}
在最后的for循環(huán)中,會(huì)遍歷mMap中所有的K-V對(duì),先調(diào)用writeString()寫(xiě)入Key,再調(diào)用writeValue()來(lái)寫(xiě)入Value。真相就在writeValue()里:
public final void writeValue(Object v) {
if (v == null) {
writeInt(VAL_NULL);
} else if (v instanceof String) {
writeInt(VAL_STRING);
writeString((String) v);
} else if (v instanceof Integer) {
writeInt(VAL_INTEGER);
writeInt((Integer) v);
} else if (v instanceof Map) {
writeInt(VAL_MAP);
writeMap((Map) v);
}
......
......
}
這里會(huì)判斷value的具體類(lèi)型,如果是Map類(lèi)型,會(huì)先寫(xiě)入一個(gè)VAL_MAP的類(lèi)型常量,緊接著調(diào)用writeMap()寫(xiě)入value。writeMap()最后走到了writeMapInternal():
void writeMapInternal(Map<String,Object> val) {
if (val == null) {
writeInt(-1);
return;
}
Set<Map.Entry<String,Object>> entries = val.entrySet();
writeInt(entries.size());
for (Map.Entry<String,Object> e : entries) {
writeValue(e.getKey());
writeValue(e.getValue());
}
}
可見(jiàn),這里并沒(méi)有直接將LinkedHashMap序列化,而是遍歷其中所有K-V,依次寫(xiě)入每個(gè)Key和Value,所以L(fǎng)inkedHashMap到這時(shí)就已經(jīng)失去意義了。
那么B頁(yè)面在讀取這個(gè)LinkedHashMap的時(shí)候,是什么情況呢?從Intent中讀取數(shù)據(jù)時(shí),最終會(huì)走到getSerializable():
Serializable getSerializable(@Nullable String key) {
unparcel();
Object o = mMap.get(key);
if (o == null) {
return null;
}
try {
return (Serializable) o;
} catch (ClassCastException e) {
typeWarning(key, o, "Serializable", e);
return null;
}
}
這里乍一看就是直接從mMap中通過(guò)key取到value,其實(shí)重要的邏輯全都在第一句unparcel()中:
synchronized void unparcel() {
synchronized (this) {
final Parcel parcelledData = mParcelledData;
if (parcelledData == null) {
if (DEBUG) Log.d(TAG, "unparcel "
+ Integer.toHexString(System.identityHashCode(this))
+ ": no parcelled data");
return;
}
if (LOG_DEFUSABLE && sShouldDefuse && (mFlags & FLAG_DEFUSABLE) == 0) {
Slog.wtf(TAG, "Attempting to unparcel a Bundle while in transit; this may "
+ "clobber all data inside!", new Throwable());
}
if (isEmptyParcel()) {
if (DEBUG) Log.d(TAG, "unparcel "
+ Integer.toHexString(System.identityHashCode(this)) + ": empty");
if (mMap == null) {
mMap = new ArrayMap<>(1);
} else {
mMap.erase();
}
mParcelledData = null;
return;
}
int N = parcelledData.readInt();
if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this))
+ ": reading " + N + " maps");
if (N < 0) {
return;
}
ArrayMap<String, Object> map = mMap;
if (map == null) {
map = new ArrayMap<>(N);
} else {
map.erase();
map.ensureCapacity(N);
}
try {
parcelledData.readArrayMapInternal(map, N, mClassLoader);
} catch (BadParcelableException e) {
if (sShouldDefuse) {
Log.w(TAG, "Failed to parse Bundle, but defusing quietly", e);
map.erase();
} else {
throw e;
}
} finally {
mMap = map;
parcelledData.recycle();
mParcelledData = null;
}
if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this))
+ " final map: " + mMap);
}
這里主要是讀取數(shù)據(jù),然后填充到mMap中,其中關(guān)鍵點(diǎn)在于parcelledData.readArrayMapInternal(map, N, mClassLoader):
void readArrayMapInternal(ArrayMap outVal, int N,
ClassLoader loader) {
if (DEBUG_ARRAY_MAP) {
RuntimeException here = new RuntimeException("here");
here.fillInStackTrace();
Log.d(TAG, "Reading " + N + " ArrayMap entries", here);
}
int startPos;
while (N > 0) {
if (DEBUG_ARRAY_MAP) startPos = dataPosition();
String key = readString();
Object value = readValue(loader);
if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " "
+ (dataPosition()-startPos) + " bytes: key=0x"
+ Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key);
outVal.append(key, value);
N--;
}
outVal.validate();
}
這里其實(shí)對(duì)應(yīng)于之前所說(shuō)的writeArrayMapInternal(),先調(diào)用readString讀出Key值,再調(diào)用readValue()讀取value值,所以重點(diǎn)還是在于readValue():
public final Object readValue(ClassLoader loader) {
int type = readInt();
switch (type) {
case VAL_NULL:
return null;
case VAL_STRING:
return readString();
case VAL_INTEGER:
return readInt();
case VAL_MAP:
return readHashMap(loader);
......
}
}
這里對(duì)應(yīng)之前的writeValue(),先讀取之間寫(xiě)入的類(lèi)型常量值,如果是VAL_MAP,就調(diào)用readHashMap():
public final HashMap readHashMap(ClassLoader loader){
int N = readInt();
if (N < 0) {
return null;
}
HashMap m = new HashMap(N);
readMapInternal(m, N, loader);
return m;
}
真相大白了,readHashMap()中直接new了一個(gè)HashMap,再依次讀取之前寫(xiě)入的K-V值,填充到HashMap中,所以B頁(yè)面拿到就是這個(gè)HashMap,而拿不到LinkedHashMap了。
一題多解
雖然不能直接傳LinkedHashMap,不過(guò)可以通過(guò)另一種方式來(lái)傳遞,那就是傳遞一個(gè)實(shí)現(xiàn)了Serializable接口的類(lèi)對(duì)象,將LinkedHashMap作為一個(gè)成員變量放入該對(duì)象中,再進(jìn)行傳遞。如:
public class MapWrapper implements Serializable {
private HashMap mMap;
public void setMap(HashMap map){
mMap=map;
}
public HashMap getMap() {
return mMap;
}
}
那么為什么這樣傳遞就行了呢?其實(shí)也很簡(jiǎn)單,因?yàn)樵趙riteValue()時(shí),如果寫(xiě)入的是Serializable對(duì)象,那么就會(huì)調(diào)用writeSerializable():
public final void writeSerializable(Serializable s) {
if (s == null) {
writeString(null);
return;
}
String name = s.getClass().getName();
writeString(name);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(s);
oos.close();
writeByteArray(baos.toByteArray());
} catch (IOException ioe) {
throw new RuntimeException("Parcelable encountered " +
"IOException writing serializable object (name = " + name +
")", ioe);
}
}
可見(jiàn)這里直接將這個(gè)對(duì)象給序列化成字節(jié)數(shù)組了,并不會(huì)因?yàn)槔锩姘粋€(gè)Map對(duì)象而再走入writeMap(),所以L(fǎng)inkedHashMap得以被保存了。
結(jié)論:
一句話(huà),遇到問(wèn)題就多看源碼!