一个Intent与LinkedHashMap的小问题

作者: 半栈工程师 | 来源:发表于2017-07-26 14:18 被阅读306次

    前言

    这周QA报了一个小bug,页面A传给页面B的数据顺序不对,查了一下代码,原来页面A中数据存储容器用的是HashMap,而HasMap存取是无序的,所以传给B去读数据的时候,自然顺序不对。

    解决

    既然HashMap是无序的,那我直接用LinkedHashMap来代替不就行了,大多数人估计看到这个bug时,开始都是这么想的。于是我就顺手在HashMap前加了一个Linked,点了一下run,泯上一口茶,静静等待着奇迹的发生。

    然而奇迹没有来临,奇怪的事反倒是发生了,B页面收到数据后,居然报了一个类型强转错误,B收到的是HashMap,而不是LinkedHashMap,怎么可能!!!!我赶紧放下茶杯,review了一下代码,没错啊,A页面传递的确实是LinkedHashMap,但是B拿到就是HashMap,真是活见鬼了。

    我立马Google了一下,遇到这个错误的人还真不少,评论区给出的一种解决方案就是用Gson将LinkedHashMap序列化成String,再进行传递。。。由于bug催的紧,我也没有去尝试这种方法了,直接就放弃了传递Map,改用ArrayList了。不过后来看源码,又发现了另外一种方式,稍后再说。

    原因

    Bug倒是解决了,但是Intent无法传递LinkedHashMap的问题还在我脑海里萦绕,我就稍微翻看了一下源码,恍然大悟!

    HashMap实现了Serializable接口,而LinkedHashMap是继承自HashMap的,所以用Intent传递是没有问题的,我们先来追一下A页面传递的地方:

    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是直接构造了一个Bundle,将数据传递到Bundle里,Bundle.putSerializable()里其实也是直接调用了父类BaseBundle.putSerializable():

    void putSerializable(@Nullable String key, @Nullable Serializable value) {
        unparcel();
        mMap.put(key, value);
    }
    

    这里直接将value放入了一个ArrayMap中,并没有做什么特殊处理。

    事情到这似乎没有了下文,那么这个LinkedHashMap又是何时转为HashMap的呢?有没有可能是在startActivity()中做的处理呢?

    了解activity启动流程的工程师应该清楚,startActivity()最后调的是:

     ActivityManagerNative.getDefault().startActivity()
    

    ActivityManagerNative是个Binder对象,其功能实现是在ActivityManagerService中,而其在app进程中的代理对象则为ActivityManagerProxy。所以上面的startActivity()最后调用的是ActivityManagerProxy.startActivity(),我们来看看这个方法的源码:

    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;
    }
    

    注意到方法中调用了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);
    }
    

    最后一行调用了Parcel.writeBundle()方法,传参为mExtras,而之前的LinkedHashMap就放在这mExtras中。

        public final void writeBundle(Bundle val) {
        if (val == null) {
            writeInt(-1);
            return;
        }
    
        val.writeToParcel(this, 0);
    }
    

    这里最后调用了Bundle.writeToParcel(),最终会调用到其父类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);
            ......
        }
    }
    

    可见最后else分支里,会调用Parcel.writeArrayMapInternal(mMap),这个mMap即为Bundle中存储K-V的ArrayMap,看看这里有没有对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循环中,会遍历mMap中所有的K-V对,先调用writeString()写入Key,再调用writeValue()来写入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);
        } 
        ......
        ......
    }
    

    这里会判断value的具体类型,如果是Map类型,会先写入一个VAL_MAP的类型常量,紧接着调用writeMap()写入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());
        }
    }
    

    可见,这里并没有直接将LinkedHashMap序列化,而是遍历其中所有K-V,依次写入每个Key和Value,所以LinkedHashMap到这时就已经失去意义了。

    那么B页面在读取这个LinkedHashMap的时候,是什么情况呢?从Intent中读取数据时,最终会走到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中通过key取到value,其实重要的逻辑全都在第一句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);
        }
    

    这里主要是读取数据,然后填充到mMap中,其中关键点在于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();
    }
    

    这里其实对应于之前所说的writeArrayMapInternal(),先调用readString读出Key值,再调用readValue()读取value值,所以重点还是在于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);
    
        ......
        }
    }
    

    这里对应之前的writeValue(),先读取之间写入的类型常量值,如果是VAL_MAP,就调用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了一个HashMap,再依次读取之前写入的K-V值,填充到HashMap中,所以B页面拿到就是这个HashMap,而拿不到LinkedHashMap了。

    一题多解

    虽然不能直接传LinkedHashMap,不过可以通过另一种方式来传递,那就是传递一个实现了Serializable接口的类对象,将LinkedHashMap作为一个成员变量放入该对象中,再进行传递。如:

    public class MapWrapper implements Serializable {
    
      private HashMap mMap;
    
      public void setMap(HashMap map){
          mMap=map;
      }
    
      public HashMap getMap() {
          return mMap;
      }
    }
    

    那么为什么这样传递就行了呢?其实也很简单,因为在writeValue()时,如果写入的是Serializable对象,那么就会调用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);
        }
    }
    

    可见这里直接将这个对象给序列化成字节数组了,并不会因为里面包含一个Map对象而再走入writeMap(),所以LinkedHashMap得以被保存了。

    结论:

    一句话,遇到问题就多看源码!

    相关文章

      网友评论

      本文标题:一个Intent与LinkedHashMap的小问题

      本文链接:https://www.haomeiwen.com/subject/vqegkxtx.html