diff options
-rw-r--r-- | java/src/device/main/java/com/google/protobuf/nano/android/ParcelableExtendableMessageNano.java | 17 | ||||
-rw-r--r-- | java/src/device/main/java/com/google/protobuf/nano/android/ParcelableMessageNano.java | 17 | ||||
-rw-r--r-- | java/src/device/main/java/com/google/protobuf/nano/android/ParcelableMessageNanoCreator.java (renamed from java/src/device/main/java/com/google/protobuf/nano/android/ParcelingUtil.java) | 25 | ||||
-rw-r--r-- | src/google/protobuf/compiler/javanano/javanano_message.cc | 11 |
4 files changed, 34 insertions, 36 deletions
diff --git a/java/src/device/main/java/com/google/protobuf/nano/android/ParcelableExtendableMessageNano.java b/java/src/device/main/java/com/google/protobuf/nano/android/ParcelableExtendableMessageNano.java index f3b82ed..739ff18 100644 --- a/java/src/device/main/java/com/google/protobuf/nano/android/ParcelableExtendableMessageNano.java +++ b/java/src/device/main/java/com/google/protobuf/nano/android/ParcelableExtendableMessageNano.java @@ -42,21 +42,6 @@ import com.google.protobuf.nano.ExtendableMessageNano; public abstract class ParcelableExtendableMessageNano<M extends ExtendableMessageNano<M>> extends ExtendableMessageNano<M> implements Parcelable { - // Used by Parcelable - @SuppressWarnings({"unused"}) - public static final Creator<ParcelableExtendableMessageNano<?>> CREATOR = - new Creator<ParcelableExtendableMessageNano<?>>() { - @Override - public ParcelableExtendableMessageNano<?> createFromParcel(Parcel in) { - return ParcelingUtil.createFromParcel(in); - } - - @Override - public ParcelableExtendableMessageNano<?>[] newArray(int size) { - return new ParcelableExtendableMessageNano<?>[size]; - } - }; - @Override public int describeContents() { return 0; @@ -64,6 +49,6 @@ public abstract class ParcelableExtendableMessageNano<M extends ExtendableMessag @Override public void writeToParcel(Parcel out, int flags) { - ParcelingUtil.writeToParcel(getClass(), this, out); + ParcelableMessageNanoCreator.writeToParcel(getClass(), this, out); } } diff --git a/java/src/device/main/java/com/google/protobuf/nano/android/ParcelableMessageNano.java b/java/src/device/main/java/com/google/protobuf/nano/android/ParcelableMessageNano.java index b07f1d6..83f973d 100644 --- a/java/src/device/main/java/com/google/protobuf/nano/android/ParcelableMessageNano.java +++ b/java/src/device/main/java/com/google/protobuf/nano/android/ParcelableMessageNano.java @@ -40,21 +40,6 @@ import com.google.protobuf.nano.MessageNano; */ public abstract class ParcelableMessageNano extends MessageNano implements Parcelable { - // Used by Parcelable - @SuppressWarnings("unused") - public static final Creator<ParcelableMessageNano> CREATOR = - new Creator<ParcelableMessageNano>() { - @Override - public ParcelableMessageNano createFromParcel(Parcel in) { - return ParcelingUtil.createFromParcel(in); - } - - @Override - public ParcelableMessageNano[] newArray(int size) { - return new ParcelableMessageNano[size]; - } - }; - @Override public int describeContents() { return 0; @@ -62,6 +47,6 @@ public abstract class ParcelableMessageNano extends MessageNano implements Parce @Override public void writeToParcel(Parcel out, int flags) { - ParcelingUtil.writeToParcel(getClass(), this, out); + ParcelableMessageNanoCreator.writeToParcel(getClass(), this, out); } } diff --git a/java/src/device/main/java/com/google/protobuf/nano/android/ParcelingUtil.java b/java/src/device/main/java/com/google/protobuf/nano/android/ParcelableMessageNanoCreator.java index 1eb84ee..5a4b70c 100644 --- a/java/src/device/main/java/com/google/protobuf/nano/android/ParcelingUtil.java +++ b/java/src/device/main/java/com/google/protobuf/nano/android/ParcelableMessageNanoCreator.java @@ -1,5 +1,5 @@ // Protocol Buffers - Google's data interchange format -// Copyright 2014 Google Inc. All rights reserved. +// Copyright 2015 Google Inc. All rights reserved. // http://code.google.com/p/protobuf/ // // Redistribution and use in source and binary forms, with or without @@ -31,16 +31,27 @@ package com.google.protobuf.nano.android; import android.os.Parcel; +import android.os.Parcelable; import android.util.Log; import com.google.protobuf.nano.InvalidProtocolBufferNanoException; import com.google.protobuf.nano.MessageNano; -final class ParcelingUtil { - private static final String TAG = "ParcelingUtil"; +import java.lang.reflect.Array; + +public final class ParcelableMessageNanoCreator<T extends MessageNano> + implements Parcelable.Creator<T> { + private static final String TAG = "PMNCreator"; + + private final Class<T> mClazz; + + public ParcelableMessageNanoCreator(Class<T> clazz) { + mClazz = clazz; + } @SuppressWarnings("unchecked") - static <T extends MessageNano> T createFromParcel(Parcel in) { + @Override + public T createFromParcel(Parcel in) { String className = in.readString(); byte[] data = in.createByteArray(); @@ -64,6 +75,12 @@ final class ParcelingUtil { return proto; } + @SuppressWarnings("unchecked") + @Override + public T[] newArray(int i) { + return (T[]) Array.newInstance(mClazz, i); + } + static <T extends MessageNano> void writeToParcel(Class<T> clazz, MessageNano message, Parcel out) { out.writeString(clazz.getName()); diff --git a/src/google/protobuf/compiler/javanano/javanano_message.cc b/src/google/protobuf/compiler/javanano/javanano_message.cc index dcfb870..dd6ef91 100644 --- a/src/google/protobuf/compiler/javanano/javanano_message.cc +++ b/src/google/protobuf/compiler/javanano/javanano_message.cc @@ -154,6 +154,17 @@ void MessageGenerator::Generate(io::Printer* printer) { } printer->Indent(); + if (params_.parcelable_messages()) { + printer->Print( + "\n" + "// Used by Parcelable\n" + "@SuppressWarnings({\"unused\"})\n" + "public static final android.os.Parcelable.Creator<$classname$> CREATOR =\n" + " new com.google.protobuf.nano.android.ParcelableMessageNanoCreator<\n" + " $classname$>($classname$.class);\n", + "classname", descriptor_->name()); + } + // Nested types and extensions for (int i = 0; i < descriptor_->extension_count(); i++) { ExtensionGenerator(descriptor_->extension(i), params_).Generate(printer); |