read Parcelable Creator - Android android.os

Android examples for android.os:Parcel

Description

read Parcelable Creator

Demo Code


//package com.java2s;
import android.os.BadParcelableException;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import java.lang.reflect.Field;
import java.util.HashMap;

public class Main {
    private static final String TAG = "ParcelUtils.class";
    private static final HashMap<ClassLoader, HashMap<String, Parcelable.Creator>> mCreators = new HashMap<ClassLoader, HashMap<String, Parcelable.Creator>>();

    public static final <T extends Parcelable> Parcelable.Creator<T> readParcelableCreator(
            Parcel p, ClassLoader loader) {

        String name = p.readString();
        if (name == null) {
            return null;
        }//from www . jav  a 2  s. co m
        Parcelable.Creator<T> creator;
        synchronized (mCreators) {
            HashMap<String, Parcelable.Creator> map = mCreators.get(loader);
            if (map == null) {
                map = new HashMap<String, Parcelable.Creator>();
                mCreators.put(loader, map);
            }
            creator = map.get(name);
            if (creator == null) {
                try {
                    Class c = loader == null ? Class.forName(name) : Class
                            .forName(name, true, loader);
                    Field f = c.getField("CREATOR");
                    creator = (Parcelable.Creator) f.get(null);
                } catch (IllegalAccessException e) {
                    Log.e(TAG,
                            "Illegal access when unmarshalling: " + name, e);
                    throw new BadParcelableException(
                            "IllegalAccessException when unmarshalling: "
                                    + name);
                } catch (ClassNotFoundException e) {
                    Log.e(TAG, "Class not found when unmarshalling: "
                            + name, e);
                    throw new BadParcelableException(
                            "ClassNotFoundException when unmarshalling: "
                                    + name);
                } catch (ClassCastException e) {
                    throw new BadParcelableException(
                            "Parcelable protocol requires a "
                                    + "Parcelable.Creator object called "
                                    + " CREATOR on class " + name);
                } catch (NoSuchFieldException e) {
                    throw new BadParcelableException(
                            "Parcelable protocol requires a "
                                    + "Parcelable.Creator object called "
                                    + " CREATOR on class " + name);
                } catch (NullPointerException e) {
                    throw new BadParcelableException(
                            "Parcelable protocol requires "
                                    + "the CREATOR object to be static on class "
                                    + name);
                }
                if (creator == null) {
                    throw new BadParcelableException(
                            "Parcelable protocol requires a "
                                    + "Parcelable.Creator object called "
                                    + " CREATOR on class " + name);
                }

                map.put(name, creator);
            }
        }

        return creator;
    }
}

Related Tutorials