android.os.Parcelable.Creator源码实例Demo

类android.os.Parcelable.Creator源码实例Demo

下面列出了android.os.Parcelable.Creator 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
 * Converts a {@link ByteString} into a {@link Parcelable}.
 *
 * @param byteString the {@link ByteString} encoding of the {@link Parcelable}
 * @return an instance of {@link Parcelable}
 */
@Override
public Parcelable convert(@NonNull ByteString byteString) {
  Parcel parcel = Parcel.obtain();
  byte[] bytes = byteString.toByteArray();
  Parcelable fromParcel = null;
  try {
    parcel.unmarshall(bytes, 0, bytes.length);
    // Move the current read/write position to the beginning
    parcel.setDataPosition(0);
    Field creatorField = parcelableClass.getField("CREATOR");
    Parcelable.Creator<?> creator = (Creator) creatorField.get(null);
    fromParcel = parcelableClass.cast(creator.createFromParcel(parcel));
  } catch (NoSuchFieldException nsfe) {
    throw new RemoteProtocolException(
        String.format(
            Locale.ROOT,
            "Cannot find CREATOR field for Parcelable %s",
            parcelableClass.getName()),
        nsfe);
  } catch (IllegalAccessException iae) {
    throw new RemoteProtocolException(
        String.format(
            Locale.ROOT,
            "Cannot create instance of %s. CREATOR field is inaccessible",
            parcelableClass.getName()),
        iae);
  } finally {
    if (parcel != null) {
      parcel.recycle();
    }
  }
  return fromParcel;
}
 
源代码2 项目: postman   文件: Postman.java
private static <T> Parcelable.Creator<T> newCreator(final Class<T> clazz) {
    return new Parcelable.Creator<T>() {
        @Override
        public T createFromParcel(Parcel source) {
            return readFromParcel(clazz, source);
        }

        @Override
        public T[] newArray(int size) {
            return Postman.newArray(clazz, size);
        }
    };
}
 
源代码3 项目: letv   文件: ParcelableCompat.java
public static <T> Creator<T> newCreator(ParcelableCompatCreatorCallbacks<T> callbacks) {
    if (VERSION.SDK_INT >= 13) {
        return ParcelableCompatCreatorHoneycombMR2Stub.instantiate(callbacks);
    }
    return new CompatCreator(callbacks);
}
 
源代码4 项目: android-test   文件: ParcelableSubject.java
public void recreatesEqual(Creator<T> creator) {
  T recreated = forceParcel(actual, creator);
  check("recreatesEqual()").that(actual).isEqualTo(recreated);
}
 
源代码5 项目: android-test   文件: Parcelables.java
/**
 * Parcelables are lazily marshalled, meaning that in typical testing, no marshalling would occur
 * and would therefore go untested. This forces marshalling to happen for a Parcelable.
 *
 * <p>This utility will marshall the provided Parcelable, and attempt to recreate it with the
 * given CREATOR. It is up to the caller to validate the two instances are equivalent.
 *
 * @param parcelable the parcelable to marshall.
 * @param creator the CREATOR field for that parcelable.
 * @return a new instance of the parcelable that has been unmarshalled.
 */
public static <T extends Parcelable> T forceParcel(T parcelable, Creator<T> creator) {
  Parcel parcel = Parcel.obtain();
  try {
    parcelable.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    return creator.createFromParcel(parcel);
  } finally {
    parcel.recycle();
  }
}
 
源代码6 项目: postman   文件: Postman.java
/**
 * Get a {@link Creator} that can be used for the {@code CREATOR} field of a {@link
 * Parcelable}.
 *
 * @param clazz The class associated with the type the the Creator will create.
 * @param <T> The type the Creator will create.
 *
 * @return A fully implemented Creator for the specified type.
 *
 * @throws PostmanException if there is no {@link Parceler} associated with the given type.
 */
public static <T> Parcelable.Creator<T> getCreator(Class<T> clazz) throws PostmanException {
    @SuppressWarnings("unchecked")
    Parcelable.Creator<T> creator = (Parcelable.Creator<T>) creatorMap.get(clazz);
    if (creator == null) {
        creator = newCreator(clazz);
        creatorMap.put(clazz, creator);
    }
    return creator;
}
 
源代码评论
动弹
沙发等你来抢
 类所在包
 类方法
 同包方法