From 22e1cfd84ac2e7a6e0ee1101d75673c13a801c03 Mon Sep 17 00:00:00 2001 From: Jisi Liu Date: Wed, 13 Dec 2017 14:09:50 -0800 Subject: [PATCH 1/2] Add deprecation annotation for oneof case. --- .../google/protobuf/DeprecatedFieldTest.java | 20 +++++++++++++------ .../protobuf/compiler/java/java_message.cc | 9 ++++----- src/google/protobuf/unittest.proto | 3 +++ 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/java/core/src/test/java/com/google/protobuf/DeprecatedFieldTest.java b/java/core/src/test/java/com/google/protobuf/DeprecatedFieldTest.java index 2c272a7347..9c0997c49b 100644 --- a/java/core/src/test/java/com/google/protobuf/DeprecatedFieldTest.java +++ b/java/core/src/test/java/com/google/protobuf/DeprecatedFieldTest.java @@ -37,22 +37,22 @@ import junit.framework.TestCase; /** * Test field deprecation - * + * * @author birdo@google.com (Roberto Scaramuzzi) */ public class DeprecatedFieldTest extends TestCase { private String[] deprecatedGetterNames = { "hasDeprecatedInt32", "getDeprecatedInt32"}; - + private String[] deprecatedBuilderGetterNames = { "hasDeprecatedInt32", "getDeprecatedInt32", "clearDeprecatedInt32"}; - + private String[] deprecatedBuilderSetterNames = { - "setDeprecatedInt32"}; - + "setDeprecatedInt32"}; + public void testDeprecatedField() throws Exception { Class deprecatedFields = TestDeprecatedFields.class; Class deprecatedFieldsBuilder = TestDeprecatedFields.Builder.class; @@ -72,7 +72,15 @@ public class DeprecatedFieldTest extends TestCase { isDeprecated(method)); } } - + + public void testDeprecatedFieldInOneof() throws Exception { + Class oneofCase = TestDeprecatedFields.OneofFieldsCase.class; + String name = "DEPRECATED_INT32_IN_ONEOF"; + java.lang.reflect.Field enumValue = oneofCase.getField(name); + assertTrue("Enum value " + name + " should be deprecated.", + isDeprecated(enumValue)); + } + private boolean isDeprecated(AnnotatedElement annotated) { return annotated.isAnnotationPresent(Deprecated.class); } diff --git a/src/google/protobuf/compiler/java/java_message.cc b/src/google/protobuf/compiler/java/java_message.cc index f2ff2800d4..eca7cf9ae6 100644 --- a/src/google/protobuf/compiler/java/java_message.cc +++ b/src/google/protobuf/compiler/java/java_message.cc @@ -437,11 +437,10 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) { const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j); printer->Print( - "$field_name$($field_number$),\n", - "field_name", - ToUpper(field->name()), - "field_number", - SimpleItoa(field->number())); + "$deprecation$$field_name$($field_number$),\n", + "deprecation", field->options().deprecated() ? "@java.lang.Deprecated " : "", + "field_name", ToUpper(field->name()), + "field_number", SimpleItoa(field->number())); } printer->Print( "$cap_oneof_name$_NOT_SET(0);\n", diff --git a/src/google/protobuf/unittest.proto b/src/google/protobuf/unittest.proto index 45a0edad9d..2a288daa52 100644 --- a/src/google/protobuf/unittest.proto +++ b/src/google/protobuf/unittest.proto @@ -189,6 +189,9 @@ message NestedTestAllTypes { message TestDeprecatedFields { optional int32 deprecated_int32 = 1 [deprecated=true]; + oneof oneof_fields { + int32 deprecated_int32_in_oneof = 2 [deprecated=true]; + } } message TestDeprecatedMessage { From 594ec2262c30cb1bd19fc43c2799cfeca31603ac Mon Sep 17 00:00:00 2001 From: Jisi Liu Date: Thu, 14 Dec 2017 11:16:34 -0800 Subject: [PATCH 2/2] Fix python descriptor test. --- python/google/protobuf/internal/descriptor_test.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/python/google/protobuf/internal/descriptor_test.py b/python/google/protobuf/internal/descriptor_test.py index f1d5934ede..02a43d15a9 100755 --- a/python/google/protobuf/internal/descriptor_test.py +++ b/python/google/protobuf/internal/descriptor_test.py @@ -742,6 +742,19 @@ class DescriptorCopyToProtoTest(unittest.TestCase): deprecated: true > > + field { + name: "deprecated_int32_in_oneof" + number: 2 + label: LABEL_OPTIONAL + type: TYPE_INT32 + options { + deprecated: true + } + oneof_index: 0 + } + oneof_decl { + name: "oneof_fields" + } """ self._InternalTestCopyToProto(