Browse Source

Add deprecation annotation for oneof case.

Jisi Liu 7 years ago
parent
commit
22e1cfd84a

+ 14 - 6
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);
   }

+ 4 - 5
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",

+ 3 - 0
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 {