소스 검색

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
Jeff Davidson 11 년 전
부모
커밋
ec4b1ce0b6

+ 61 - 0
Android.mk

@@ -142,6 +142,7 @@ LOCAL_MODULE_TAGS := optional
 LOCAL_SDK_VERSION := 8
 LOCAL_SDK_VERSION := 8
 
 
 LOCAL_SRC_FILES := $(call all-java-files-under, java/src/main/java/com/google/protobuf/nano)
 LOCAL_SRC_FILES := $(call all-java-files-under, java/src/main/java/com/google/protobuf/nano)
+LOCAL_SRC_FILES += $(call all-java-files-under, java/src/device/main/java/com/google/protobuf/nano)
 
 
 include $(BUILD_STATIC_JAVA_LIBRARY)
 include $(BUILD_STATIC_JAVA_LIBRARY)
 
 
@@ -379,3 +380,63 @@ LOCAL_PROTO_JAVA_OUTPUT_PARAMS := \
         java_outer_classname = $(LOCAL_PATH)/src/google/protobuf/unittest_import_nano.proto|UnittestImportNano
         java_outer_classname = $(LOCAL_PATH)/src/google/protobuf/unittest_import_nano.proto|UnittestImportNano
 
 
 include $(BUILD_STATIC_JAVA_LIBRARY)
 include $(BUILD_STATIC_JAVA_LIBRARY)
+
+# To test Android-specific nanoproto features.
+# =======================================================
+include $(CLEAR_VARS)
+
+# Parcelable messages
+LOCAL_MODULE := android-nano-test-parcelable
+LOCAL_MODULE_TAGS := tests
+LOCAL_SDK_VERSION := current
+
+LOCAL_PROTOC_OPTIMIZE_TYPE := nano
+
+LOCAL_SRC_FILES := src/google/protobuf/unittest_simple_nano.proto
+
+LOCAL_PROTOC_FLAGS := --proto_path=$(LOCAL_PATH)/src
+
+LOCAL_PROTO_JAVA_OUTPUT_PARAMS := \
+        parcelable_messages = true
+
+include $(BUILD_STATIC_JAVA_LIBRARY)
+
+include $(CLEAR_VARS)
+
+# Parcelable and extendable messages
+LOCAL_MODULE := android-nano-test-parcelable-extendable
+LOCAL_MODULE_TAGS := tests
+LOCAL_SDK_VERSION := current
+
+LOCAL_PROTOC_OPTIMIZE_TYPE := nano
+
+LOCAL_SRC_FILES := src/google/protobuf/unittest_extension_nano.proto
+
+LOCAL_PROTOC_FLAGS := --proto_path=$(LOCAL_PATH)/src
+
+LOCAL_PROTO_JAVA_OUTPUT_PARAMS := \
+        parcelable_messages = true, \
+        store_unknown_fields = true
+
+include $(BUILD_STATIC_JAVA_LIBRARY)
+
+include $(CLEAR_VARS)
+
+# Test APK
+LOCAL_PACKAGE_NAME := NanoAndroidTest
+
+LOCAL_SDK_VERSION := 8
+
+LOCAL_MODULE_TAGS := tests
+
+LOCAL_SRC_FILES := $(call all-java-files-under, java/src/device/test/java/com/google/protobuf/nano)
+
+LOCAL_MANIFEST_FILE := java/src/device/test/AndroidManifest.xml
+
+LOCAL_STATIC_JAVA_LIBRARIES := libprotobuf-java-2.3.0-nano \
+        android-nano-test-parcelable \
+        android-nano-test-parcelable-extendable
+
+WITH_DEXPREOPT := false
+
+include $(BUILD_PACKAGE)

+ 10 - 1
java/README.txt

@@ -476,6 +476,7 @@ java_nano_generate_has -> true or false [DEPRECATED]
 optional_field_style   -> default or accessors
 optional_field_style   -> default or accessors
 enum_style             -> c or java
 enum_style             -> c or java
 ignore_services        -> true or false
 ignore_services        -> true or false
+parcelable_messages    -> true or false
 
 
 java_package:
 java_package:
 java_outer_classname:
 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,
   it will generate a compilation error. If this flag is set to true,
   services will be silently ignored, instead.
   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:
 To use nano protobufs within the Android repo:
 
 
@@ -638,8 +642,13 @@ Please run the following steps to test:
 - cd ../../..
 - cd ../../..
 - . build/envsetup.sh
 - . build/envsetup.sh
 - lunch 1
 - 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.
   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
 - repo sync -c -j256
 - "make -j12" and check for build errors
 - "make -j12" and check for build errors
 
 

+ 2 - 0
src/google/protobuf/compiler/javanano/javanano_generator.cc

@@ -144,6 +144,8 @@ bool JavaNanoGenerator::Generate(const FileDescriptor* file,
       params.set_generate_equals(option_value == "true");
       params.set_generate_equals(option_value == "true");
     } else if (option_name == "ignore_services") {
     } else if (option_name == "ignore_services") {
       params.set_ignore_services(option_value == "true");
       params.set_ignore_services(option_value == "true");
+    } else if (option_name == "parcelable_messages") {
+      params.set_parcelable_messages(option_value == "true");
     } else {
     } else {
       *error = "Ignore unknown javanano generator option: " + option_name;
       *error = "Ignore unknown javanano generator option: " + option_name;
     }
     }

+ 8 - 1
src/google/protobuf/compiler/javanano/javanano_message.cc

@@ -132,10 +132,17 @@ void MessageGenerator::Generate(io::Printer* printer) {
       "public static final class $classname$ extends\n",
       "public static final class $classname$ extends\n",
       "classname", descriptor_->name());
       "classname", descriptor_->name());
   }
   }
-  if (params_.store_unknown_fields()) {
+  if (params_.store_unknown_fields() && params_.parcelable_messages()) {
+    printer->Print(
+      "    com.google.protobuf.nano.android.ParcelableExtendableMessageNano<$classname$> {\n",
+      "classname", descriptor_->name());
+  } else if (params_.store_unknown_fields()) {
     printer->Print(
     printer->Print(
       "    com.google.protobuf.nano.ExtendableMessageNano<$classname$> {\n",
       "    com.google.protobuf.nano.ExtendableMessageNano<$classname$> {\n",
       "classname", descriptor_->name());
       "classname", descriptor_->name());
+  } else if (params_.parcelable_messages()) {
+    printer->Print(
+      "    com.google.protobuf.nano.android.ParcelableMessageNano {\n");
   } else {
   } else {
     printer->Print(
     printer->Print(
       "    com.google.protobuf.nano.MessageNano {\n");
       "    com.google.protobuf.nano.MessageNano {\n");

+ 10 - 1
src/google/protobuf/compiler/javanano/javanano_params.h

@@ -63,6 +63,7 @@ class Params {
   bool use_reference_types_for_primitives_;
   bool use_reference_types_for_primitives_;
   bool generate_equals_;
   bool generate_equals_;
   bool ignore_services_;
   bool ignore_services_;
+  bool parcelable_messages_;
 
 
  public:
  public:
   Params(const string & base_name) :
   Params(const string & base_name) :
@@ -75,7 +76,8 @@ class Params {
     optional_field_accessors_(false),
     optional_field_accessors_(false),
     use_reference_types_for_primitives_(false),
     use_reference_types_for_primitives_(false),
     generate_equals_(false),
     generate_equals_(false),
-    ignore_services_(false) {
+    ignore_services_(false),
+    parcelable_messages_(false) {
   }
   }
 
 
   const string& base_name() const {
   const string& base_name() const {
@@ -204,6 +206,13 @@ class Params {
   bool ignore_services() const {
   bool ignore_services() const {
     return ignore_services_;
     return ignore_services_;
   }
   }
+
+  void set_parcelable_messages(bool value) {
+    parcelable_messages_ = value;
+  }
+  bool parcelable_messages() const {
+    return parcelable_messages_;
+  }
 };
 };
 
 
 }  // namespace javanano
 }  // namespace javanano