diff options
author | Jeff Davidson <jpd@google.com> | 2015-04-02 14:46:35 -0700 |
---|---|---|
committer | Jeff Davidson <jpd@google.com> | 2015-04-03 10:04:41 -0700 |
commit | bae6b59812ac972c9792605d521d6ada069288b2 (patch) | |
tree | f8b252def86c4bd71be3454b2542f72b9cd2a416 | |
parent | 00c7607ce445af504021447068d6237ba8100716 (diff) | |
download | external_protobuf-bae6b59812ac972c9792605d521d6ada069288b2.zip external_protobuf-bae6b59812ac972c9792605d521d6ada069288b2.tar.gz external_protobuf-bae6b59812ac972c9792605d521d6ada069288b2.tar.bz2 |
Generate a CREATOR for each Parcelable message.
This is less ideal from a dex count perspective because it requires a
new variable for each message, and because most apps have proguard
rules that will ensure that CREATOR classes are retained.
However, it is required to be able to use nano protos inside of AIDL
files, as the autogenerated AIDL code fails to compile otherwise. This
is a substantial benefit as it allows for backwards-compatible
parameters and return types in AIDL methods along the lines of
safeparcel.
Bug: 19084705
Change-Id: I66a2c0424b96cf8ff6b631b186cc4f9407dfc1f4
-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 4026031..758c9e8 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); |