| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | packagecom.yunkapay.push.mobile.parcelable;importandroid.os.Parcel;importandroid.os.Parcelable;importandroid.util.Log;importcom.yunkapay.push.mobile.util.Logger;importjava.lang.reflect.*;importjava.util.*;publicabstractclassGlobalParcelable<T extendsGlobalParcelable> implementsParcelable {    publicGlobalParcelable() {        // TODO Auto-generated constructor stub    }    publicGlobalParcelable(Parcel in) {        String className = in.readString();        Log.i("GlobalParcelable", "Constructor: "+ ((Object) this).getClass().getSimpleName() + "; In parcel: "+ className);        readFromParcel(in);    }    @Override    publicvoidwriteToParcel(Parcel dest, intflags) {        dest.writeString(((Object) this).getClass().getName());        try{            dehydrate(this, dest);        } catch(IllegalArgumentException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch(IllegalAccessException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    publicvoidreadFromParcel(Parcel in) {        try{            rehydrate(this, in);        } catch(IllegalArgumentException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch(IllegalAccessException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    @Override    publicintdescribeContents() {        // TODO Auto-generated method stub        return0;    }    // writes fields of a GlobalParcelable to a parcel    // does not include the first parcelled item -- the class name    protectedvoiddehydrate(GlobalParcelable model, Parcel out) throwsIllegalArgumentException, IllegalAccessException {        Class<?> cla = ((Object) model).getClass();        Log.i("GlobalParcelable", "dehydrating... "+ cla.toString());        // get the fields        Field[] fields=getFields(cla);        // sort the fields so it is in deterministic order        Arrays.sort(fields, compareMemberByName);        // populate the fields        for(Field field : fields) {            field.setAccessible(true);            intmodifier = field.getModifiers();            Log.i("GlobalParcelable", "Field:"+ field.getName() + " "+ Modifier.isStatic(modifier) + " "+ Modifier.isFinal(modifier));            if(field.getType().equals(int.class)) {                out.writeInt(field.getInt(model));            } elseif(field.getType().equals(double.class)) {                out.writeDouble(field.getDouble(model));            } elseif(field.getType().equals(float.class)) {                out.writeFloat(field.getFloat(model));            } elseif(field.getType().equals(long.class)) {                out.writeLong(field.getLong(model));            } elseif(field.getType().equals(String.class)) {                out.writeString((String) field.get(model));            } elseif(field.getType().equals(boolean.class)) {                out.writeByte(field.getBoolean(model) ? (byte) 1: (byte) 0);            } elseif(field.getType().equals(Date.class)) {                Date date = (Date) field.get(model);                if(date != null) {                    out.writeLong(date.getTime());                } else{                    out.writeLong(0);                }            } elseif(GlobalParcelable.class.isAssignableFrom(field.getType())) {                // why did this happen?                Log.e("GlobalParcelable", "GlobalParcelable F*ck up: "+ " ("+ field.getType().toString() + ")");                out.writeParcelable((GlobalParcelable) field.get(model), 0);            } else{                // wtf                Log.e("GlobalParcelable", "Could not write field to parcel: "+ " ("+ field.getType().toString() + ")");            }        }    }    protectedstaticField[] getFields(Class<?> cla) {        List<Field> fieldList = newArrayList<Field>();        do{            Logger.d("Class "+cla.toString());            Field[] fields = cla.getDeclaredFields();            for(Field f : fields) {                intmodifier = f.getModifiers();                if(Modifier.isStatic(modifier) && Modifier.isFinal(modifier)) {                    Logger.d("final static value "+f.getName());                } else{                    fieldList.add(f);                }            }            cla = cla.getSuperclass();        } while(cla != null&& !GlobalParcelable.class.equals(cla));        returnfieldList.toArray(newField[fieldList.size()]);    }    // reads the parcelled items and put them into this object‘s fields    // must be run after getting the first parcelled item -- the class name    protectedstaticvoidrehydrate(GlobalParcelable model, Parcel in) throwsIllegalArgumentException, IllegalAccessException {        Class<?> cla = ((Object) model).getClass();        Log.i("GlobalParcelable", "rehydrating... "+ cla.toString());        // get the fields        Field[] fields =getFields(cla);        // sort the fields so it is in deterministic order        Arrays.sort(fields, compareMemberByName);        // populate the fields        for(Field field : fields) {            field.setAccessible(true);            if(field.getType().equals(int.class)) {                field.set(model, in.readInt());            } elseif(field.getType().equals(double.class)) {                field.set(model, in.readDouble());            } elseif(field.getType().equals(float.class)) {                field.set(model, in.readFloat());            } elseif(field.getType().equals(long.class)) {                field.set(model, in.readLong());            } elseif(field.getType().equals(String.class)) {                field.set(model, in.readString());            } elseif(field.getType().equals(boolean.class)) {                field.set(model, in.readByte() == 1);            } elseif(field.getType().equals(Date.class)) {                Date date = newDate(in.readLong());                field.set(model, date);            } elseif(GlobalParcelable.class.isAssignableFrom(field.getType())) {                Log.e("GlobalParcelable", "read GlobalParcelable: "+ " ("+ field.getType().toString() + ")");                field.set(model, in.readParcelable(field.getType().getClassLoader()));            } else{                // wtf                Log.e("GlobalParcelable", "Could not read field from parcel: "+ field.getName() + " ("+ field.getType().toString() + ")");            }        }    }    /*     * Comparator object for Members, Fields, and Methods     */    privatestaticComparator<Field> compareMemberByName =            newCompareMemberByName();    privatestaticclassCompareMemberByName implementsComparator {        publicintcompare(Object o1, Object o2) {            String s1 = ((Member) o1).getName();            String s2 = ((Member) o2).getName();            if(o1 instanceofMethod) {                s1 += getSignature((Method) o1);                s2 += getSignature((Method) o2);            } elseif(o1 instanceofConstructor) {                s1 += getSignature((Constructor) o1);                s2 += getSignature((Constructor) o2);            }            returns1.compareTo(s2);        }    }    /**     * Compute the JVM signature for the class.     */    privatestaticString getSignature(Class clazz) {        String type = null;        if(clazz.isArray()) {            Class cl = clazz;            intdimensions = 0;            while(cl.isArray()) {                dimensions++;                cl = cl.getComponentType();            }            StringBuffer sb = newStringBuffer();            for(inti = 0; i < dimensions; i++) {                sb.append("[");            }            sb.append(getSignature(cl));            type = sb.toString();        } elseif(clazz.isPrimitive()) {            if(clazz == Integer.TYPE) {                type = "I";            } elseif(clazz == Byte.TYPE) {                type = "B";            } elseif(clazz == Long.TYPE) {                type = "J";            } elseif(clazz == Float.TYPE) {                type = "F";            } elseif(clazz == Double.TYPE) {                type = "D";            } elseif(clazz == Short.TYPE) {                type = "S";            } elseif(clazz == Character.TYPE) {                type = "C";            } elseif(clazz == Boolean.TYPE) {                type = "Z";            } elseif(clazz == Void.TYPE) {                type = "V";            }        } else{            type = "L"+ clazz.getName().replace(‘.‘, ‘/‘) + ";";        }        returntype;    }    /*     * Compute the JVM method descriptor for the method.     */    privatestaticString getSignature(Method meth) {        StringBuffer sb = newStringBuffer();        sb.append("(");        Class[] params = meth.getParameterTypes(); // avoid clone        for(intj = 0; j < params.length; j++) {            sb.append(getSignature(params[j]));        }        sb.append(")");        sb.append(getSignature(meth.getReturnType()));        returnsb.toString();    }    /*     * Compute the JVM constructor descriptor for the constructor.     */    privatestaticString getSignature(Constructor cons) {        StringBuffer sb = newStringBuffer();        sb.append("(");        Class[] params = cons.getParameterTypes(); // avoid clone        for(intj = 0; j < params.length; j++) {            sb.append(getSignature(params[j]));        }        sb.append(")V");        returnsb.toString();    }} | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | packagecom.yunkapay.push.mobile.notification;importandroid.os.Parcel;importandroid.os.Parcelable;importcom.yunkapay.push.mobile.parcelable.GlobalParcelable;importcom.yunkapay.push.mobile.util.Logger;importorg.json.JSONObject;/** * Created with IntelliJ IDEA. * User: zac * Date: 12/2/13 * Time: 2:26 PM * To change this template use File | Settings | File Templates. */publicclassXJNotification extendsGlobalParcelable {    publicstaticfinalString ELEMENT_NAME = "notification";    publicstaticfinalString NAMESPACE = "pubsub:message:notification";    publicstaticfinalString NOTIFICATION_NODE = "node";    publicstaticfinalString NOTIFICATION_ID = "id";    publicstaticfinalString NOTIFICATION_TITLE = "title";    publicstaticfinalString NOTIFICATION_CONTENT = "content";    publicstaticfinalString NOTIFICATION_TYPE = "type";    publicstaticfinalString NOTIFICATION_IMAGE_URL = "image_url";    publicstaticfinalString NOTIFICATION_LINK = "link";    publicstaticfinalString NOTIFICATION_TIME = "time";    publicstaticfinalString NOTIFICATION_TIME_TO_LIVE = "time_to_live";    publicString mNotificationID;    publicString mNode;    publicString mTitle;    publicString mContent;    publiclongmTime;    publicintmTimeToLive;    publicXJNotification(JSONObject jsonObject) {    }    publicXJNotification() {        super();    }    publicstaticfinalParcelable.Creator<XJNotification> CREATOR = newParcelable.Creator<XJNotification>() {        publicXJNotification createFromParcel(Parcel in) {            // get class from first parcelled item            Class<?> parceledClass;            try{                parceledClass = Class.forName(in.readString());                Logger.i("Creator: "+ parceledClass.getSimpleName());                // create instance of that class                XJNotification model = (XJNotification) parceledClass.newInstance();                rehydrate(model, in);                returnmodel;            } catch(ClassNotFoundException e) {                // TODO Auto-generated catch block                e.printStackTrace();            } catch(InstantiationException e) {                // TODO Auto-generated catch block                e.printStackTrace();            } catch(IllegalAccessException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            returnnull;        }        publicXJNotification[] newArray(intsize) {            returnnewXJNotification[size];        }    };    publicString getNotificationID() {        returnthis.mNotificationID;    }    publicString getNode() {        returnthis.mNode;    }    publicString getTitle() {        returnthis.mTitle;    }    publicString getContent() {        returnthis.mContent;    }    publicstaticString getElementName() {        returnELEMENT_NAME;    }    publiclonggetTime() {        returnthis.mTime;    }    publicintgetTimeToLive() {        returnthis.mTimeToLive;    }    publicvoidsetNotificationID(String notificationID) {        this.mNotificationID = notificationID;    }    publicvoidsetNode(String node) {        this.mNode = node;    }    publicvoidsetTitle(String title) {        this.mTitle = title;    }    publicvoidsetContent(String content) {        this.mContent = content;    }    publicvoidsetTime(longtime) {        this.mTime = time;    }    publicvoidsetTimeToLive(inttimeToLive) {        this.mTimeToLive = timeToLive;    }    publicNotificationType getType() {        returnNotificationType.UNKNOWN;    }} | 
原文:http://www.cnblogs.com/starblogs/p/3726199.html