Pull new version of protobuf sources.
[chromium-blink-merge.git] / third_party / protobuf / csharp / src / Google.Protobuf / Reflection / OneofDescriptor.cs
blobd51ee5261d3cece6de9fde4ab19049671fa997fa
1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2015 Google Inc. All rights reserved.
4 // https://developers.google.com/protocol-buffers/
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
15 // distribution.
16 // * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #endregion
33 using System.Collections.Generic;
34 using System.Collections.ObjectModel;
35 using Google.Protobuf.Compatibility;
37 namespace Google.Protobuf.Reflection
39 /// <summary>
40 /// Describes a "oneof" field collection in a message type: a set of
41 /// fields of which at most one can be set in any particular message.
42 /// </summary>
43 public sealed class OneofDescriptor : DescriptorBase
45 private readonly OneofDescriptorProto proto;
46 private MessageDescriptor containingType;
47 private IList<FieldDescriptor> fields;
48 private readonly OneofAccessor accessor;
50 internal OneofDescriptor(OneofDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index, string clrName)
51 : base(file, file.ComputeFullName(parent, proto.Name), index)
53 this.proto = proto;
54 containingType = parent;
56 file.DescriptorPool.AddSymbol(this);
57 accessor = CreateAccessor(clrName);
60 /// <summary>
61 /// The brief name of the descriptor's target.
62 /// </summary>
63 public override string Name { get { return proto.Name; } }
65 /// <summary>
66 /// Gets the message type containing this oneof.
67 /// </summary>
68 /// <value>
69 /// The message type containing this oneof.
70 /// </value>
71 public MessageDescriptor ContainingType
73 get { return containingType; }
76 /// <summary>
77 /// Gets the fields within this oneof, in declaration order.
78 /// </summary>
79 /// <value>
80 /// The fields within this oneof, in declaration order.
81 /// </value>
82 public IList<FieldDescriptor> Fields { get { return fields; } }
84 /// <summary>
85 /// Gets an accessor for reflective access to the values associated with the oneof
86 /// in a particular message.
87 /// </summary>
88 /// <value>
89 /// The accessor used for reflective access, or <c>null</c> if reflection is not
90 /// supported by this descriptor.
91 /// </value>
92 public OneofAccessor Accessor { get { return accessor; } }
94 internal void CrossLink()
96 List<FieldDescriptor> fieldCollection = new List<FieldDescriptor>();
97 foreach (var field in ContainingType.Fields.InDeclarationOrder())
99 if (field.ContainingOneof == this)
101 fieldCollection.Add(field);
104 fields = new ReadOnlyCollection<FieldDescriptor>(fieldCollection);
107 private OneofAccessor CreateAccessor(string clrName)
109 if (containingType.GeneratedType == null || clrName == null)
111 return null;
113 var caseProperty = containingType.GeneratedType.GetProperty(clrName + "Case");
114 if (caseProperty == null)
116 throw new DescriptorValidationException(this, "Property " + clrName + "Case not found in " + containingType.GeneratedType);
118 var clearMethod = containingType.GeneratedType.GetMethod("Clear" + clrName);
119 if (clearMethod == null)
121 throw new DescriptorValidationException(this, "Method Clear" + clrName + " not found in " + containingType.GeneratedType);
124 return new OneofAccessor(caseProperty, clearMethod, this);