diff options
author | Jeff Davidson <jpd@google.com> | 2014-04-25 22:11:03 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2014-04-25 22:11:04 +0000 |
commit | 33a6680ab98977cd5b9b7b03f4875b9265cfec3b (patch) | |
tree | 195940ae75bbcb5b64f644c5ecfc4074eef34486 /java | |
parent | 829f6c014ce69d04593d30652c8acc2f7a793851 (diff) | |
parent | 721ea491a8e3e9ea5a130965dc5761fc335c3e61 (diff) | |
download | external_protobuf-33a6680ab98977cd5b9b7b03f4875b9265cfec3b.zip external_protobuf-33a6680ab98977cd5b9b7b03f4875b9265cfec3b.tar.gz external_protobuf-33a6680ab98977cd5b9b7b03f4875b9265cfec3b.tar.bz2 |
Merge "Support generation of Parcelable nano messages."
Diffstat (limited to 'java')
6 files changed, 355 insertions, 1 deletions
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<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; + } + + @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<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; + } + + @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 extends MessageNano> 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 <T extends MessageNano> void writeToParcel(Class<T> 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 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. +--> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.google.protobuf.nano.test" + android:versionCode="1" + android:versionName="1.0"> + + <uses-sdk + android:minSdkVersion="8" + android:targetSdkVersion="8" /> + + <instrumentation + android:name="android.test.InstrumentationTestRunner" + android:targetPackage="com.google.protobuf.nano.test" /> + + <application> + <uses-library android:name="android.test.runner" /> + </application> + +</manifest> 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)); + } +} |