From 721ea491a8e3e9ea5a130965dc5761fc335c3e61 Mon Sep 17 00:00:00 2001 From: Jeff Davidson Date: Tue, 22 Apr 2014 23:25:53 -0700 Subject: Support generation of Parcelable nano messages. This CL adds the "parcelable_messages" option. When enabled, all generated message classes will conform to the Android Parcelable contract. This is achieved by introducing a new parent class for generated classes which implements the required functionality. Since the store_unknown_fields option also makes use of a superclass, ExtendableMessageNano, we have two versions of the new Parcelable superclass: one extending MessageNano, and one extending ExtendableMessageNano. These classes are otherwise identical. As these classes depend on Android framework jars, they are not included in the host .jar build of the nanoproto library. Finally, add a test suite for running tests of Android-specific functionality, as this cannot be done on a desktop JVM. Change-Id: Icc2a257f03317e947f7078dbb9857c3286857497 --- java/README.txt | 11 ++- .../android/ParcelableExtendableMessageNano.java | 69 +++++++++++++++++ .../nano/android/ParcelableMessageNano.java | 67 +++++++++++++++++ .../protobuf/nano/android/ParcelingUtil.java | 72 ++++++++++++++++++ java/src/device/test/AndroidManifest.xml | 50 +++++++++++++ .../com/google/protobuf/nano/NanoAndroidTest.java | 87 ++++++++++++++++++++++ 6 files changed, 355 insertions(+), 1 deletion(-) create mode 100644 java/src/device/main/java/com/google/protobuf/nano/android/ParcelableExtendableMessageNano.java create mode 100644 java/src/device/main/java/com/google/protobuf/nano/android/ParcelableMessageNano.java create mode 100644 java/src/device/main/java/com/google/protobuf/nano/android/ParcelingUtil.java create mode 100644 java/src/device/test/AndroidManifest.xml create mode 100644 java/src/device/test/java/com/google/protobuf/nano/NanoAndroidTest.java (limited to 'java') diff --git a/java/README.txt b/java/README.txt index f922c4b..c693313 100644 --- a/java/README.txt +++ b/java/README.txt @@ -476,6 +476,7 @@ java_nano_generate_has -> true or false [DEPRECATED] optional_field_style -> default or accessors enum_style -> c or java ignore_services -> true or false +parcelable_messages -> true or false java_package: java_outer_classname: @@ -588,6 +589,9 @@ ignore_services={true,false} (default: false) it will generate a compilation error. If this flag is set to true, services will be silently ignored, instead. +parcelable_messages={true,false} (default: false) + Android-specific option to generate Parcelable messages. + To use nano protobufs within the Android repo: @@ -638,8 +642,13 @@ Please run the following steps to test: - cd ../../.. - . build/envsetup.sh - lunch 1 -- "make -j12 aprotoc libprotobuf-java-2.3.0-nano aprotoc-test-nano-params" and +- "make -j12 aprotoc libprotobuf-java-2.3.0-nano aprotoc-test-nano-params NanoAndroidTest" and check for build errors. +- Plug in an Android device or start an emulator. +- adb install -r out/target/product/generic/data/app/NanoAndroidTest.apk +- Run: + "adb shell am instrument -w com.google.protobuf.nano.test/android.test.InstrumentationTestRunner" + and verify all tests pass. - repo sync -c -j256 - "make -j12" and check for build errors 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 new file mode 100644 index 0000000..f3b82ed --- /dev/null +++ b/java/src/device/main/java/com/google/protobuf/nano/android/ParcelableExtendableMessageNano.java @@ -0,0 +1,69 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2014 Google Inc. All rights reserved. +// http://code.google.com/p/protobuf/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf.nano.android; + +import android.os.Parcel; +import android.os.Parcelable; + +import com.google.protobuf.nano.ExtendableMessageNano; + +/** + * Base class for Parcelable Protocol Buffer messages which also need to store unknown + * fields, such as extensions. + */ +public abstract class ParcelableExtendableMessageNano> + extends ExtendableMessageNano implements Parcelable { + + // Used by Parcelable + @SuppressWarnings({"unused"}) + public static final Creator> CREATOR = + new Creator>() { + @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; + } + + @Override + public void writeToParcel(Parcel out, int flags) { + ParcelingUtil.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 new file mode 100644 index 0000000..b07f1d6 --- /dev/null +++ b/java/src/device/main/java/com/google/protobuf/nano/android/ParcelableMessageNano.java @@ -0,0 +1,67 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2014 Google Inc. All rights reserved. +// http://code.google.com/p/protobuf/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf.nano.android; + +import android.os.Parcel; +import android.os.Parcelable; + +import com.google.protobuf.nano.MessageNano; + +/** + * Base class for Parcelable Protocol Buffer messages. + */ +public abstract class ParcelableMessageNano extends MessageNano implements Parcelable { + + // Used by Parcelable + @SuppressWarnings("unused") + public static final Creator CREATOR = + new Creator() { + @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; + } + + @Override + public void writeToParcel(Parcel out, int flags) { + ParcelingUtil.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/ParcelingUtil.java new file mode 100644 index 0000000..1eb84ee --- /dev/null +++ b/java/src/device/main/java/com/google/protobuf/nano/android/ParcelingUtil.java @@ -0,0 +1,72 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2014 Google Inc. All rights reserved. +// http://code.google.com/p/protobuf/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf.nano.android; + +import android.os.Parcel; +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"; + + @SuppressWarnings("unchecked") + static T createFromParcel(Parcel in) { + String className = in.readString(); + byte[] data = in.createByteArray(); + + T proto = null; + + try { + Class clazz = Class.forName(className); + Object instance = clazz.newInstance(); + proto = (T) instance; + MessageNano.mergeFrom(proto, data); + } catch (ClassNotFoundException e) { + Log.e(TAG, "Exception trying to create proto from parcel", e); + } catch (IllegalAccessException e) { + Log.e(TAG, "Exception trying to create proto from parcel", e); + } catch (InstantiationException e) { + Log.e(TAG, "Exception trying to create proto from parcel", e); + } catch (InvalidProtocolBufferNanoException e) { + Log.e(TAG, "Exception trying to create proto from parcel", e); + } + + return proto; + } + + static void writeToParcel(Class clazz, MessageNano message, + Parcel out) { + out.writeString(clazz.getName()); + out.writeByteArray(MessageNano.toByteArray(message)); + } +} diff --git a/java/src/device/test/AndroidManifest.xml b/java/src/device/test/AndroidManifest.xml new file mode 100644 index 0000000..cc54e57 --- /dev/null +++ b/java/src/device/test/AndroidManifest.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + diff --git a/java/src/device/test/java/com/google/protobuf/nano/NanoAndroidTest.java b/java/src/device/test/java/com/google/protobuf/nano/NanoAndroidTest.java new file mode 100644 index 0000000..7092485 --- /dev/null +++ b/java/src/device/test/java/com/google/protobuf/nano/NanoAndroidTest.java @@ -0,0 +1,87 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2014 Google Inc. All rights reserved. +// http://code.google.com/p/protobuf/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package com.google.protobuf.nano; + +import android.os.Parcel; + +import com.google.protobuf.nano.Extensions.ContainerMessage; +import com.google.protobuf.nano.Extensions.ExtendableMessage; +import com.google.protobuf.nano.UnittestSimpleNano.SimpleMessageNano; +import com.google.protobuf.nano.UnittestSimpleNano.SimpleMessageNano.NestedMessage; + +import junit.framework.TestCase; + +public class NanoAndroidTest extends TestCase { + public void testParceling() { + SimpleMessageNano message = new SimpleMessageNano(); + message.d = 54321; + message.nestedMsg = new NestedMessage(); + message.nestedMsg.bb = 12345; + message.defaultNestedEnum = SimpleMessageNano.FOO; + + Parcel parcel = null; + try { + parcel = Parcel.obtain(); + parcel.writeParcelable(message, 0); + parcel.setDataPosition(0); + message = parcel.readParcelable(getClass().getClassLoader()); + } finally { + if (parcel != null) { + parcel.recycle(); + } + } + + assertEquals(54321, message.d); + assertEquals(12345, message.nestedMsg.bb); + assertEquals(SimpleMessageNano.FOO, message.defaultNestedEnum); + } + + public void testExtendableParceling() { + ExtendableMessage message = new ExtendableMessage(); + message.field = 12345; + message.setExtension(ContainerMessage.anotherThing, true); + + Parcel parcel = null; + try { + parcel = Parcel.obtain(); + parcel.writeParcelable(message, 0); + parcel.setDataPosition(0); + message = parcel.readParcelable(getClass().getClassLoader()); + } finally { + if (parcel != null) { + parcel.recycle(); + } + } + + assertEquals(12345, message.field); + assertTrue((boolean) message.getExtension(ContainerMessage.anotherThing)); + } +} -- cgit v1.1