From 59eeebee870332ea2b9085688ba524c69311f662 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Fri, 17 Jul 2015 08:26:04 +0100 Subject: First pass at the big rename from ProtocolBuffers to Google.Protobuf. We'll see what I've missed when CI fails... --- csharp/src/Google.Protobuf/Google.Protobuf.csproj | 122 ++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 csharp/src/Google.Protobuf/Google.Protobuf.csproj (limited to 'csharp/src/Google.Protobuf/Google.Protobuf.csproj') diff --git a/csharp/src/Google.Protobuf/Google.Protobuf.csproj b/csharp/src/Google.Protobuf/Google.Protobuf.csproj new file mode 100644 index 00000000..431668fe --- /dev/null +++ b/csharp/src/Google.Protobuf/Google.Protobuf.csproj @@ -0,0 +1,122 @@ + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {6908BDCE-D925-43F3-94AC-A531E6DF2591} + Library + Properties + Google.Protobuf + Google.Protobuf + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + Profile92 + v4.0 + 512 + true + ..\..\keys\Google.Protobuf.snk + 3.5 + 10.0 + + + true + full + false + bin\Debug + obj\Debug\ + bin\Debug\Google.Protobuf.xml + 1591, 1570, 1571, 1572, 1573, 1574 + DEBUG;TRACE;$(EnvironmentFlavor);$(EnvironmentTemplate) + prompt + 4 + true + Off + + + pdbonly + true + bin\Release + obj\Release\ + $(OutputPath)\$(AssemblyName).xml + 1591, 1570, 1571, 1572, 1573, 1574 + TRACE;$(EnvironmentFlavor);$(EnvironmentTemplate) + prompt + 4 + true + Off + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3 From 53c399a1d65df65e9f83a70b55041a01cf8d7489 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Mon, 20 Jul 2015 19:24:31 +0100 Subject: Revamp to reflection. Changes in brief: 1. Descriptor is now the entry point for all reflection. 2. IReflectedMessage has gone; there's now a Descriptor property in IMessage, which is explicitly implemented (due to the static property). 3. FieldAccessorTable has gone away 4. IFieldAccessor and OneofFieldAccessor still exist; we *could* put the functionality straight into FieldDescriptor and OneofDescriptor... I'm unsure about that. 5. There's a temporary property MessageDescriptor.FieldAccessorsByFieldNumber to make the test changes small - we probably want this to go away 6. Discovery for delegates is now via attributes applied to properties and the Clear method of a oneof I'm happy with 1-3. 4 I'm unsure about - feedback welcome. 5 will go away 6 I'm unsure about, both in design and implementation. Should we have a ProtobufMessageAttribute too? Should we find all the relevant attributes in MessageDescriptor and pass them down, to avoid an O(N^2) scenario? Generated code changes coming in the next commit. --- .../Google.Protobuf.Test/GeneratedMessageTest.cs | 47 ++++++----- .../Reflection/DescriptorsTest.cs | 1 + .../WellKnownTypes/WrappersTest.cs | 6 +- csharp/src/Google.Protobuf/Collections/MapField.cs | 3 + csharp/src/Google.Protobuf/Google.Protobuf.csproj | 3 +- csharp/src/Google.Protobuf/IMessage.cs | 18 ++-- csharp/src/Google.Protobuf/JsonFormatter.cs | 26 +++--- .../Google.Protobuf/Reflection/DescriptorUtil.cs | 5 +- .../Google.Protobuf/Reflection/EnumDescriptor.cs | 10 ++- .../Reflection/FieldAccessorBase.cs | 7 +- .../Reflection/FieldAccessorTable.cs | 97 ---------------------- .../Google.Protobuf/Reflection/FieldDescriptor.cs | 28 +++++++ .../Google.Protobuf/Reflection/FileDescriptor.cs | 36 ++++++-- .../Google.Protobuf/Reflection/MapFieldAccessor.cs | 3 +- .../Reflection/MessageDescriptor.cs | 34 +++++++- .../Google.Protobuf/Reflection/OneofAccessor.cs | 10 +-- .../Google.Protobuf/Reflection/OneofDescriptor.cs | 34 ++++++++ .../Reflection/ProtobufFieldAttribute.cs | 58 +++++++++++++ .../Reflection/ProtobufOneofAttribute.cs | 52 ++++++++++++ .../Google.Protobuf/Reflection/ReflectionUtil.cs | 20 +++++ .../Reflection/RepeatedFieldAccessor.cs | 3 +- .../Reflection/SingleFieldAccessor.cs | 5 +- .../protobuf/compiler/csharp/csharp_field_base.cc | 2 +- .../protobuf/compiler/csharp/csharp_helpers.h | 6 +- .../protobuf/compiler/csharp/csharp_map_field.cc | 1 + .../protobuf/compiler/csharp/csharp_message.cc | 84 ++----------------- .../protobuf/compiler/csharp/csharp_message.h | 2 - .../compiler/csharp/csharp_message_field.cc | 2 + .../compiler/csharp/csharp_primitive_field.cc | 2 + .../compiler/csharp/csharp_repeated_enum_field.cc | 3 +- .../csharp/csharp_repeated_message_field.cc | 1 + .../csharp/csharp_repeated_primitive_field.cc | 1 + .../compiler/csharp/csharp_umbrella_class.cc | 37 ++++++--- .../compiler/csharp/csharp_umbrella_class.h | 1 + .../compiler/csharp/csharp_wrapper_field.cc | 2 + 35 files changed, 372 insertions(+), 278 deletions(-) delete mode 100644 csharp/src/Google.Protobuf/Reflection/FieldAccessorTable.cs create mode 100644 csharp/src/Google.Protobuf/Reflection/ProtobufFieldAttribute.cs create mode 100644 csharp/src/Google.Protobuf/Reflection/ProtobufOneofAttribute.cs (limited to 'csharp/src/Google.Protobuf/Google.Protobuf.csproj') diff --git a/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs b/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs index acb20b15..180976d1 100644 --- a/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs +++ b/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs @@ -604,7 +604,7 @@ namespace Google.Protobuf public void Reflection_GetValue() { var message = SampleMessages.CreateFullTestAllTypes(); - var fields = ((IReflectedMessage) message).Fields; + var fields = TestAllTypes.Descriptor.FieldAccessorsByFieldNumber; Assert.AreEqual(message.SingleBool, fields[TestAllTypes.SingleBoolFieldNumber].GetValue(message)); Assert.AreEqual(message.SingleBytes, fields[TestAllTypes.SingleBytesFieldNumber].GetValue(message)); Assert.AreEqual(message.SingleDouble, fields[TestAllTypes.SingleDoubleFieldNumber].GetValue(message)); @@ -639,7 +639,7 @@ namespace Google.Protobuf // Just a single map field, for the same reason var mapMessage = new TestMap { MapStringString = { { "key1", "value1" }, { "key2", "value2" } } }; - fields = ((IReflectedMessage) mapMessage).Fields; + fields = TestMap.Descriptor.FieldAccessorsByFieldNumber; var dictionary = (IDictionary) fields[TestMap.MapStringStringFieldNumber].GetValue(mapMessage); Assert.AreEqual(mapMessage.MapStringString, dictionary); Assert.AreEqual("value1", dictionary["key1"]); @@ -648,8 +648,8 @@ namespace Google.Protobuf [Test] public void Reflection_Clear() { - IReflectedMessage message = SampleMessages.CreateFullTestAllTypes(); - var fields = message.Fields; + var message = SampleMessages.CreateFullTestAllTypes(); + var fields = TestAllTypes.Descriptor.FieldAccessorsByFieldNumber; fields[TestAllTypes.SingleBoolFieldNumber].Clear(message); fields[TestAllTypes.SingleInt32FieldNumber].Clear(message); fields[TestAllTypes.SingleStringFieldNumber].Clear(message); @@ -673,7 +673,7 @@ namespace Google.Protobuf // Separately, maps. var mapMessage = new TestMap { MapStringString = { { "key1", "value1" }, { "key2", "value2" } } }; - fields = ((IReflectedMessage) mapMessage).Fields; + fields = TestMap.Descriptor.FieldAccessorsByFieldNumber; fields[TestMap.MapStringStringFieldNumber].Clear(mapMessage); Assert.AreEqual(0, mapMessage.MapStringString.Count); } @@ -682,8 +682,8 @@ namespace Google.Protobuf public void Reflection_SetValue_SingleFields() { // Just a sample (primitives, messages, enums, strings, byte strings) - IReflectedMessage message = SampleMessages.CreateFullTestAllTypes(); - var fields = message.Fields; + var message = SampleMessages.CreateFullTestAllTypes(); + var fields = TestAllTypes.Descriptor.FieldAccessorsByFieldNumber; fields[TestAllTypes.SingleBoolFieldNumber].SetValue(message, false); fields[TestAllTypes.SingleInt32FieldNumber].SetValue(message, 500); fields[TestAllTypes.SingleStringFieldNumber].SetValue(message, "It's a string"); @@ -709,51 +709,52 @@ namespace Google.Protobuf [Test] public void Reflection_SetValue_SingleFields_WrongType() { - IReflectedMessage message = SampleMessages.CreateFullTestAllTypes(); - var fields = message.Fields; + IMessage message = SampleMessages.CreateFullTestAllTypes(); + var fields = message.Descriptor.FieldAccessorsByFieldNumber; Assert.Throws(() => fields[TestAllTypes.SingleBoolFieldNumber].SetValue(message, "This isn't a bool")); } [Test] public void Reflection_SetValue_MapFields() { - IReflectedMessage message = new TestMap(); - var fields = message.Fields; + IMessage message = new TestMap(); + var fields = message.Descriptor.FieldAccessorsByFieldNumber; Assert.Throws(() => fields[TestMap.MapStringStringFieldNumber].SetValue(message, new Dictionary())); } [Test] public void Reflection_SetValue_RepeatedFields() { - IReflectedMessage message = SampleMessages.CreateFullTestAllTypes(); - var fields = message.Fields; + IMessage message = SampleMessages.CreateFullTestAllTypes(); + var fields = message.Descriptor.FieldAccessorsByFieldNumber; Assert.Throws(() => fields[TestAllTypes.RepeatedDoubleFieldNumber].SetValue(message, new double[10])); } [Test] public void Reflection_GetValue_IncorrectType() { - IReflectedMessage message = SampleMessages.CreateFullTestAllTypes(); - Assert.Throws(() => message.Fields[TestAllTypes.SingleBoolFieldNumber].GetValue(new TestMap())); + IMessage message = SampleMessages.CreateFullTestAllTypes(); + var fields = message.Descriptor.FieldAccessorsByFieldNumber; + Assert.Throws(() => fields[TestAllTypes.SingleBoolFieldNumber].GetValue(new TestMap())); } [Test] public void Reflection_Oneof() { var message = new TestAllTypes(); - var fields = ((IReflectedMessage) message).Fields; - Assert.AreEqual(1, fields.Oneofs.Count); - var oneof = fields.Oneofs[0]; - Assert.AreEqual("oneof_field", oneof.Descriptor.Name); - Assert.IsNull(oneof.GetCaseFieldDescriptor(message)); + var descriptor = TestAllTypes.Descriptor; + Assert.AreEqual(1, descriptor.Oneofs.Count); + var oneof = descriptor.Oneofs[0]; + Assert.AreEqual("oneof_field", oneof.Name); + Assert.IsNull(oneof.Accessor.GetCaseFieldDescriptor(message)); message.OneofString = "foo"; - Assert.AreSame(fields[TestAllTypes.OneofStringFieldNumber].Descriptor, oneof.GetCaseFieldDescriptor(message)); + Assert.AreSame(descriptor.FieldAccessorsByFieldNumber[TestAllTypes.OneofStringFieldNumber].Descriptor, oneof.Accessor.GetCaseFieldDescriptor(message)); message.OneofUint32 = 10; - Assert.AreSame(fields[TestAllTypes.OneofUint32FieldNumber].Descriptor, oneof.GetCaseFieldDescriptor(message)); + Assert.AreSame(descriptor.FieldAccessorsByFieldNumber[TestAllTypes.OneofUint32FieldNumber].Descriptor, oneof.Accessor.GetCaseFieldDescriptor(message)); - oneof.Clear(message); + oneof.Accessor.Clear(message); Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase); } } diff --git a/csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs b/csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs index 0db01a5e..0aff0a6c 100644 --- a/csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs +++ b/csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs @@ -62,6 +62,7 @@ namespace Google.Protobuf.Reflection Assert.AreEqual(UnittestImportProto3.Descriptor, file.Dependencies[0]); MessageDescriptor messageType = TestAllTypes.Descriptor; + Assert.AreSame(typeof(TestAllTypes), messageType.GeneratedType); Assert.AreEqual(messageType, file.MessageTypes[0]); Assert.AreEqual(messageType, file.FindTypeByName("TestAllTypes")); Assert.Null(file.FindTypeByName("NoSuchType")); diff --git a/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs b/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs index ad88c4eb..c617db36 100644 --- a/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs +++ b/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs @@ -192,7 +192,7 @@ namespace Google.Protobuf.WellKnownTypes Uint32Field = 3, Uint64Field = 4 }; - var fields = ((IReflectedMessage) message).Fields; + var fields = TestWellKnownTypes.Descriptor.FieldAccessorsByFieldNumber; Assert.AreEqual("x", fields[TestWellKnownTypes.StringFieldFieldNumber].GetValue(message)); Assert.AreEqual(ByteString.CopyFrom(1, 2, 3), fields[TestWellKnownTypes.BytesFieldFieldNumber].GetValue(message)); @@ -216,7 +216,7 @@ namespace Google.Protobuf.WellKnownTypes { // Just a single example... note that we can't have a null value here var message = new RepeatedWellKnownTypes { Int32Field = { 1, 2 } }; - var fields = ((IReflectedMessage) message).Fields; + var fields = RepeatedWellKnownTypes.Descriptor.FieldAccessorsByFieldNumber; var list = (IList) fields[RepeatedWellKnownTypes.Int32FieldFieldNumber].GetValue(message); CollectionAssert.AreEqual(new[] { 1, 2 }, list); } @@ -226,7 +226,7 @@ namespace Google.Protobuf.WellKnownTypes { // Just a single example... note that we can't have a null value here var message = new MapWellKnownTypes { Int32Field = { { 1, 2 }, { 3, null } } }; - var fields = ((IReflectedMessage) message).Fields; + var fields = MapWellKnownTypes.Descriptor.FieldAccessorsByFieldNumber; var dictionary = (IDictionary) fields[MapWellKnownTypes.Int32FieldFieldNumber].GetValue(message); Assert.AreEqual(2, dictionary[1]); Assert.IsNull(dictionary[3]); diff --git a/csharp/src/Google.Protobuf/Collections/MapField.cs b/csharp/src/Google.Protobuf/Collections/MapField.cs index f5e4fd3a..9ca5104d 100644 --- a/csharp/src/Google.Protobuf/Collections/MapField.cs +++ b/csharp/src/Google.Protobuf/Collections/MapField.cs @@ -30,6 +30,7 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion +using Google.Protobuf.Reflection; using System; using System.Collections; using System.Collections.Generic; @@ -559,6 +560,8 @@ namespace Google.Protobuf.Collections { return codec.keyCodec.CalculateSizeWithTag(Key) + codec.valueCodec.CalculateSizeWithTag(Value); } + + MessageDescriptor IMessage.Descriptor { get { return null; } } } } } diff --git a/csharp/src/Google.Protobuf/Google.Protobuf.csproj b/csharp/src/Google.Protobuf/Google.Protobuf.csproj index 431668fe..50756299 100644 --- a/csharp/src/Google.Protobuf/Google.Protobuf.csproj +++ b/csharp/src/Google.Protobuf/Google.Protobuf.csproj @@ -78,7 +78,6 @@ - @@ -91,6 +90,8 @@ + + diff --git a/csharp/src/Google.Protobuf/IMessage.cs b/csharp/src/Google.Protobuf/IMessage.cs index 8c4a4aaf..d179c386 100644 --- a/csharp/src/Google.Protobuf/IMessage.cs +++ b/csharp/src/Google.Protobuf/IMessage.cs @@ -39,15 +39,6 @@ namespace Google.Protobuf // TODO(jonskeet): Do we want a "weak" (non-generic) version of IReflectedMessage? // TODO(jonskeet): Split these interfaces into separate files when we're happy with them. - /// - /// Reflection support for accessing field values. - /// - public interface IReflectedMessage : IMessage - { - FieldAccessorTable Fields { get; } - // TODO(jonskeet): Descriptor? Or a single property which has "all you need for reflection"? - } - /// /// Interface for a Protocol Buffers message, supporting /// basic operations required for serialization. @@ -73,6 +64,13 @@ namespace Google.Protobuf /// The number of bytes required to write this message /// to a coded output stream. int CalculateSize(); + + /// + /// Descriptor for this message. All instances are expected to return the same descriptor, + /// and for generated types this will be an explicitly-implemented member, returning the + /// same value as the static property declared on the type. + /// + MessageDescriptor Descriptor { get; } } /// @@ -81,7 +79,7 @@ namespace Google.Protobuf /// the implementation class. /// /// The message type. - public interface IMessage : IReflectedMessage, IEquatable, IDeepCloneable, IFreezable where T : IMessage + public interface IMessage : IMessage, IEquatable, IDeepCloneable, IFreezable where T : IMessage { /// /// Merges the given message into this one. diff --git a/csharp/src/Google.Protobuf/JsonFormatter.cs b/csharp/src/Google.Protobuf/JsonFormatter.cs index a06e6545..7f13e33e 100644 --- a/csharp/src/Google.Protobuf/JsonFormatter.cs +++ b/csharp/src/Google.Protobuf/JsonFormatter.cs @@ -118,7 +118,7 @@ namespace Google.Protobuf this.settings = settings; } - public string Format(IReflectedMessage message) + public string Format(IMessage message) { ThrowHelper.ThrowIfNull(message, "message"); StringBuilder builder = new StringBuilder(); @@ -129,7 +129,7 @@ namespace Google.Protobuf return builder.ToString(); } - private void WriteMessage(StringBuilder builder, IReflectedMessage message) + private void WriteMessage(StringBuilder builder, IMessage message) { if (message == null) { @@ -137,15 +137,15 @@ namespace Google.Protobuf return; } builder.Append("{ "); - var fields = message.Fields; + var fields = message.Descriptor.Fields; bool first = true; // First non-oneof fields - foreach (var accessor in fields.Accessors) + foreach (var field in fields) { - var descriptor = accessor.Descriptor; + var accessor = field.Accessor; // Oneofs are written later // TODO: Change to write out fields in order, interleaving oneofs appropriately (as per binary format) - if (descriptor.ContainingOneof != null) + if (field.ContainingOneof != null) { continue; } @@ -156,7 +156,7 @@ namespace Google.Protobuf continue; } // Omit awkward (single) values such as unknown enum values - if (!descriptor.IsRepeated && !descriptor.IsMap && !CanWriteSingleValue(accessor.Descriptor, value)) + if (!field.IsRepeated && !field.IsMap && !CanWriteSingleValue(accessor.Descriptor, value)) { continue; } @@ -173,15 +173,15 @@ namespace Google.Protobuf } // Now oneofs - foreach (var accessor in fields.Oneofs) + foreach (var oneof in message.Descriptor.Oneofs) { + var accessor = oneof.Accessor; var fieldDescriptor = accessor.GetCaseFieldDescriptor(message); if (fieldDescriptor == null) { continue; } - var fieldAccessor = fields[fieldDescriptor.FieldNumber]; - object value = fieldAccessor.GetValue(message); + object value = fieldDescriptor.Accessor.GetValue(message); // Omit awkward (single) values such as unknown enum values if (!fieldDescriptor.IsRepeated && !fieldDescriptor.IsMap && !CanWriteSingleValue(fieldDescriptor, value)) { @@ -194,7 +194,7 @@ namespace Google.Protobuf } WriteString(builder, ToCamelCase(fieldDescriptor.Name)); builder.Append(": "); - WriteValue(builder, fieldAccessor, value); + WriteValue(builder, fieldDescriptor.Accessor, value); first = false; } builder.Append(first ? "}" : " }"); @@ -385,7 +385,7 @@ namespace Google.Protobuf } else { - WriteMessage(builder, (IReflectedMessage) value); + WriteMessage(builder, (IMessage) value); } break; default: @@ -406,7 +406,7 @@ namespace Google.Protobuf WriteSingleValue(builder, descriptor.MessageType.FindFieldByNumber(1), value); return; } - WriteMessage(builder, (IReflectedMessage) value); + WriteMessage(builder, (IMessage) value); } private void WriteList(StringBuilder builder, IFieldAccessor accessor, IList list) diff --git a/csharp/src/Google.Protobuf/Reflection/DescriptorUtil.cs b/csharp/src/Google.Protobuf/Reflection/DescriptorUtil.cs index af31dfb1..f5570fc4 100644 --- a/csharp/src/Google.Protobuf/Reflection/DescriptorUtil.cs +++ b/csharp/src/Google.Protobuf/Reflection/DescriptorUtil.cs @@ -50,9 +50,8 @@ namespace Google.Protobuf.Reflection /// Converts the given array into a read-only list, applying the specified conversion to /// each input element. /// - internal static IList ConvertAndMakeReadOnly(IList input, - IndexedConverter - converter) + internal static IList ConvertAndMakeReadOnly + (IList input, IndexedConverter converter) { TOutput[] array = new TOutput[input.Count]; for (int i = 0; i < array.Length; i++) diff --git a/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs index bf8f8c83..285f26f3 100644 --- a/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs @@ -30,6 +30,7 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion +using System; using System.Collections.Generic; namespace Google.Protobuf.Reflection @@ -42,11 +43,13 @@ namespace Google.Protobuf.Reflection private readonly EnumDescriptorProto proto; private readonly MessageDescriptor containingType; private readonly IList values; + private readonly Type generatedType; - internal EnumDescriptor(EnumDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index) + internal EnumDescriptor(EnumDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index, Type generatedType) : base(file, file.ComputeFullName(parent, proto.Name), index) { this.proto = proto; + this.generatedType = generatedType; containingType = parent; if (proto.Value.Count == 0) @@ -69,6 +72,11 @@ namespace Google.Protobuf.Reflection /// public override string Name { get { return proto.Name; } } + /// + /// The generated type for this enum, or null if the descriptor does not represent a generated type. + /// + public Type GeneratedType { get { return generatedType; } } + /// /// If this is a nested type, get the outer descriptor, otherwise null. /// diff --git a/csharp/src/Google.Protobuf/Reflection/FieldAccessorBase.cs b/csharp/src/Google.Protobuf/Reflection/FieldAccessorBase.cs index 39a63b47..0893dc3d 100644 --- a/csharp/src/Google.Protobuf/Reflection/FieldAccessorBase.cs +++ b/csharp/src/Google.Protobuf/Reflection/FieldAccessorBase.cs @@ -43,13 +43,8 @@ namespace Google.Protobuf.Reflection private readonly Func getValueDelegate; private readonly FieldDescriptor descriptor; - internal FieldAccessorBase(Type type, string propertyName, FieldDescriptor descriptor) + internal FieldAccessorBase(PropertyInfo property, FieldDescriptor descriptor) { - PropertyInfo property = type.GetProperty(propertyName); - if (property == null || !property.CanRead) - { - throw new ArgumentException("Not all required properties/methods available"); - } this.descriptor = descriptor; getValueDelegate = ReflectionUtil.CreateFuncObjectObject(property.GetGetMethod()); } diff --git a/csharp/src/Google.Protobuf/Reflection/FieldAccessorTable.cs b/csharp/src/Google.Protobuf/Reflection/FieldAccessorTable.cs deleted file mode 100644 index 24fcbc64..00000000 --- a/csharp/src/Google.Protobuf/Reflection/FieldAccessorTable.cs +++ /dev/null @@ -1,97 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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. -#endregion - -using System; -using System.Collections.ObjectModel; - -namespace Google.Protobuf.Reflection -{ - /// - /// Provides access to fields in generated messages via reflection. - /// - public sealed class FieldAccessorTable - { - private readonly ReadOnlyCollection accessors; - private readonly ReadOnlyCollection oneofs; - private readonly MessageDescriptor descriptor; - - /// - /// Constructs a FieldAccessorTable for a particular message class. - /// Only one FieldAccessorTable should be constructed per class. - /// - /// The CLR type for the message. - /// The type's descriptor - /// The Pascal-case names of all the field-based properties in the message. - public FieldAccessorTable(Type type, MessageDescriptor descriptor, string[] propertyNames, string[] oneofPropertyNames) - { - this.descriptor = descriptor; - var accessorsArray = new IFieldAccessor[descriptor.Fields.Count]; - for (int i = 0; i < accessorsArray.Length; i++) - { - var field = descriptor.Fields[i]; - var name = propertyNames[i]; - accessorsArray[i] = - field.IsMap ? new MapFieldAccessor(type, name, field) - : field.IsRepeated ? new RepeatedFieldAccessor(type, name, field) - : (IFieldAccessor) new SingleFieldAccessor(type, name, field); - } - accessors = new ReadOnlyCollection(accessorsArray); - var oneofsArray = new OneofAccessor[descriptor.Oneofs.Count]; - for (int i = 0; i < oneofsArray.Length; i++) - { - var oneof = descriptor.Oneofs[i]; - oneofsArray[i] = new OneofAccessor(type, oneofPropertyNames[i], oneof); - } - oneofs = new ReadOnlyCollection(oneofsArray); - } - - // TODO: Validate the name here... should possibly make this type a more "general reflection access" type, - // bearing in mind the oneof parts to come as well. - /// - /// Returns all of the field accessors for the message type. - /// - public ReadOnlyCollection Accessors { get { return accessors; } } - - public ReadOnlyCollection Oneofs { get { return oneofs; } } - - // TODO: Review this, as it's easy to get confused between FieldNumber and Index. - // Currently only used to get an accessor related to a oneof... maybe just make that simpler? - public IFieldAccessor this[int fieldNumber] - { - get - { - FieldDescriptor field = descriptor.FindFieldByNumber(fieldNumber); - return accessors[field.Index]; - } - } - } -} \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs index 3d9d0d75..57378e4c 100644 --- a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs @@ -31,6 +31,7 @@ #endregion using System; +using System.Linq; namespace Google.Protobuf.Reflection { @@ -45,6 +46,7 @@ namespace Google.Protobuf.Reflection private readonly MessageDescriptor containingType; private readonly OneofDescriptor containingOneof; private FieldType fieldType; + private IFieldAccessor accessor; internal FieldDescriptor(FieldDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index) @@ -82,6 +84,8 @@ namespace Google.Protobuf.Reflection public override string Name { get { return proto.Name; } } internal FieldDescriptorProto Proto { get { return proto; } } + + public IFieldAccessor Accessor { get { return accessor; } } /// /// Maps a field type as included in the .proto file to a FieldType. @@ -287,6 +291,30 @@ namespace Google.Protobuf.Reflection { throw new DescriptorValidationException(this, "MessageSet format is not supported."); } + + accessor = CreateAccessor(); + } + + private IFieldAccessor CreateAccessor() + { + // TODO: Check the performance of this with some large protos. Each message is O(N^2) in the number of fields, + // which isn't great... + if (containingType.GeneratedType == null) + { + return null; + } + var property = containingType + .GeneratedType + .GetProperties() + .FirstOrDefault(p => p.IsDefined(typeof(ProtobufFieldAttribute), false) && + p.GetCustomAttributes(typeof(ProtobufFieldAttribute), false).Cast().Single().Number == FieldNumber); + if (property == null) + { + return null; + } + return IsMap ? new MapFieldAccessor(property, this) + : IsRepeated ? new RepeatedFieldAccessor(property, this) + : (IFieldAccessor) new SingleFieldAccessor(property, this); } } } \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs index db393480..a10e617b 100644 --- a/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs @@ -62,11 +62,12 @@ namespace Google.Protobuf.Reflection get { return proto.Syntax == "proto3" ? ProtoSyntax.Proto3 : ProtoSyntax.Proto2; } } - private FileDescriptor(FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies) + private FileDescriptor(FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies, Type[] generatedTypes) { this.pool = pool; this.proto = proto; this.dependencies = new ReadOnlyCollection((FileDescriptor[]) dependencies.Clone()); + IEnumerator generatedTypeIterator = generatedTypes == null ? null : ((IEnumerable)generatedTypes).GetEnumerator(); publicDependencies = DeterminePublicDependencies(this, proto, dependencies, allowUnknownDependencies); @@ -74,15 +75,21 @@ namespace Google.Protobuf.Reflection messageTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.MessageType, (message, index) => - new MessageDescriptor(message, this, null, index)); + new MessageDescriptor(message, this, null, index, generatedTypeIterator)); enumTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.EnumType, (enumType, index) => - new EnumDescriptor(enumType, this, null, index)); + new EnumDescriptor(enumType, this, null, index, ReflectionUtil.GetNextType(generatedTypeIterator))); services = DescriptorUtil.ConvertAndMakeReadOnly(proto.Service, (service, index) => new ServiceDescriptor(service, this, index)); + + // We should now have consumed all the generated types. + if (generatedTypeIterator != null && generatedTypeIterator.MoveNext()) + { + throw new ArgumentException("More generated types left over after consuming all expected ones", "generatedTypes"); + } } /// @@ -265,7 +272,7 @@ namespace Google.Protobuf.Reflection /// If is not /// a valid descriptor. This can occur for a number of reasons, such as a field /// having an undefined type or because two messages were defined with the same name. - private static FileDescriptor BuildFrom(FileDescriptorProto proto, FileDescriptor[] dependencies, bool allowUnknownDependencies) + private static FileDescriptor BuildFrom(FileDescriptorProto proto, FileDescriptor[] dependencies, bool allowUnknownDependencies, Type[] generatedTypes) { // Building descriptors involves two steps: translating and linking. // In the translation step (implemented by FileDescriptor's @@ -282,7 +289,7 @@ namespace Google.Protobuf.Reflection } DescriptorPool pool = new DescriptorPool(dependencies); - FileDescriptor result = new FileDescriptor(proto, dependencies, pool, allowUnknownDependencies); + FileDescriptor result = new FileDescriptor(proto, dependencies, pool, allowUnknownDependencies, generatedTypes); // TODO(jonskeet): Reinstate these checks, or get rid of them entirely. They aren't in the Java code, // and fail for the CustomOptions test right now. (We get "descriptor.proto" vs "google/protobuf/descriptor.proto".) @@ -319,8 +326,23 @@ namespace Google.Protobuf.Reflection } } + /// + /// Creates an instance for generated code. + /// + /// + /// The parameter should be null for descriptors which don't correspond to + /// generated types. Otherwise, the array should be represent all the generated types in the file: messages then + /// enums. Within each message, there can be nested messages and enums, which must be specified "inline" in the array: + /// containing message, nested messages, nested enums - and of course each nested message may contain *more* nested messages, + /// etc. All messages within the descriptor should be represented, even if they do not have a generated type - any + /// type without a corresponding generated type (such as map entries) should respond to a null element. + /// For example, a file with a messages OuterMessage and InnerMessage, and enums OuterEnum and InnerEnum (where + /// InnerMessage and InnerEnum are nested within InnerMessage) would result in an array of + /// OuterMessage, InnerMessage, InnerEnum, OuterEnum. + /// public static FileDescriptor InternalBuildGeneratedFileFrom(byte[] descriptorData, - FileDescriptor[] dependencies) + FileDescriptor[] dependencies, + Type[] generatedTypes) { FileDescriptorProto proto; try @@ -336,7 +358,7 @@ namespace Google.Protobuf.Reflection { // When building descriptors for generated code, we allow unknown // dependencies by default. - return BuildFrom(proto, dependencies, true); + return BuildFrom(proto, dependencies, true, generatedTypes); } catch (DescriptorValidationException e) { diff --git a/csharp/src/Google.Protobuf/Reflection/MapFieldAccessor.cs b/csharp/src/Google.Protobuf/Reflection/MapFieldAccessor.cs index 317fbd8d..6df4c5f0 100644 --- a/csharp/src/Google.Protobuf/Reflection/MapFieldAccessor.cs +++ b/csharp/src/Google.Protobuf/Reflection/MapFieldAccessor.cs @@ -32,6 +32,7 @@ using System; using System.Collections; +using System.Reflection; namespace Google.Protobuf.Reflection { @@ -40,7 +41,7 @@ namespace Google.Protobuf.Reflection /// internal sealed class MapFieldAccessor : FieldAccessorBase { - internal MapFieldAccessor(Type type, string propertyName, FieldDescriptor descriptor) : base(type, propertyName, descriptor) + internal MapFieldAccessor(PropertyInfo property, FieldDescriptor descriptor) : base(property, descriptor) { } diff --git a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs index 1c22c460..9413cf61 100644 --- a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs @@ -30,8 +30,10 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion +using Google.Protobuf.Collections; using System; using System.Collections.Generic; +using System.Linq; namespace Google.Protobuf.Reflection { @@ -60,11 +62,15 @@ namespace Google.Protobuf.Reflection private readonly IList enumTypes; private readonly IList fields; private readonly IList oneofs; + // CLR representation of the type described by this descriptor, if any. + private readonly Type generatedType; + private IDictionary fieldAccessorsByFieldNumber; - internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex) + internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex, IEnumerator generatedTypeIterator) : base(file, file.ComputeFullName(parent, proto.Name), typeIndex) { this.proto = proto; + generatedType = ReflectionUtil.GetNextType(generatedTypeIterator); containingType = parent; oneofs = DescriptorUtil.ConvertAndMakeReadOnly(proto.OneofDecl, @@ -73,11 +79,11 @@ namespace Google.Protobuf.Reflection nestedTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.NestedType, (type, index) => - new MessageDescriptor(type, file, this, index)); + new MessageDescriptor(type, file, this, index, generatedTypeIterator)); enumTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.EnumType, (type, index) => - new EnumDescriptor(type, file, this, index)); + new EnumDescriptor(type, file, this, index, ReflectionUtil.GetNextType(generatedTypeIterator))); // TODO(jonskeet): Sort fields first? fields = DescriptorUtil.ConvertAndMakeReadOnly(proto.Field, @@ -85,6 +91,14 @@ namespace Google.Protobuf.Reflection new FieldDescriptor(field, file, this, index)); file.DescriptorPool.AddSymbol(this); } + + /// + /// Returns the total number of nested types and enums, recursively. + /// + private int CountTotalGeneratedTypes() + { + return nestedTypes.Sum(nested => nested.CountTotalGeneratedTypes()) + enumTypes.Count; + } /// /// The brief name of the descriptor's target. @@ -93,6 +107,11 @@ namespace Google.Protobuf.Reflection internal DescriptorProto Proto { get { return proto; } } + /// + /// The generated type for this message, or null if the descriptor does not represent a generated type. + /// + public Type GeneratedType { get { return generatedType; } } + /// /// Returns whether this message is one of the "well known types" which may have runtime/protoc support. /// @@ -141,6 +160,13 @@ namespace Google.Protobuf.Reflection get { return oneofs; } } + /// + /// Returns a map from field number to accessor. + /// TODO: Revisit this. It's mostly in place to make the transition from FieldAccessorTable + /// to descriptor-based reflection simple in terms of tests. Work out what we really want. + /// + public IDictionary FieldAccessorsByFieldNumber { get { return fieldAccessorsByFieldNumber; } } + /// /// Finds a field by field name. /// @@ -192,6 +218,8 @@ namespace Google.Protobuf.Reflection { oneof.CrossLink(); } + + fieldAccessorsByFieldNumber = new ReadOnlyDictionary(fields.ToDictionary(field => field.FieldNumber, field => field.Accessor)); } } } \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/Reflection/OneofAccessor.cs b/csharp/src/Google.Protobuf/Reflection/OneofAccessor.cs index 7a11d36b..20cbea92 100644 --- a/csharp/src/Google.Protobuf/Reflection/OneofAccessor.cs +++ b/csharp/src/Google.Protobuf/Reflection/OneofAccessor.cs @@ -44,18 +44,16 @@ namespace Google.Protobuf.Reflection private readonly Action clearDelegate; private OneofDescriptor descriptor; - internal OneofAccessor(Type type, string propertyName, OneofDescriptor descriptor) + internal OneofAccessor(PropertyInfo caseProperty, MethodInfo clearMethod, OneofDescriptor descriptor) { - PropertyInfo property = type.GetProperty(propertyName + "Case"); - if (property == null || !property.CanRead) + if (!caseProperty.CanRead) { - throw new ArgumentException("Not all required properties/methods available"); + throw new ArgumentException("Cannot read from property"); } this.descriptor = descriptor; - caseDelegate = ReflectionUtil.CreateFuncObjectT(property.GetGetMethod()); + caseDelegate = ReflectionUtil.CreateFuncObjectT(caseProperty.GetGetMethod()); this.descriptor = descriptor; - MethodInfo clearMethod = type.GetMethod("Clear" + propertyName); clearDelegate = ReflectionUtil.CreateActionObject(clearMethod); } diff --git a/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs index e92dc8bb..b4cc0791 100644 --- a/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs @@ -32,6 +32,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Linq; namespace Google.Protobuf.Reflection { @@ -40,6 +41,7 @@ namespace Google.Protobuf.Reflection private readonly OneofDescriptorProto proto; private MessageDescriptor containingType; private IList fields; + private OneofAccessor accessor; internal OneofDescriptor(OneofDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index) : base(file, file.ComputeFullName(parent, proto.Name), index) @@ -62,6 +64,8 @@ namespace Google.Protobuf.Reflection public IList Fields { get { return fields; } } + public OneofAccessor Accessor { get { return accessor; } } + internal void CrossLink() { List fieldCollection = new List(); @@ -73,6 +77,36 @@ namespace Google.Protobuf.Reflection } } fields = new ReadOnlyCollection(fieldCollection); + accessor = CreateAccessor(); + } + + private OneofAccessor CreateAccessor() + { + if (containingType.GeneratedType == null) + { + return null; + } + var caseProperty = containingType + .GeneratedType + .GetProperties() + .FirstOrDefault(p => p.IsDefined(typeof(ProtobufOneofAttribute), false) && + p.GetCustomAttributes(typeof(ProtobufOneofAttribute), false).Cast().Single().Name == Name); + if (caseProperty == null) + { + return null; + } + + var clearMethod = containingType + .GeneratedType + .GetMethods() + .FirstOrDefault(p => p.IsDefined(typeof(ProtobufOneofAttribute), false) && + p.GetCustomAttributes(typeof(ProtobufOneofAttribute), false).Cast().Single().Name == Name); + if (clearMethod == null) + { + return null; + } + + return new OneofAccessor(caseProperty, clearMethod, this); } } } diff --git a/csharp/src/Google.Protobuf/Reflection/ProtobufFieldAttribute.cs b/csharp/src/Google.Protobuf/Reflection/ProtobufFieldAttribute.cs new file mode 100644 index 00000000..a59caea7 --- /dev/null +++ b/csharp/src/Google.Protobuf/Reflection/ProtobufFieldAttribute.cs @@ -0,0 +1,58 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2015 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// 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. +#endregion +using System; + +namespace Google.Protobuf.Reflection +{ + /// + /// Attribute applied to a generated property corresponding to a field in a .proto file. + /// + [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] + public sealed class ProtobufFieldAttribute : Attribute + { + /// + /// The field number in the original .proto file. + /// + public int Number { get; set; } + + /// + /// The field name in the original .proto file. + /// + public string Name { get; set; } + + public ProtobufFieldAttribute(int number, string name) + { + this.Number = number; + this.Name = name; + } + } +} diff --git a/csharp/src/Google.Protobuf/Reflection/ProtobufOneofAttribute.cs b/csharp/src/Google.Protobuf/Reflection/ProtobufOneofAttribute.cs new file mode 100644 index 00000000..74ad8c53 --- /dev/null +++ b/csharp/src/Google.Protobuf/Reflection/ProtobufOneofAttribute.cs @@ -0,0 +1,52 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2015 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// 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. +#endregion +using System; + +namespace Google.Protobuf.Reflection +{ + /// + /// Attribute applied to the "case" property or "clear" method corresponding to a oneof in a .proto file. + /// + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = false)] + public sealed class ProtobufOneofAttribute : Attribute + { + /// + /// The oneof name in the original .proto file. + /// + public string Name { get; set; } + + public ProtobufOneofAttribute(string name) + { + this.Name = name; + } + } +} diff --git a/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs b/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs index d0dc3e8b..ec222dc1 100644 --- a/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs +++ b/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs @@ -31,6 +31,7 @@ #endregion using System; +using System.Collections.Generic; using System.Linq.Expressions; using System.Reflection; @@ -102,5 +103,24 @@ namespace Google.Protobuf.Reflection Expression call = Expression.Call(castTarget, method); return Expression.Lambda>(call, targetParameter).Compile(); } + + /// + /// Returns the next type from an iterator of types, unless the iterator is a null reference, + /// in which case null is returned. + /// + internal static Type GetNextType(IEnumerator generatedTypeIterator) + { + if (generatedTypeIterator == null) + { + return null; + } + if (!generatedTypeIterator.MoveNext()) + { + // This parameter name corresponds to any public method supplying the generated types to start with. + throw new ArgumentException("More generated types left over after consuming all expected ones", "generatedTypes"); + } + return generatedTypeIterator.Current; + } + } } \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs b/csharp/src/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs index 0ada7567..acb3c8d5 100644 --- a/csharp/src/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs +++ b/csharp/src/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs @@ -32,6 +32,7 @@ using System; using System.Collections; +using System.Reflection; namespace Google.Protobuf.Reflection { @@ -40,7 +41,7 @@ namespace Google.Protobuf.Reflection /// internal sealed class RepeatedFieldAccessor : FieldAccessorBase { - internal RepeatedFieldAccessor(Type type, string propertyName, FieldDescriptor descriptor) : base(type, propertyName, descriptor) + internal RepeatedFieldAccessor(PropertyInfo property, FieldDescriptor descriptor) : base(property, descriptor) { } diff --git a/csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs b/csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs index 8c24e46e..f00a51ba 100644 --- a/csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs +++ b/csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs @@ -48,11 +48,8 @@ namespace Google.Protobuf.Reflection private readonly Action setValueDelegate; private readonly Action clearDelegate; - internal SingleFieldAccessor(Type type, string propertyName, FieldDescriptor descriptor) : base(type, propertyName, descriptor) + internal SingleFieldAccessor(PropertyInfo property, FieldDescriptor descriptor) : base(property, descriptor) { - PropertyInfo property = type.GetProperty(propertyName); - // We know there *is* such a property, or the base class constructor would have thrown. We should be able to write - // to it though. if (!property.CanWrite) { throw new ArgumentException("Not all required properties/methods available"); diff --git a/src/google/protobuf/compiler/csharp/csharp_field_base.cc b/src/google/protobuf/compiler/csharp/csharp_field_base.cc index 89d4eb18..d327e267 100644 --- a/src/google/protobuf/compiler/csharp/csharp_field_base.cc +++ b/src/google/protobuf/compiler/csharp/csharp_field_base.cc @@ -74,6 +74,7 @@ void FieldGeneratorBase::SetCommonFieldVariables( (*variables)["property_name"] = property_name(); (*variables)["type_name"] = type_name(); + (*variables)["original_name"] = descriptor_->name(); (*variables)["name"] = name(); (*variables)["descriptor_name"] = descriptor_->name(); (*variables)["default_value"] = default_value(); @@ -85,7 +86,6 @@ void FieldGeneratorBase::SetCommonFieldVariables( } (*variables)["capitalized_type_name"] = capitalized_type_name(); (*variables)["number"] = number(); - (*variables)["field_ordinal"] = field_ordinal(); (*variables)["has_property_check"] = (*variables)["property_name"] + " != " + (*variables)["default_value"]; (*variables)["other_has_property_check"] = "other." + diff --git a/src/google/protobuf/compiler/csharp/csharp_helpers.h b/src/google/protobuf/compiler/csharp/csharp_helpers.h index 653ff992..7737ffe2 100644 --- a/src/google/protobuf/compiler/csharp/csharp_helpers.h +++ b/src/google/protobuf/compiler/csharp/csharp_helpers.h @@ -77,6 +77,8 @@ std::string GetFullUmbrellaClassName(const FileDescriptor* descriptor); std::string GetQualifiedUmbrellaClassName(const FileDescriptor* descriptor); +std::string GetClassName(const Descriptor* descriptor); + std::string GetClassName(const EnumDescriptor* descriptor); std::string GetFieldName(const FieldDescriptor* descriptor); @@ -119,10 +121,6 @@ inline bool IsDescriptorProto(const FileDescriptor* descriptor) { return descriptor->name() == "google/protobuf/descriptor_proto_file.proto"; } -inline bool IsMapEntry(const Descriptor* descriptor) { - return descriptor->options().map_entry(); -} - inline bool IsWrapperType(const FieldDescriptor* descriptor) { return descriptor->type() == FieldDescriptor::TYPE_MESSAGE && descriptor->message_type()->file()->name() == "google/protobuf/wrappers.proto"; diff --git a/src/google/protobuf/compiler/csharp/csharp_map_field.cc b/src/google/protobuf/compiler/csharp/csharp_map_field.cc index cba24a59..bdbfd92b 100644 --- a/src/google/protobuf/compiler/csharp/csharp_map_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_map_field.cc @@ -79,6 +79,7 @@ void MapFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, + "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ pbc::MapField<$key_type_name$, $value_type_name$> $property_name$ {\n" " get { return $name$_; }\n" "}\n"); diff --git a/src/google/protobuf/compiler/csharp/csharp_message.cc b/src/google/protobuf/compiler/csharp/csharp_message.cc index 10cf3585..42fd5065 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message.cc +++ b/src/google/protobuf/compiler/csharp/csharp_message.cc @@ -96,88 +96,11 @@ const std::vector& MessageGenerator::fields_by_number() return fields_by_number_; } -/// Get an identifier that uniquely identifies this type within the file. -/// This is used to declare static variables related to this type at the -/// outermost file scope. -std::string GetUniqueFileScopeIdentifier(const Descriptor* descriptor) { - std::string result = descriptor->full_name(); - std::replace(result.begin(), result.end(), '.', '_'); - return "static_" + result; -} - -void MessageGenerator::GenerateStaticVariables(io::Printer* printer) { - // Because descriptor.proto (Google.Protobuf.DescriptorProtos) is - // used in the construction of descriptors, we have a tricky bootstrapping - // problem. To help control static initialization order, we make sure all - // descriptors and other static data that depends on them are members of - // the proto-descriptor class. This way, they will be initialized in - // a deterministic order. - - std::string identifier = GetUniqueFileScopeIdentifier(descriptor_); - - // The descriptor for this type. - printer->Print( - "internal static pbr::FieldAccessorTable internal__$identifier$__FieldAccessorTable;\n", - "identifier", GetUniqueFileScopeIdentifier(descriptor_), - "full_class_name", full_class_name()); - - for (int i = 0; i < descriptor_->nested_type_count(); i++) { - // Don't generate accessor table fields for maps... - if (!IsMapEntryMessage(descriptor_->nested_type(i))) { - MessageGenerator messageGenerator(descriptor_->nested_type(i)); - messageGenerator.GenerateStaticVariables(printer); - } - } -} - -void MessageGenerator::GenerateStaticVariableInitializers(io::Printer* printer) { - map vars; - vars["identifier"] = GetUniqueFileScopeIdentifier(descriptor_); - vars["full_class_name"] = full_class_name(); - - // Work out how to get to the message descriptor (which may be multiply nested) from the file - // descriptor. - string descriptor_chain; - const Descriptor* current_descriptor = descriptor_; - while (current_descriptor->containing_type()) { - descriptor_chain = ".NestedTypes[" + SimpleItoa(current_descriptor->index()) + "]" + descriptor_chain; - current_descriptor = current_descriptor->containing_type(); - } - descriptor_chain = "descriptor.MessageTypes[" + SimpleItoa(current_descriptor->index()) + "]" + descriptor_chain; - vars["descriptor_chain"] = descriptor_chain; - - printer->Print( - vars, - "internal__$identifier$__FieldAccessorTable = \n" - " new pbr::FieldAccessorTable(typeof($full_class_name$), $descriptor_chain$,\n"); - printer->Print(" new string[] { "); - for (int i = 0; i < descriptor_->field_count(); i++) { - printer->Print("\"$property_name$\", ", - "property_name", GetPropertyName(descriptor_->field(i))); - } - printer->Print("}, new string[] { "); - for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { - printer->Print("\"$oneof_name$\", ", - "oneof_name", - UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), true)); - } - printer->Print("});\n"); - - // Generate static member initializers for all non-map-entry nested types. - for (int i = 0; i < descriptor_->nested_type_count(); i++) { - if (!IsMapEntryMessage(descriptor_->nested_type(i))) { - MessageGenerator messageGenerator(descriptor_->nested_type(i)); - messageGenerator.GenerateStaticVariableInitializers(printer); - } - } -} - void MessageGenerator::Generate(io::Printer* printer) { map vars; vars["class_name"] = class_name(); vars["access_level"] = class_access_level(); vars["umbrella_class_name"] = GetFullUmbrellaClassName(descriptor_->file()); - vars["identifier"] = GetUniqueFileScopeIdentifier(descriptor_); printer->Print( "[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]\n"); @@ -221,8 +144,8 @@ void MessageGenerator::Generate(io::Printer* printer) { " get { return $descriptor_accessor$; }\n" "}\n" "\n" - "pbr::FieldAccessorTable pb::IReflectedMessage.Fields {\n" - " get { return $umbrella_class_name$.internal__$identifier$__FieldAccessorTable; }\n" + "pbr::MessageDescriptor pb::IMessage.Descriptor {\n" + " get { return Descriptor; }\n" "}\n" "\n" "private bool _frozen = false;\n" @@ -258,6 +181,7 @@ void MessageGenerator::Generate(io::Printer* printer) { for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { vars["name"] = UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), false); vars["property_name"] = UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), true); + vars["original_name"] = descriptor_->oneof_decl(i)->name(); printer->Print( vars, "private object $name$_;\n" @@ -275,9 +199,11 @@ void MessageGenerator::Generate(io::Printer* printer) { printer->Print( vars, "private $property_name$OneofCase $name$Case_ = $property_name$OneofCase.None;\n" + "[pbr::ProtobufOneof(\"$original_name$\")]\n" "public $property_name$OneofCase $property_name$Case {\n" " get { return $name$Case_; }\n" "}\n\n" + "[pbr::ProtobufOneof(\"$original_name$\")]\n" "public void Clear$property_name$() {\n" " pb::Freezable.CheckMutable(this);\n" " $name$Case_ = $property_name$OneofCase.None;\n" diff --git a/src/google/protobuf/compiler/csharp/csharp_message.h b/src/google/protobuf/compiler/csharp/csharp_message.h index fbe8a3be..f0c49ac9 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message.h +++ b/src/google/protobuf/compiler/csharp/csharp_message.h @@ -53,8 +53,6 @@ class MessageGenerator : public SourceGeneratorBase { void GenerateCloningCode(io::Printer* printer); void GenerateFreezingCode(io::Printer* printer); void GenerateFrameworkMethods(io::Printer* printer); - void GenerateStaticVariables(io::Printer* printer); - void GenerateStaticVariableInitializers(io::Printer* printer); void Generate(io::Printer* printer); private: diff --git a/src/google/protobuf/compiler/csharp/csharp_message_field.cc b/src/google/protobuf/compiler/csharp/csharp_message_field.cc index d2c3a88b..c2b6ff76 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_message_field.cc @@ -64,6 +64,7 @@ void MessageFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, + "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ $type_name$ $property_name$ {\n" " get { return $name$_; }\n" " set {\n" @@ -158,6 +159,7 @@ void MessageOneofFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, + "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ $type_name$ $property_name$ {\n" " get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : null; }\n" " set {\n" diff --git a/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc b/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc index 4454ef02..2c9338bd 100644 --- a/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc @@ -71,6 +71,7 @@ void PrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, + "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ $type_name$ $property_name$ {\n" " get { return $name$_; }\n" " set {\n" @@ -174,6 +175,7 @@ void PrimitiveOneofFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, + "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ $type_name$ $property_name$ {\n" " get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : $default_value$; }\n" " set {\n" diff --git a/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc b/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc index d5fc6d98..60d06154 100644 --- a/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc @@ -59,12 +59,13 @@ void RepeatedEnumFieldGenerator::GenerateMembers(io::Printer* printer) { printer->Print( variables_, "private static readonly pb::FieldCodec<$type_name$> _repeated_$name$_codec\n" - " = pb::FieldCodec.ForEnum($tag$, x => (int) x, x => ($type_name$) x);"); + " = pb::FieldCodec.ForEnum($tag$, x => (int) x, x => ($type_name$) x);\n"); printer->Print(variables_, "private readonly pbc::RepeatedField<$type_name$> $name$_ = new pbc::RepeatedField<$type_name$>();\n"); AddDeprecatedFlag(printer); printer->Print( variables_, + "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ pbc::RepeatedField<$type_name$> $property_name$ {\n" " get { return $name$_; }\n" "}\n"); diff --git a/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc b/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc index d939fc79..921798b0 100644 --- a/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc @@ -78,6 +78,7 @@ void RepeatedMessageFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, + "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ pbc::RepeatedField<$type_name$> $property_name$ {\n" " get { return $name$_; }\n" "}\n"); diff --git a/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc b/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc index 5b5d9b3d..bcfb9936 100644 --- a/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc @@ -65,6 +65,7 @@ void RepeatedPrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, + "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ pbc::RepeatedField<$type_name$> $property_name$ {\n" " get { return $name$_; }\n" "}\n"); diff --git a/src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc b/src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc index 57271d17..3f39250e 100644 --- a/src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc +++ b/src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc @@ -63,12 +63,6 @@ UmbrellaClassGenerator::~UmbrellaClassGenerator() { void UmbrellaClassGenerator::Generate(io::Printer* printer) { WriteIntroduction(printer); - printer->Print("#region Static variables\n"); - for (int i = 0; i < file_->message_type_count(); i++) { - MessageGenerator messageGenerator(file_->message_type(i)); - messageGenerator.GenerateStaticVariables(printer); - } - printer->Print("#endregion\n"); WriteDescriptor(printer); // Close the class declaration. printer->Outdent(); @@ -183,24 +177,43 @@ void UmbrellaClassGenerator::WriteDescriptor(io::Printer* printer) { // Invoke InternalBuildGeneratedFileFrom() to build the file. printer->Print( "descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,\n"); - printer->Print(" new pbr::FileDescriptor[] {\n"); + printer->Print(" new pbr::FileDescriptor[] { "); for (int i = 0; i < file_->dependency_count(); i++) { printer->Print( - " $full_umbrella_class_name$.Descriptor, \n", + "$full_umbrella_class_name$.Descriptor, ", "full_umbrella_class_name", GetFullUmbrellaClassName(file_->dependency(i))); } - printer->Print(" });\n"); - // Then invoke any other static variable initializers, e.g. field accessors. + // Specify all the generated types (messages and enums), recursively, as an array. + printer->Print("},\n" + " new global::System.Type[] { "); for (int i = 0; i < file_->message_type_count(); i++) { - MessageGenerator messageGenerator(file_->message_type(i)); - messageGenerator.GenerateStaticVariableInitializers(printer); + WriteTypeLiterals(file_->message_type(i), printer); } + for (int i = 0; i < file_->enum_type_count(); i++) { + printer->Print("typeof($type_name$), ", "type_name", GetClassName(file_->enum_type(i))); + } + printer->Print("});\n"); + printer->Outdent(); printer->Print("}\n"); printer->Print("#endregion\n\n"); } +void UmbrellaClassGenerator::WriteTypeLiterals(const Descriptor* descriptor, io::Printer* printer) { + if (IsMapEntryMessage(descriptor)) { + printer->Print("null, "); + return; + } + printer->Print("typeof($type_name$), ", "type_name", GetClassName(descriptor)); + for (int i = 0; i < descriptor->nested_type_count(); i++) { + WriteTypeLiterals(descriptor->nested_type(i), printer); + } + for (int i = 0; i < descriptor->enum_type_count(); i++) { + printer->Print("typeof($type_name$), ", "type_name", GetClassName(descriptor->enum_type(i))); + } +} + } // namespace csharp } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/csharp/csharp_umbrella_class.h b/src/google/protobuf/compiler/csharp/csharp_umbrella_class.h index f7533d2d..db1092a9 100644 --- a/src/google/protobuf/compiler/csharp/csharp_umbrella_class.h +++ b/src/google/protobuf/compiler/csharp/csharp_umbrella_class.h @@ -57,6 +57,7 @@ class UmbrellaClassGenerator : public SourceGeneratorBase { void WriteIntroduction(io::Printer* printer); void WriteDescriptor(io::Printer* printer); + void WriteTypeLiterals(const Descriptor* descriptor, io::Printer* printer); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UmbrellaClassGenerator); }; diff --git a/src/google/protobuf/compiler/csharp/csharp_wrapper_field.cc b/src/google/protobuf/compiler/csharp/csharp_wrapper_field.cc index 75ef5e50..d6cd0a10 100644 --- a/src/google/protobuf/compiler/csharp/csharp_wrapper_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_wrapper_field.cc @@ -73,6 +73,7 @@ void WrapperFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, + "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ $type_name$ $property_name$ {\n" " get { return $name$_; }\n" " set {\n" @@ -169,6 +170,7 @@ void WrapperOneofFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, + "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ $type_name$ $property_name$ {\n" " get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : ($type_name$) null; }\n" " set {\n" -- cgit v1.2.3 From 4668c3dc3921e157b7904e26a1ddd84287b881be Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Wed, 22 Jul 2015 11:38:22 +0100 Subject: Remove the usage of attributes for field/method discovery. Instead, introduce GeneratedCodeInfo which passes in what we need, and adjust the codegen to take account of this. --- csharp/src/Google.Protobuf/Google.Protobuf.csproj | 3 +- .../Reflection/DescriptorProtoFile.cs | 171 +++------------------ .../Google.Protobuf/Reflection/FieldDescriptor.cs | 26 ++-- .../Google.Protobuf/Reflection/FileDescriptor.cs | 36 ++--- .../Reflection/GeneratedCodeInfo.cs | 66 ++++++++ .../Reflection/MessageDescriptor.cs | 42 ++--- .../Google.Protobuf/Reflection/OneofDescriptor.cs | 27 ++-- .../Reflection/ProtobufFieldAttribute.cs | 58 ------- .../Reflection/ProtobufOneofAttribute.cs | 52 ------- .../Google.Protobuf/Reflection/ReflectionUtil.cs | 21 +-- .../protobuf/compiler/csharp/csharp_field_base.cc | 5 - .../protobuf/compiler/csharp/csharp_field_base.h | 1 - .../protobuf/compiler/csharp/csharp_map_field.cc | 1 - .../protobuf/compiler/csharp/csharp_message.cc | 13 -- .../compiler/csharp/csharp_message_field.cc | 2 - .../compiler/csharp/csharp_primitive_field.cc | 2 - .../compiler/csharp/csharp_repeated_enum_field.cc | 1 - .../csharp/csharp_repeated_message_field.cc | 1 - .../csharp/csharp_repeated_primitive_field.cc | 1 - .../compiler/csharp/csharp_umbrella_class.cc | 115 +++++++++++--- .../compiler/csharp/csharp_umbrella_class.h | 2 +- .../compiler/csharp/csharp_wrapper_field.cc | 2 - 22 files changed, 243 insertions(+), 405 deletions(-) create mode 100644 csharp/src/Google.Protobuf/Reflection/GeneratedCodeInfo.cs delete mode 100644 csharp/src/Google.Protobuf/Reflection/ProtobufFieldAttribute.cs delete mode 100644 csharp/src/Google.Protobuf/Reflection/ProtobufOneofAttribute.cs (limited to 'csharp/src/Google.Protobuf/Google.Protobuf.csproj') diff --git a/csharp/src/Google.Protobuf/Google.Protobuf.csproj b/csharp/src/Google.Protobuf/Google.Protobuf.csproj index 50756299..29320ca8 100644 --- a/csharp/src/Google.Protobuf/Google.Protobuf.csproj +++ b/csharp/src/Google.Protobuf/Google.Protobuf.csproj @@ -81,6 +81,7 @@ + @@ -90,8 +91,6 @@ - - diff --git a/csharp/src/Google.Protobuf/Reflection/DescriptorProtoFile.cs b/csharp/src/Google.Protobuf/Reflection/DescriptorProtoFile.cs index 6565982c..def70b53 100644 --- a/csharp/src/Google.Protobuf/Reflection/DescriptorProtoFile.cs +++ b/csharp/src/Google.Protobuf/Reflection/DescriptorProtoFile.cs @@ -131,10 +131,30 @@ namespace Google.Protobuf.Reflection { "YWlsaW5nX2NvbW1lbnRzGAQgASgJEiEKGWxlYWRpbmdfZGV0YWNoZWRfY29t", "bWVudHMYBiADKAlCWAoTY29tLmdvb2dsZS5wcm90b2J1ZkIQRGVzY3JpcHRv", "clByb3Rvc0gBWgpkZXNjcmlwdG9yogIDR1BCqgIaR29vZ2xlLlByb3RvYnVm", - "LlJlZmxlY3Rpb24=")); + "LlJlZmxlY3Rpb24=")); descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData, new pbr::FileDescriptor[] { }, - new global::System.Type[] { typeof(global::Google.Protobuf.Reflection.FileDescriptorSet), typeof(global::Google.Protobuf.Reflection.FileDescriptorProto), typeof(global::Google.Protobuf.Reflection.DescriptorProto), typeof(global::Google.Protobuf.Reflection.DescriptorProto.Types.ExtensionRange), typeof(global::Google.Protobuf.Reflection.DescriptorProto.Types.ReservedRange), typeof(global::Google.Protobuf.Reflection.FieldDescriptorProto), typeof(global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type), typeof(global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label), typeof(global::Google.Protobuf.Reflection.OneofDescriptorProto), typeof(global::Google.Protobuf.Reflection.EnumDescriptorProto), typeof(global::Google.Protobuf.Reflection.EnumValueDescriptorProto), typeof(global::Google.Protobuf.Reflection.ServiceDescriptorProto), typeof(global::Google.Protobuf.Reflection.MethodDescriptorProto), typeof(global::Google.Protobuf.Reflection.FileOptions), typeof(global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode), typeof(global::Google.Protobuf.Reflection.MessageOptions), typeof(global::Google.Protobuf.Reflection.FieldOptions), typeof(global::Google.Protobuf.Reflection.FieldOptions.Types.CType), typeof(global::Google.Protobuf.Reflection.FieldOptions.Types.JSType), typeof(global::Google.Protobuf.Reflection.EnumOptions), typeof(global::Google.Protobuf.Reflection.EnumValueOptions), typeof(global::Google.Protobuf.Reflection.ServiceOptions), typeof(global::Google.Protobuf.Reflection.MethodOptions), typeof(global::Google.Protobuf.Reflection.UninterpretedOption), typeof(global::Google.Protobuf.Reflection.UninterpretedOption.Types.NamePart), typeof(global::Google.Protobuf.Reflection.SourceCodeInfo), typeof(global::Google.Protobuf.Reflection.SourceCodeInfo.Types.Location), }); + new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] { + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.FileDescriptorSet), new[]{ "File" }, null, null, null), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.FileDescriptorProto), new[]{ "Name", "Package", "Dependency", "PublicDependency", "WeakDependency", "MessageType", "EnumType", "Service", "Extension", "Options", "SourceCodeInfo", "Syntax" }, null, null, null), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.DescriptorProto), new[]{ "Name", "Field", "Extension", "NestedType", "EnumType", "ExtensionRange", "OneofDecl", "Options", "ReservedRange", "ReservedName" }, null, null, new pbr::GeneratedCodeInfo[] { new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.DescriptorProto.Types.ExtensionRange), new[]{ "Start", "End" }, null, null, null), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.DescriptorProto.Types.ReservedRange), new[]{ "Start", "End" }, null, null, null)}), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.FieldDescriptorProto), new[]{ "Name", "Number", "Label", "Type", "TypeName", "Extendee", "DefaultValue", "OneofIndex", "Options" }, null, new[]{ typeof(global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type), typeof(global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label) }, null), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.OneofDescriptorProto), new[]{ "Name" }, null, null, null), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.EnumDescriptorProto), new[]{ "Name", "Value", "Options" }, null, null, null), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.EnumValueDescriptorProto), new[]{ "Name", "Number", "Options" }, null, null, null), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.ServiceDescriptorProto), new[]{ "Name", "Method", "Options" }, null, null, null), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.MethodDescriptorProto), new[]{ "Name", "InputType", "OutputType", "Options", "ClientStreaming", "ServerStreaming" }, null, null, null), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.FileOptions), new[]{ "JavaPackage", "JavaOuterClassname", "JavaMultipleFiles", "JavaGenerateEqualsAndHash", "JavaStringCheckUtf8", "OptimizeFor", "GoPackage", "CcGenericServices", "JavaGenericServices", "PyGenericServices", "Deprecated", "CcEnableArenas", "ObjcClassPrefix", "CsharpNamespace", "UninterpretedOption" }, null, new[]{ typeof(global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode) }, null), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.MessageOptions), new[]{ "MessageSetWireFormat", "NoStandardDescriptorAccessor", "Deprecated", "MapEntry", "UninterpretedOption" }, null, null, null), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.FieldOptions), new[]{ "Ctype", "Packed", "Jstype", "Lazy", "Deprecated", "Weak", "UninterpretedOption" }, null, new[]{ typeof(global::Google.Protobuf.Reflection.FieldOptions.Types.CType), typeof(global::Google.Protobuf.Reflection.FieldOptions.Types.JSType) }, null), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.EnumOptions), new[]{ "AllowAlias", "Deprecated", "UninterpretedOption" }, null, null, null), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.EnumValueOptions), new[]{ "Deprecated", "UninterpretedOption" }, null, null, null), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.ServiceOptions), new[]{ "Deprecated", "UninterpretedOption" }, null, null, null), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.MethodOptions), new[]{ "Deprecated", "UninterpretedOption" }, null, null, null), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.UninterpretedOption), new[]{ "Name", "IdentifierValue", "PositiveIntValue", "NegativeIntValue", "DoubleValue", "StringValue", "AggregateValue" }, null, null, new pbr::GeneratedCodeInfo[] { new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.UninterpretedOption.Types.NamePart), new[]{ "NamePart_", "IsExtension" }, null, null, null)}), + new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.SourceCodeInfo), new[]{ "Location" }, null, null, new pbr::GeneratedCodeInfo[] { new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Reflection.SourceCodeInfo.Types.Location), new[]{ "Path", "Span", "LeadingComments", "TrailingComments", "LeadingDetachedComments" }, null, null, null)}) + })); } #endregion @@ -145,8 +165,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new FileDescriptorSet()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "file" }; - private static readonly uint[] _fieldTags = new uint[] { 10 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[0]; } } @@ -184,7 +202,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_file_codec = pb::FieldCodec.ForMessage(10, global::Google.Protobuf.Reflection.FileDescriptorProto.Parser); private readonly pbc::RepeatedField file_ = new pbc::RepeatedField(); - [pbr::ProtobufField(1, "file")] internal pbc::RepeatedField File { get { return file_; } } @@ -257,8 +274,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new FileDescriptorProto()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "dependency", "enum_type", "extension", "message_type", "name", "options", "package", "public_dependency", "service", "source_code_info", "syntax", "weak_dependency" }; - private static readonly uint[] _fieldTags = new uint[] { 26, 42, 58, 34, 10, 66, 18, 80, 50, 74, 98, 88 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[1]; } } @@ -313,7 +328,6 @@ namespace Google.Protobuf.Reflection { public const int NameFieldNumber = 1; private string name_ = ""; - [pbr::ProtobufField(1, "name")] internal string Name { get { return name_; } set { @@ -324,7 +338,6 @@ namespace Google.Protobuf.Reflection { public const int PackageFieldNumber = 2; private string package_ = ""; - [pbr::ProtobufField(2, "package")] internal string Package { get { return package_; } set { @@ -337,7 +350,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_dependency_codec = pb::FieldCodec.ForString(26); private readonly pbc::RepeatedField dependency_ = new pbc::RepeatedField(); - [pbr::ProtobufField(3, "dependency")] internal pbc::RepeatedField Dependency { get { return dependency_; } } @@ -346,7 +358,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_publicDependency_codec = pb::FieldCodec.ForInt32(80); private readonly pbc::RepeatedField publicDependency_ = new pbc::RepeatedField(); - [pbr::ProtobufField(10, "public_dependency")] internal pbc::RepeatedField PublicDependency { get { return publicDependency_; } } @@ -355,7 +366,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_weakDependency_codec = pb::FieldCodec.ForInt32(88); private readonly pbc::RepeatedField weakDependency_ = new pbc::RepeatedField(); - [pbr::ProtobufField(11, "weak_dependency")] internal pbc::RepeatedField WeakDependency { get { return weakDependency_; } } @@ -364,7 +374,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_messageType_codec = pb::FieldCodec.ForMessage(34, global::Google.Protobuf.Reflection.DescriptorProto.Parser); private readonly pbc::RepeatedField messageType_ = new pbc::RepeatedField(); - [pbr::ProtobufField(4, "message_type")] internal pbc::RepeatedField MessageType { get { return messageType_; } } @@ -373,7 +382,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_enumType_codec = pb::FieldCodec.ForMessage(42, global::Google.Protobuf.Reflection.EnumDescriptorProto.Parser); private readonly pbc::RepeatedField enumType_ = new pbc::RepeatedField(); - [pbr::ProtobufField(5, "enum_type")] internal pbc::RepeatedField EnumType { get { return enumType_; } } @@ -382,7 +390,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_service_codec = pb::FieldCodec.ForMessage(50, global::Google.Protobuf.Reflection.ServiceDescriptorProto.Parser); private readonly pbc::RepeatedField service_ = new pbc::RepeatedField(); - [pbr::ProtobufField(6, "service")] internal pbc::RepeatedField Service { get { return service_; } } @@ -391,14 +398,12 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_extension_codec = pb::FieldCodec.ForMessage(58, global::Google.Protobuf.Reflection.FieldDescriptorProto.Parser); private readonly pbc::RepeatedField extension_ = new pbc::RepeatedField(); - [pbr::ProtobufField(7, "extension")] internal pbc::RepeatedField Extension { get { return extension_; } } public const int OptionsFieldNumber = 8; private global::Google.Protobuf.Reflection.FileOptions options_; - [pbr::ProtobufField(8, "options")] internal global::Google.Protobuf.Reflection.FileOptions Options { get { return options_; } set { @@ -409,7 +414,6 @@ namespace Google.Protobuf.Reflection { public const int SourceCodeInfoFieldNumber = 9; private global::Google.Protobuf.Reflection.SourceCodeInfo sourceCodeInfo_; - [pbr::ProtobufField(9, "source_code_info")] internal global::Google.Protobuf.Reflection.SourceCodeInfo SourceCodeInfo { get { return sourceCodeInfo_; } set { @@ -420,7 +424,6 @@ namespace Google.Protobuf.Reflection { public const int SyntaxFieldNumber = 12; private string syntax_ = ""; - [pbr::ProtobufField(12, "syntax")] internal string Syntax { get { return syntax_; } set { @@ -645,8 +648,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new DescriptorProto()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "enum_type", "extension", "extension_range", "field", "name", "nested_type", "oneof_decl", "options", "reserved_name", "reserved_range" }; - private static readonly uint[] _fieldTags = new uint[] { 34, 50, 42, 18, 10, 26, 66, 58, 82, 74 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[2]; } } @@ -699,7 +700,6 @@ namespace Google.Protobuf.Reflection { public const int NameFieldNumber = 1; private string name_ = ""; - [pbr::ProtobufField(1, "name")] internal string Name { get { return name_; } set { @@ -712,7 +712,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_field_codec = pb::FieldCodec.ForMessage(18, global::Google.Protobuf.Reflection.FieldDescriptorProto.Parser); private readonly pbc::RepeatedField field_ = new pbc::RepeatedField(); - [pbr::ProtobufField(2, "field")] internal pbc::RepeatedField Field { get { return field_; } } @@ -721,7 +720,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_extension_codec = pb::FieldCodec.ForMessage(50, global::Google.Protobuf.Reflection.FieldDescriptorProto.Parser); private readonly pbc::RepeatedField extension_ = new pbc::RepeatedField(); - [pbr::ProtobufField(6, "extension")] internal pbc::RepeatedField Extension { get { return extension_; } } @@ -730,7 +728,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_nestedType_codec = pb::FieldCodec.ForMessage(26, global::Google.Protobuf.Reflection.DescriptorProto.Parser); private readonly pbc::RepeatedField nestedType_ = new pbc::RepeatedField(); - [pbr::ProtobufField(3, "nested_type")] internal pbc::RepeatedField NestedType { get { return nestedType_; } } @@ -739,7 +736,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_enumType_codec = pb::FieldCodec.ForMessage(34, global::Google.Protobuf.Reflection.EnumDescriptorProto.Parser); private readonly pbc::RepeatedField enumType_ = new pbc::RepeatedField(); - [pbr::ProtobufField(4, "enum_type")] internal pbc::RepeatedField EnumType { get { return enumType_; } } @@ -748,7 +744,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_extensionRange_codec = pb::FieldCodec.ForMessage(42, global::Google.Protobuf.Reflection.DescriptorProto.Types.ExtensionRange.Parser); private readonly pbc::RepeatedField extensionRange_ = new pbc::RepeatedField(); - [pbr::ProtobufField(5, "extension_range")] internal pbc::RepeatedField ExtensionRange { get { return extensionRange_; } } @@ -757,14 +752,12 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_oneofDecl_codec = pb::FieldCodec.ForMessage(66, global::Google.Protobuf.Reflection.OneofDescriptorProto.Parser); private readonly pbc::RepeatedField oneofDecl_ = new pbc::RepeatedField(); - [pbr::ProtobufField(8, "oneof_decl")] internal pbc::RepeatedField OneofDecl { get { return oneofDecl_; } } public const int OptionsFieldNumber = 7; private global::Google.Protobuf.Reflection.MessageOptions options_; - [pbr::ProtobufField(7, "options")] internal global::Google.Protobuf.Reflection.MessageOptions Options { get { return options_; } set { @@ -777,7 +770,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_reservedRange_codec = pb::FieldCodec.ForMessage(74, global::Google.Protobuf.Reflection.DescriptorProto.Types.ReservedRange.Parser); private readonly pbc::RepeatedField reservedRange_ = new pbc::RepeatedField(); - [pbr::ProtobufField(9, "reserved_range")] internal pbc::RepeatedField ReservedRange { get { return reservedRange_; } } @@ -786,7 +778,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_reservedName_codec = pb::FieldCodec.ForString(82); private readonly pbc::RepeatedField reservedName_ = new pbc::RepeatedField(); - [pbr::ProtobufField(10, "reserved_name")] internal pbc::RepeatedField ReservedName { get { return reservedName_; } } @@ -961,8 +952,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new ExtensionRange()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "end", "start" }; - private static readonly uint[] _fieldTags = new uint[] { 16, 8 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProto.Descriptor.NestedTypes[0]; } } @@ -998,7 +987,6 @@ namespace Google.Protobuf.Reflection { public const int StartFieldNumber = 1; private int start_; - [pbr::ProtobufField(1, "start")] internal int Start { get { return start_; } set { @@ -1009,7 +997,6 @@ namespace Google.Protobuf.Reflection { public const int EndFieldNumber = 2; private int end_; - [pbr::ProtobufField(2, "end")] internal int End { get { return end_; } set { @@ -1109,8 +1096,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new ReservedRange()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "end", "start" }; - private static readonly uint[] _fieldTags = new uint[] { 16, 8 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProto.Descriptor.NestedTypes[1]; } } @@ -1146,7 +1131,6 @@ namespace Google.Protobuf.Reflection { public const int StartFieldNumber = 1; private int start_; - [pbr::ProtobufField(1, "start")] internal int Start { get { return start_; } set { @@ -1157,7 +1141,6 @@ namespace Google.Protobuf.Reflection { public const int EndFieldNumber = 2; private int end_; - [pbr::ProtobufField(2, "end")] internal int End { get { return end_; } set { @@ -1262,8 +1245,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new FieldDescriptorProto()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "default_value", "extendee", "label", "name", "number", "oneof_index", "options", "type", "type_name" }; - private static readonly uint[] _fieldTags = new uint[] { 58, 18, 32, 10, 24, 72, 66, 40, 50 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[3]; } } @@ -1307,7 +1288,6 @@ namespace Google.Protobuf.Reflection { public const int NameFieldNumber = 1; private string name_ = ""; - [pbr::ProtobufField(1, "name")] internal string Name { get { return name_; } set { @@ -1318,7 +1298,6 @@ namespace Google.Protobuf.Reflection { public const int NumberFieldNumber = 3; private int number_; - [pbr::ProtobufField(3, "number")] internal int Number { get { return number_; } set { @@ -1329,7 +1308,6 @@ namespace Google.Protobuf.Reflection { public const int LabelFieldNumber = 4; private global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label label_ = global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label.LABEL_OPTIONAL; - [pbr::ProtobufField(4, "label")] internal global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Label Label { get { return label_; } set { @@ -1340,7 +1318,6 @@ namespace Google.Protobuf.Reflection { public const int TypeFieldNumber = 5; private global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type type_ = global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type.TYPE_DOUBLE; - [pbr::ProtobufField(5, "type")] internal global::Google.Protobuf.Reflection.FieldDescriptorProto.Types.Type Type { get { return type_; } set { @@ -1351,7 +1328,6 @@ namespace Google.Protobuf.Reflection { public const int TypeNameFieldNumber = 6; private string typeName_ = ""; - [pbr::ProtobufField(6, "type_name")] internal string TypeName { get { return typeName_; } set { @@ -1362,7 +1338,6 @@ namespace Google.Protobuf.Reflection { public const int ExtendeeFieldNumber = 2; private string extendee_ = ""; - [pbr::ProtobufField(2, "extendee")] internal string Extendee { get { return extendee_; } set { @@ -1373,7 +1348,6 @@ namespace Google.Protobuf.Reflection { public const int DefaultValueFieldNumber = 7; private string defaultValue_ = ""; - [pbr::ProtobufField(7, "default_value")] internal string DefaultValue { get { return defaultValue_; } set { @@ -1384,7 +1358,6 @@ namespace Google.Protobuf.Reflection { public const int OneofIndexFieldNumber = 9; private int oneofIndex_; - [pbr::ProtobufField(9, "oneof_index")] internal int OneofIndex { get { return oneofIndex_; } set { @@ -1395,7 +1368,6 @@ namespace Google.Protobuf.Reflection { public const int OptionsFieldNumber = 8; private global::Google.Protobuf.Reflection.FieldOptions options_; - [pbr::ProtobufField(8, "options")] internal global::Google.Protobuf.Reflection.FieldOptions Options { get { return options_; } set { @@ -1646,8 +1618,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new OneofDescriptorProto()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "name" }; - private static readonly uint[] _fieldTags = new uint[] { 10 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[4]; } } @@ -1682,7 +1652,6 @@ namespace Google.Protobuf.Reflection { public const int NameFieldNumber = 1; private string name_ = ""; - [pbr::ProtobufField(1, "name")] internal string Name { get { return name_; } set { @@ -1766,8 +1735,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new EnumDescriptorProto()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "name", "options", "value" }; - private static readonly uint[] _fieldTags = new uint[] { 10, 26, 18 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[5]; } } @@ -1806,7 +1773,6 @@ namespace Google.Protobuf.Reflection { public const int NameFieldNumber = 1; private string name_ = ""; - [pbr::ProtobufField(1, "name")] internal string Name { get { return name_; } set { @@ -1819,14 +1785,12 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_value_codec = pb::FieldCodec.ForMessage(18, global::Google.Protobuf.Reflection.EnumValueDescriptorProto.Parser); private readonly pbc::RepeatedField value_ = new pbc::RepeatedField(); - [pbr::ProtobufField(2, "value")] internal pbc::RepeatedField Value { get { return value_; } } public const int OptionsFieldNumber = 3; private global::Google.Protobuf.Reflection.EnumOptions options_; - [pbr::ProtobufField(3, "options")] internal global::Google.Protobuf.Reflection.EnumOptions Options { get { return options_; } set { @@ -1941,8 +1905,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new EnumValueDescriptorProto()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "name", "number", "options" }; - private static readonly uint[] _fieldTags = new uint[] { 10, 16, 26 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[6]; } } @@ -1980,7 +1942,6 @@ namespace Google.Protobuf.Reflection { public const int NameFieldNumber = 1; private string name_ = ""; - [pbr::ProtobufField(1, "name")] internal string Name { get { return name_; } set { @@ -1991,7 +1952,6 @@ namespace Google.Protobuf.Reflection { public const int NumberFieldNumber = 2; private int number_; - [pbr::ProtobufField(2, "number")] internal int Number { get { return number_; } set { @@ -2002,7 +1962,6 @@ namespace Google.Protobuf.Reflection { public const int OptionsFieldNumber = 3; private global::Google.Protobuf.Reflection.EnumValueOptions options_; - [pbr::ProtobufField(3, "options")] internal global::Google.Protobuf.Reflection.EnumValueOptions Options { get { return options_; } set { @@ -2124,8 +2083,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new ServiceDescriptorProto()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "method", "name", "options" }; - private static readonly uint[] _fieldTags = new uint[] { 18, 10, 26 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[7]; } } @@ -2164,7 +2121,6 @@ namespace Google.Protobuf.Reflection { public const int NameFieldNumber = 1; private string name_ = ""; - [pbr::ProtobufField(1, "name")] internal string Name { get { return name_; } set { @@ -2177,14 +2133,12 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_method_codec = pb::FieldCodec.ForMessage(18, global::Google.Protobuf.Reflection.MethodDescriptorProto.Parser); private readonly pbc::RepeatedField method_ = new pbc::RepeatedField(); - [pbr::ProtobufField(2, "method")] internal pbc::RepeatedField Method { get { return method_; } } public const int OptionsFieldNumber = 3; private global::Google.Protobuf.Reflection.ServiceOptions options_; - [pbr::ProtobufField(3, "options")] internal global::Google.Protobuf.Reflection.ServiceOptions Options { get { return options_; } set { @@ -2299,8 +2253,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new MethodDescriptorProto()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "client_streaming", "input_type", "name", "options", "output_type", "server_streaming" }; - private static readonly uint[] _fieldTags = new uint[] { 40, 18, 10, 34, 26, 48 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[8]; } } @@ -2341,7 +2293,6 @@ namespace Google.Protobuf.Reflection { public const int NameFieldNumber = 1; private string name_ = ""; - [pbr::ProtobufField(1, "name")] internal string Name { get { return name_; } set { @@ -2352,7 +2303,6 @@ namespace Google.Protobuf.Reflection { public const int InputTypeFieldNumber = 2; private string inputType_ = ""; - [pbr::ProtobufField(2, "input_type")] internal string InputType { get { return inputType_; } set { @@ -2363,7 +2313,6 @@ namespace Google.Protobuf.Reflection { public const int OutputTypeFieldNumber = 3; private string outputType_ = ""; - [pbr::ProtobufField(3, "output_type")] internal string OutputType { get { return outputType_; } set { @@ -2374,7 +2323,6 @@ namespace Google.Protobuf.Reflection { public const int OptionsFieldNumber = 4; private global::Google.Protobuf.Reflection.MethodOptions options_; - [pbr::ProtobufField(4, "options")] internal global::Google.Protobuf.Reflection.MethodOptions Options { get { return options_; } set { @@ -2385,7 +2333,6 @@ namespace Google.Protobuf.Reflection { public const int ClientStreamingFieldNumber = 5; private bool clientStreaming_; - [pbr::ProtobufField(5, "client_streaming")] internal bool ClientStreaming { get { return clientStreaming_; } set { @@ -2396,7 +2343,6 @@ namespace Google.Protobuf.Reflection { public const int ServerStreamingFieldNumber = 6; private bool serverStreaming_; - [pbr::ProtobufField(6, "server_streaming")] internal bool ServerStreaming { get { return serverStreaming_; } set { @@ -2566,8 +2512,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new FileOptions()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "cc_enable_arenas", "cc_generic_services", "csharp_namespace", "deprecated", "go_package", "java_generate_equals_and_hash", "java_generic_services", "java_multiple_files", "java_outer_classname", "java_package", "java_string_check_utf8", "objc_class_prefix", "optimize_for", "py_generic_services", "uninterpreted_option" }; - private static readonly uint[] _fieldTags = new uint[] { 248, 128, 298, 184, 90, 160, 136, 80, 66, 10, 216, 290, 72, 144, 7994 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[9]; } } @@ -2617,7 +2561,6 @@ namespace Google.Protobuf.Reflection { public const int JavaPackageFieldNumber = 1; private string javaPackage_ = ""; - [pbr::ProtobufField(1, "java_package")] internal string JavaPackage { get { return javaPackage_; } set { @@ -2628,7 +2571,6 @@ namespace Google.Protobuf.Reflection { public const int JavaOuterClassnameFieldNumber = 8; private string javaOuterClassname_ = ""; - [pbr::ProtobufField(8, "java_outer_classname")] internal string JavaOuterClassname { get { return javaOuterClassname_; } set { @@ -2639,7 +2581,6 @@ namespace Google.Protobuf.Reflection { public const int JavaMultipleFilesFieldNumber = 10; private bool javaMultipleFiles_; - [pbr::ProtobufField(10, "java_multiple_files")] internal bool JavaMultipleFiles { get { return javaMultipleFiles_; } set { @@ -2650,7 +2591,6 @@ namespace Google.Protobuf.Reflection { public const int JavaGenerateEqualsAndHashFieldNumber = 20; private bool javaGenerateEqualsAndHash_; - [pbr::ProtobufField(20, "java_generate_equals_and_hash")] internal bool JavaGenerateEqualsAndHash { get { return javaGenerateEqualsAndHash_; } set { @@ -2661,7 +2601,6 @@ namespace Google.Protobuf.Reflection { public const int JavaStringCheckUtf8FieldNumber = 27; private bool javaStringCheckUtf8_; - [pbr::ProtobufField(27, "java_string_check_utf8")] internal bool JavaStringCheckUtf8 { get { return javaStringCheckUtf8_; } set { @@ -2672,7 +2611,6 @@ namespace Google.Protobuf.Reflection { public const int OptimizeForFieldNumber = 9; private global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode optimizeFor_ = global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode.SPEED; - [pbr::ProtobufField(9, "optimize_for")] internal global::Google.Protobuf.Reflection.FileOptions.Types.OptimizeMode OptimizeFor { get { return optimizeFor_; } set { @@ -2683,7 +2621,6 @@ namespace Google.Protobuf.Reflection { public const int GoPackageFieldNumber = 11; private string goPackage_ = ""; - [pbr::ProtobufField(11, "go_package")] internal string GoPackage { get { return goPackage_; } set { @@ -2694,7 +2631,6 @@ namespace Google.Protobuf.Reflection { public const int CcGenericServicesFieldNumber = 16; private bool ccGenericServices_; - [pbr::ProtobufField(16, "cc_generic_services")] internal bool CcGenericServices { get { return ccGenericServices_; } set { @@ -2705,7 +2641,6 @@ namespace Google.Protobuf.Reflection { public const int JavaGenericServicesFieldNumber = 17; private bool javaGenericServices_; - [pbr::ProtobufField(17, "java_generic_services")] internal bool JavaGenericServices { get { return javaGenericServices_; } set { @@ -2716,7 +2651,6 @@ namespace Google.Protobuf.Reflection { public const int PyGenericServicesFieldNumber = 18; private bool pyGenericServices_; - [pbr::ProtobufField(18, "py_generic_services")] internal bool PyGenericServices { get { return pyGenericServices_; } set { @@ -2727,7 +2661,6 @@ namespace Google.Protobuf.Reflection { public const int DeprecatedFieldNumber = 23; private bool deprecated_; - [pbr::ProtobufField(23, "deprecated")] internal bool Deprecated { get { return deprecated_; } set { @@ -2738,7 +2671,6 @@ namespace Google.Protobuf.Reflection { public const int CcEnableArenasFieldNumber = 31; private bool ccEnableArenas_; - [pbr::ProtobufField(31, "cc_enable_arenas")] internal bool CcEnableArenas { get { return ccEnableArenas_; } set { @@ -2749,7 +2681,6 @@ namespace Google.Protobuf.Reflection { public const int ObjcClassPrefixFieldNumber = 36; private string objcClassPrefix_ = ""; - [pbr::ProtobufField(36, "objc_class_prefix")] internal string ObjcClassPrefix { get { return objcClassPrefix_; } set { @@ -2760,7 +2691,6 @@ namespace Google.Protobuf.Reflection { public const int CsharpNamespaceFieldNumber = 37; private string csharpNamespace_ = ""; - [pbr::ProtobufField(37, "csharp_namespace")] internal string CsharpNamespace { get { return csharpNamespace_; } set { @@ -2773,7 +2703,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_uninterpretedOption_codec = pb::FieldCodec.ForMessage(7994, global::Google.Protobuf.Reflection.UninterpretedOption.Parser); private readonly pbc::RepeatedField uninterpretedOption_ = new pbc::RepeatedField(); - [pbr::ProtobufField(999, "uninterpreted_option")] internal pbc::RepeatedField UninterpretedOption { get { return uninterpretedOption_; } } @@ -3082,8 +3011,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new MessageOptions()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "deprecated", "map_entry", "message_set_wire_format", "no_standard_descriptor_accessor", "uninterpreted_option" }; - private static readonly uint[] _fieldTags = new uint[] { 24, 56, 8, 16, 7994 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[10]; } } @@ -3123,7 +3050,6 @@ namespace Google.Protobuf.Reflection { public const int MessageSetWireFormatFieldNumber = 1; private bool messageSetWireFormat_; - [pbr::ProtobufField(1, "message_set_wire_format")] internal bool MessageSetWireFormat { get { return messageSetWireFormat_; } set { @@ -3134,7 +3060,6 @@ namespace Google.Protobuf.Reflection { public const int NoStandardDescriptorAccessorFieldNumber = 2; private bool noStandardDescriptorAccessor_; - [pbr::ProtobufField(2, "no_standard_descriptor_accessor")] internal bool NoStandardDescriptorAccessor { get { return noStandardDescriptorAccessor_; } set { @@ -3145,7 +3070,6 @@ namespace Google.Protobuf.Reflection { public const int DeprecatedFieldNumber = 3; private bool deprecated_; - [pbr::ProtobufField(3, "deprecated")] internal bool Deprecated { get { return deprecated_; } set { @@ -3156,7 +3080,6 @@ namespace Google.Protobuf.Reflection { public const int MapEntryFieldNumber = 7; private bool mapEntry_; - [pbr::ProtobufField(7, "map_entry")] internal bool MapEntry { get { return mapEntry_; } set { @@ -3169,7 +3092,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_uninterpretedOption_codec = pb::FieldCodec.ForMessage(7994, global::Google.Protobuf.Reflection.UninterpretedOption.Parser); private readonly pbc::RepeatedField uninterpretedOption_ = new pbc::RepeatedField(); - [pbr::ProtobufField(999, "uninterpreted_option")] internal pbc::RepeatedField UninterpretedOption { get { return uninterpretedOption_; } } @@ -3306,8 +3228,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new FieldOptions()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "ctype", "deprecated", "jstype", "lazy", "packed", "uninterpreted_option", "weak" }; - private static readonly uint[] _fieldTags = new uint[] { 8, 24, 48, 40, 16, 7994, 80 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[11]; } } @@ -3349,7 +3269,6 @@ namespace Google.Protobuf.Reflection { public const int CtypeFieldNumber = 1; private global::Google.Protobuf.Reflection.FieldOptions.Types.CType ctype_ = global::Google.Protobuf.Reflection.FieldOptions.Types.CType.STRING; - [pbr::ProtobufField(1, "ctype")] internal global::Google.Protobuf.Reflection.FieldOptions.Types.CType Ctype { get { return ctype_; } set { @@ -3360,7 +3279,6 @@ namespace Google.Protobuf.Reflection { public const int PackedFieldNumber = 2; private bool packed_; - [pbr::ProtobufField(2, "packed")] internal bool Packed { get { return packed_; } set { @@ -3371,7 +3289,6 @@ namespace Google.Protobuf.Reflection { public const int JstypeFieldNumber = 6; private global::Google.Protobuf.Reflection.FieldOptions.Types.JSType jstype_ = global::Google.Protobuf.Reflection.FieldOptions.Types.JSType.JS_NORMAL; - [pbr::ProtobufField(6, "jstype")] internal global::Google.Protobuf.Reflection.FieldOptions.Types.JSType Jstype { get { return jstype_; } set { @@ -3382,7 +3299,6 @@ namespace Google.Protobuf.Reflection { public const int LazyFieldNumber = 5; private bool lazy_; - [pbr::ProtobufField(5, "lazy")] internal bool Lazy { get { return lazy_; } set { @@ -3393,7 +3309,6 @@ namespace Google.Protobuf.Reflection { public const int DeprecatedFieldNumber = 3; private bool deprecated_; - [pbr::ProtobufField(3, "deprecated")] internal bool Deprecated { get { return deprecated_; } set { @@ -3404,7 +3319,6 @@ namespace Google.Protobuf.Reflection { public const int WeakFieldNumber = 10; private bool weak_; - [pbr::ProtobufField(10, "weak")] internal bool Weak { get { return weak_; } set { @@ -3417,7 +3331,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_uninterpretedOption_codec = pb::FieldCodec.ForMessage(7994, global::Google.Protobuf.Reflection.UninterpretedOption.Parser); private readonly pbc::RepeatedField uninterpretedOption_ = new pbc::RepeatedField(); - [pbr::ProtobufField(999, "uninterpreted_option")] internal pbc::RepeatedField UninterpretedOption { get { return uninterpretedOption_; } } @@ -3604,8 +3517,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new EnumOptions()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "allow_alias", "deprecated", "uninterpreted_option" }; - private static readonly uint[] _fieldTags = new uint[] { 16, 24, 7994 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[12]; } } @@ -3643,7 +3554,6 @@ namespace Google.Protobuf.Reflection { public const int AllowAliasFieldNumber = 2; private bool allowAlias_; - [pbr::ProtobufField(2, "allow_alias")] internal bool AllowAlias { get { return allowAlias_; } set { @@ -3654,7 +3564,6 @@ namespace Google.Protobuf.Reflection { public const int DeprecatedFieldNumber = 3; private bool deprecated_; - [pbr::ProtobufField(3, "deprecated")] internal bool Deprecated { get { return deprecated_; } set { @@ -3667,7 +3576,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_uninterpretedOption_codec = pb::FieldCodec.ForMessage(7994, global::Google.Protobuf.Reflection.UninterpretedOption.Parser); private readonly pbc::RepeatedField uninterpretedOption_ = new pbc::RepeatedField(); - [pbr::ProtobufField(999, "uninterpreted_option")] internal pbc::RepeatedField UninterpretedOption { get { return uninterpretedOption_; } } @@ -3772,8 +3680,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new EnumValueOptions()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "deprecated", "uninterpreted_option" }; - private static readonly uint[] _fieldTags = new uint[] { 8, 7994 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[13]; } } @@ -3810,7 +3716,6 @@ namespace Google.Protobuf.Reflection { public const int DeprecatedFieldNumber = 1; private bool deprecated_; - [pbr::ProtobufField(1, "deprecated")] internal bool Deprecated { get { return deprecated_; } set { @@ -3823,7 +3728,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_uninterpretedOption_codec = pb::FieldCodec.ForMessage(7994, global::Google.Protobuf.Reflection.UninterpretedOption.Parser); private readonly pbc::RepeatedField uninterpretedOption_ = new pbc::RepeatedField(); - [pbr::ProtobufField(999, "uninterpreted_option")] internal pbc::RepeatedField UninterpretedOption { get { return uninterpretedOption_; } } @@ -3912,8 +3816,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new ServiceOptions()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "deprecated", "uninterpreted_option" }; - private static readonly uint[] _fieldTags = new uint[] { 264, 7994 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[14]; } } @@ -3950,7 +3852,6 @@ namespace Google.Protobuf.Reflection { public const int DeprecatedFieldNumber = 33; private bool deprecated_; - [pbr::ProtobufField(33, "deprecated")] internal bool Deprecated { get { return deprecated_; } set { @@ -3963,7 +3864,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_uninterpretedOption_codec = pb::FieldCodec.ForMessage(7994, global::Google.Protobuf.Reflection.UninterpretedOption.Parser); private readonly pbc::RepeatedField uninterpretedOption_ = new pbc::RepeatedField(); - [pbr::ProtobufField(999, "uninterpreted_option")] internal pbc::RepeatedField UninterpretedOption { get { return uninterpretedOption_; } } @@ -4052,8 +3952,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new MethodOptions()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "deprecated", "uninterpreted_option" }; - private static readonly uint[] _fieldTags = new uint[] { 264, 7994 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[15]; } } @@ -4090,7 +3988,6 @@ namespace Google.Protobuf.Reflection { public const int DeprecatedFieldNumber = 33; private bool deprecated_; - [pbr::ProtobufField(33, "deprecated")] internal bool Deprecated { get { return deprecated_; } set { @@ -4103,7 +4000,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_uninterpretedOption_codec = pb::FieldCodec.ForMessage(7994, global::Google.Protobuf.Reflection.UninterpretedOption.Parser); private readonly pbc::RepeatedField uninterpretedOption_ = new pbc::RepeatedField(); - [pbr::ProtobufField(999, "uninterpreted_option")] internal pbc::RepeatedField UninterpretedOption { get { return uninterpretedOption_; } } @@ -4192,8 +4088,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new UninterpretedOption()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "aggregate_value", "double_value", "identifier_value", "name", "negative_int_value", "positive_int_value", "string_value" }; - private static readonly uint[] _fieldTags = new uint[] { 66, 49, 26, 18, 40, 32, 58 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[16]; } } @@ -4237,14 +4131,12 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_name_codec = pb::FieldCodec.ForMessage(18, global::Google.Protobuf.Reflection.UninterpretedOption.Types.NamePart.Parser); private readonly pbc::RepeatedField name_ = new pbc::RepeatedField(); - [pbr::ProtobufField(2, "name")] internal pbc::RepeatedField Name { get { return name_; } } public const int IdentifierValueFieldNumber = 3; private string identifierValue_ = ""; - [pbr::ProtobufField(3, "identifier_value")] internal string IdentifierValue { get { return identifierValue_; } set { @@ -4255,7 +4147,6 @@ namespace Google.Protobuf.Reflection { public const int PositiveIntValueFieldNumber = 4; private ulong positiveIntValue_; - [pbr::ProtobufField(4, "positive_int_value")] internal ulong PositiveIntValue { get { return positiveIntValue_; } set { @@ -4266,7 +4157,6 @@ namespace Google.Protobuf.Reflection { public const int NegativeIntValueFieldNumber = 5; private long negativeIntValue_; - [pbr::ProtobufField(5, "negative_int_value")] internal long NegativeIntValue { get { return negativeIntValue_; } set { @@ -4277,7 +4167,6 @@ namespace Google.Protobuf.Reflection { public const int DoubleValueFieldNumber = 6; private double doubleValue_; - [pbr::ProtobufField(6, "double_value")] internal double DoubleValue { get { return doubleValue_; } set { @@ -4288,7 +4177,6 @@ namespace Google.Protobuf.Reflection { public const int StringValueFieldNumber = 7; private pb::ByteString stringValue_ = pb::ByteString.Empty; - [pbr::ProtobufField(7, "string_value")] internal pb::ByteString StringValue { get { return stringValue_; } set { @@ -4299,7 +4187,6 @@ namespace Google.Protobuf.Reflection { public const int AggregateValueFieldNumber = 8; private string aggregateValue_ = ""; - [pbr::ProtobufField(8, "aggregate_value")] internal string AggregateValue { get { return aggregateValue_; } set { @@ -4473,8 +4360,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new NamePart()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "is_extension", "name_part" }; - private static readonly uint[] _fieldTags = new uint[] { 16, 10 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.UninterpretedOption.Descriptor.NestedTypes[0]; } } @@ -4510,7 +4395,6 @@ namespace Google.Protobuf.Reflection { public const int NamePart_FieldNumber = 1; private string namePart_ = ""; - [pbr::ProtobufField(1, "name_part")] internal string NamePart_ { get { return namePart_; } set { @@ -4521,7 +4405,6 @@ namespace Google.Protobuf.Reflection { public const int IsExtensionFieldNumber = 2; private bool isExtension_; - [pbr::ProtobufField(2, "is_extension")] internal bool IsExtension { get { return isExtension_; } set { @@ -4626,8 +4509,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new SourceCodeInfo()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "location" }; - private static readonly uint[] _fieldTags = new uint[] { 10 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.DescriptorProtoFile.Descriptor.MessageTypes[17]; } } @@ -4665,7 +4546,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_location_codec = pb::FieldCodec.ForMessage(10, global::Google.Protobuf.Reflection.SourceCodeInfo.Types.Location.Parser); private readonly pbc::RepeatedField location_ = new pbc::RepeatedField(); - [pbr::ProtobufField(1, "location")] internal pbc::RepeatedField Location { get { return location_; } } @@ -4739,8 +4619,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new Location()); public static pb::MessageParser Parser { get { return _parser; } } - private static readonly string[] _fieldNames = new string[] { "leading_comments", "leading_detached_comments", "path", "span", "trailing_comments" }; - private static readonly uint[] _fieldTags = new uint[] { 26, 50, 10, 18, 34 }; public static pbr::MessageDescriptor Descriptor { get { return global::Google.Protobuf.Reflection.SourceCodeInfo.Descriptor.NestedTypes[0]; } } @@ -4784,7 +4662,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_path_codec = pb::FieldCodec.ForInt32(10); private readonly pbc::RepeatedField path_ = new pbc::RepeatedField(); - [pbr::ProtobufField(1, "path")] internal pbc::RepeatedField Path { get { return path_; } } @@ -4793,14 +4670,12 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_span_codec = pb::FieldCodec.ForInt32(18); private readonly pbc::RepeatedField span_ = new pbc::RepeatedField(); - [pbr::ProtobufField(2, "span")] internal pbc::RepeatedField Span { get { return span_; } } public const int LeadingCommentsFieldNumber = 3; private string leadingComments_ = ""; - [pbr::ProtobufField(3, "leading_comments")] internal string LeadingComments { get { return leadingComments_; } set { @@ -4811,7 +4686,6 @@ namespace Google.Protobuf.Reflection { public const int TrailingCommentsFieldNumber = 4; private string trailingComments_ = ""; - [pbr::ProtobufField(4, "trailing_comments")] internal string TrailingComments { get { return trailingComments_; } set { @@ -4824,7 +4698,6 @@ namespace Google.Protobuf.Reflection { private static readonly pb::FieldCodec _repeated_leadingDetachedComments_codec = pb::FieldCodec.ForString(50); private readonly pbc::RepeatedField leadingDetachedComments_ = new pbc::RepeatedField(); - [pbr::ProtobufField(6, "leading_detached_comments")] internal pbc::RepeatedField LeadingDetachedComments { get { return leadingDetachedComments_; } } diff --git a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs index 57378e4c..a8609b8a 100644 --- a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs @@ -46,10 +46,11 @@ namespace Google.Protobuf.Reflection private readonly MessageDescriptor containingType; private readonly OneofDescriptor containingOneof; private FieldType fieldType; + private readonly string propertyName; // Annoyingly, needed in Crosslink. private IFieldAccessor accessor; internal FieldDescriptor(FieldDescriptorProto proto, FileDescriptor file, - MessageDescriptor parent, int index) + MessageDescriptor parent, int index, string propertyName) : base(file, file.ComputeFullName(parent, proto.Name), index) { this.proto = proto; @@ -76,6 +77,12 @@ namespace Google.Protobuf.Reflection } file.DescriptorPool.AddSymbol(this); + // We can't create the accessor until we've cross-linked, unfortunately, as we + // may not know whether the type of the field is a map or not. Remember the property name + // for later. + // We could trust the generated code and check whether the type of the property is + // a MapField, but that feels a tad nasty. + this.propertyName = propertyName; } /// @@ -291,26 +298,19 @@ namespace Google.Protobuf.Reflection { throw new DescriptorValidationException(this, "MessageSet format is not supported."); } - - accessor = CreateAccessor(); + accessor = CreateAccessor(propertyName); } - private IFieldAccessor CreateAccessor() + private IFieldAccessor CreateAccessor(string propertyName) { - // TODO: Check the performance of this with some large protos. Each message is O(N^2) in the number of fields, - // which isn't great... - if (containingType.GeneratedType == null) + if (containingType.GeneratedType == null || propertyName == null) { return null; } - var property = containingType - .GeneratedType - .GetProperties() - .FirstOrDefault(p => p.IsDefined(typeof(ProtobufFieldAttribute), false) && - p.GetCustomAttributes(typeof(ProtobufFieldAttribute), false).Cast().Single().Number == FieldNumber); + var property = containingType.GeneratedType.GetProperty(propertyName); if (property == null) { - return null; + throw new DescriptorValidationException(this, "Property " + propertyName + " not found in " + containingType.GeneratedType); } return IsMap ? new MapFieldAccessor(property, this) : IsRepeated ? new RepeatedFieldAccessor(property, this) diff --git a/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs index ba832b89..041d4711 100644 --- a/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs @@ -62,12 +62,11 @@ namespace Google.Protobuf.Reflection get { return proto.Syntax == "proto3" ? ProtoSyntax.Proto3 : ProtoSyntax.Proto2; } } - private FileDescriptor(FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies, Type[] generatedTypes) + private FileDescriptor(FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies, GeneratedCodeInfo generatedCodeInfo) { this.pool = pool; this.proto = proto; this.dependencies = new ReadOnlyCollection((FileDescriptor[]) dependencies.Clone()); - IEnumerator generatedTypeIterator = generatedTypes == null ? null : ((IEnumerable)generatedTypes).GetEnumerator(); publicDependencies = DeterminePublicDependencies(this, proto, dependencies, allowUnknownDependencies); @@ -75,21 +74,15 @@ namespace Google.Protobuf.Reflection messageTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.MessageType, (message, index) => - new MessageDescriptor(message, this, null, index, generatedTypeIterator)); + new MessageDescriptor(message, this, null, index, generatedCodeInfo == null ? null : generatedCodeInfo.NestedTypes[index])); enumTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.EnumType, (enumType, index) => - new EnumDescriptor(enumType, this, null, index, ReflectionUtil.GetNextType(generatedTypeIterator))); + new EnumDescriptor(enumType, this, null, index, generatedCodeInfo == null ? null : generatedCodeInfo.NestedEnums[index])); services = DescriptorUtil.ConvertAndMakeReadOnly(proto.Service, (service, index) => new ServiceDescriptor(service, this, index)); - - // We should now have consumed all the generated types. - if (generatedTypeIterator != null && generatedTypeIterator.MoveNext()) - { - throw new ArgumentException("More generated types left over after consuming all expected ones", "generatedTypes"); - } } /// @@ -260,7 +253,7 @@ namespace Google.Protobuf.Reflection } return null; } - + /// /// Builds a FileDescriptor from its protocol buffer representation. /// @@ -269,10 +262,11 @@ namespace Google.Protobuf.Reflection /// file's dependencies, in the exact order listed in the .proto file. May be null, /// in which case it is treated as an empty array. /// Whether unknown dependencies are ignored (true) or cause an exception to be thrown (false). + /// Reflection information, if any. May be null, specifically for non-generated code. /// If is not /// a valid descriptor. This can occur for a number of reasons, such as a field /// having an undefined type or because two messages were defined with the same name. - private static FileDescriptor BuildFrom(FileDescriptorProto proto, FileDescriptor[] dependencies, bool allowUnknownDependencies, Type[] generatedTypes) + private static FileDescriptor BuildFrom(FileDescriptorProto proto, FileDescriptor[] dependencies, bool allowUnknownDependencies, GeneratedCodeInfo generatedCodeInfo) { // Building descriptors involves two steps: translating and linking. // In the translation step (implemented by FileDescriptor's @@ -289,7 +283,7 @@ namespace Google.Protobuf.Reflection } DescriptorPool pool = new DescriptorPool(dependencies); - FileDescriptor result = new FileDescriptor(proto, dependencies, pool, allowUnknownDependencies, generatedTypes); + FileDescriptor result = new FileDescriptor(proto, dependencies, pool, allowUnknownDependencies, generatedCodeInfo); // TODO(jonskeet): Reinstate these checks, or get rid of them entirely. They aren't in the Java code, // and fail for the CustomOptions test right now. (We get "descriptor.proto" vs "google/protobuf/descriptor.proto".) @@ -330,19 +324,13 @@ namespace Google.Protobuf.Reflection /// Creates an instance for generated code. /// /// - /// The parameter should be null for descriptors which don't correspond to - /// generated types. Otherwise, the array should represent all the generated types in the file: messages then - /// enums. Within each message, there can be nested messages and enums, which must be specified "inline" in the array: - /// containing message, nested messages, nested enums - and of course each nested message may contain *more* nested messages, - /// etc. All messages within the descriptor should be represented, even if they do not have a generated type - any - /// type without a corresponding generated type (such as map entries) should respond to a null element. - /// For example, a file with a messages OuterMessage and InnerMessage, and enums OuterEnum and InnerEnum (where - /// InnerMessage and InnerEnum are nested within InnerMessage) would result in an array of - /// OuterMessage, InnerMessage, InnerEnum, OuterEnum. + /// The parameter should be null for descriptors which don't correspond to + /// generated types. Otherwise, it should be a with nested types and nested + /// enums corresponding to the types and enums contained within the file descriptor. /// public static FileDescriptor InternalBuildGeneratedFileFrom(byte[] descriptorData, FileDescriptor[] dependencies, - Type[] generatedTypes) + GeneratedCodeInfo generatedCodeInfo) { FileDescriptorProto proto; try @@ -358,7 +346,7 @@ namespace Google.Protobuf.Reflection { // When building descriptors for generated code, we allow unknown // dependencies by default. - return BuildFrom(proto, dependencies, true, generatedTypes); + return BuildFrom(proto, dependencies, true, generatedCodeInfo); } catch (DescriptorValidationException e) { diff --git a/csharp/src/Google.Protobuf/Reflection/GeneratedCodeInfo.cs b/csharp/src/Google.Protobuf/Reflection/GeneratedCodeInfo.cs new file mode 100644 index 00000000..8c52cd12 --- /dev/null +++ b/csharp/src/Google.Protobuf/Reflection/GeneratedCodeInfo.cs @@ -0,0 +1,66 @@ +using System; + +namespace Google.Protobuf.Reflection +{ + /// + /// Extra information provided by generated code when initializing a message or file descriptor. + /// These are constructed as required, and are not long-lived. Hand-written code should + /// never need to use this type. + /// + public sealed class GeneratedCodeInfo + { + private static readonly string[] EmptyNames = new string[0]; + private static readonly GeneratedCodeInfo[] EmptyCodeInfo = new GeneratedCodeInfo[0]; + + /// + /// Irrelevant for file descriptors; the CLR type for the message for message descriptors. + /// + public Type ClrType { get; private set; } + + /// + /// Irrelevant for file descriptors; the CLR property names (in message descriptor field order) + /// for fields in the message for message descriptors. + /// + public string[] PropertyNames { get; private set; } + + /// + /// Irrelevant for file descriptors; the CLR property "base" names (in message descriptor oneof order) + /// for oneofs in the message for message descriptors. It is expected that for a oneof name of "Foo", + /// there will be a "FooCase" property and a "ClearFoo" method. + /// + public string[] OneofNames { get; private set; } + + /// + /// The reflection information for types within this file/message descriptor. Elements may be null + /// if there is no corresponding generated type, e.g. for map entry types. + /// + public GeneratedCodeInfo[] NestedTypes { get; private set; } + + /// + /// The CLR types for enums within this file/message descriptor. + /// + public Type[] NestedEnums { get; private set; } + + /// + /// Creates a GeneratedCodeInfo for a message descriptor, with nested types, nested enums, the CLR type, property names and oneof names. + /// Each array parameter may be null, to indicate a lack of values. + /// The parameter order is designed to make it feasible to format the generated code readably. + /// + public GeneratedCodeInfo(Type clrType, string[] propertyNames, string[] oneofNames, Type[] nestedEnums, GeneratedCodeInfo[] nestedTypes) + { + NestedTypes = nestedTypes ?? EmptyCodeInfo; + NestedEnums = nestedEnums ?? ReflectionUtil.EmptyTypes; + ClrType = clrType; + PropertyNames = propertyNames ?? EmptyNames; + OneofNames = oneofNames ?? EmptyNames; + } + + /// + /// Creates a GeneratedCodeInfo for a file descriptor, with only types and enums. + /// + public GeneratedCodeInfo(Type[] nestedEnums, GeneratedCodeInfo[] nestedTypes) + : this(null, null, null, nestedEnums, nestedTypes) + { + } + } +} \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs index 9413cf61..b29b4b20 100644 --- a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs @@ -66,29 +66,33 @@ namespace Google.Protobuf.Reflection private readonly Type generatedType; private IDictionary fieldAccessorsByFieldNumber; - internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex, IEnumerator generatedTypeIterator) + internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex, GeneratedCodeInfo generatedCodeInfo) : base(file, file.ComputeFullName(parent, proto.Name), typeIndex) { this.proto = proto; - generatedType = ReflectionUtil.GetNextType(generatedTypeIterator); - containingType = parent; - - oneofs = DescriptorUtil.ConvertAndMakeReadOnly(proto.OneofDecl, - (oneof, index) => - new OneofDescriptor(oneof, file, this, index)); - - nestedTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.NestedType, - (type, index) => - new MessageDescriptor(type, file, this, index, generatedTypeIterator)); + generatedType = generatedCodeInfo == null ? null : generatedCodeInfo.ClrType; - enumTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.EnumType, - (type, index) => - new EnumDescriptor(type, file, this, index, ReflectionUtil.GetNextType(generatedTypeIterator))); + containingType = parent; - // TODO(jonskeet): Sort fields first? - fields = DescriptorUtil.ConvertAndMakeReadOnly(proto.Field, - (field, index) => - new FieldDescriptor(field, file, this, index)); + oneofs = DescriptorUtil.ConvertAndMakeReadOnly( + proto.OneofDecl, + (oneof, index) => + new OneofDescriptor(oneof, file, this, index, generatedCodeInfo == null ? null : generatedCodeInfo.OneofNames[index])); + + nestedTypes = DescriptorUtil.ConvertAndMakeReadOnly( + proto.NestedType, + (type, index) => + new MessageDescriptor(type, file, this, index, generatedCodeInfo == null ? null : generatedCodeInfo.NestedTypes[index])); + + enumTypes = DescriptorUtil.ConvertAndMakeReadOnly( + proto.EnumType, + (type, index) => + new EnumDescriptor(type, file, this, index, generatedCodeInfo == null ? null : generatedCodeInfo.NestedEnums[index])); + + fields = DescriptorUtil.ConvertAndMakeReadOnly( + proto.Field, + (field, index) => + new FieldDescriptor(field, file, this, index, generatedCodeInfo == null ? null : generatedCodeInfo.PropertyNames[index])); file.DescriptorPool.AddSymbol(this); } @@ -220,6 +224,6 @@ namespace Google.Protobuf.Reflection } fieldAccessorsByFieldNumber = new ReadOnlyDictionary(fields.ToDictionary(field => field.FieldNumber, field => field.Accessor)); - } + } } } \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs index b4cc0791..a79d9de4 100644 --- a/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs @@ -41,15 +41,16 @@ namespace Google.Protobuf.Reflection private readonly OneofDescriptorProto proto; private MessageDescriptor containingType; private IList fields; - private OneofAccessor accessor; + private readonly OneofAccessor accessor; - internal OneofDescriptor(OneofDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index) + internal OneofDescriptor(OneofDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index, string clrName) : base(file, file.ComputeFullName(parent, proto.Name), index) { this.proto = proto; containingType = parent; file.DescriptorPool.AddSymbol(this); + accessor = CreateAccessor(clrName); } /// @@ -77,33 +78,23 @@ namespace Google.Protobuf.Reflection } } fields = new ReadOnlyCollection(fieldCollection); - accessor = CreateAccessor(); } - private OneofAccessor CreateAccessor() + private OneofAccessor CreateAccessor(string clrName) { - if (containingType.GeneratedType == null) + if (containingType.GeneratedType == null || clrName == null) { return null; } - var caseProperty = containingType - .GeneratedType - .GetProperties() - .FirstOrDefault(p => p.IsDefined(typeof(ProtobufOneofAttribute), false) && - p.GetCustomAttributes(typeof(ProtobufOneofAttribute), false).Cast().Single().Name == Name); + var caseProperty = containingType.GeneratedType.GetProperty(clrName + "Case"); if (caseProperty == null) { - return null; + throw new DescriptorValidationException(this, "Property " + clrName + "Case not found in " + containingType.GeneratedType); } - - var clearMethod = containingType - .GeneratedType - .GetMethods() - .FirstOrDefault(p => p.IsDefined(typeof(ProtobufOneofAttribute), false) && - p.GetCustomAttributes(typeof(ProtobufOneofAttribute), false).Cast().Single().Name == Name); + var clearMethod = containingType.GeneratedType.GetMethod("Clear" + clrName, ReflectionUtil.EmptyTypes); if (clearMethod == null) { - return null; + throw new DescriptorValidationException(this, "Method Clear" + clrName + " not found in " + containingType.GeneratedType); } return new OneofAccessor(caseProperty, clearMethod, this); diff --git a/csharp/src/Google.Protobuf/Reflection/ProtobufFieldAttribute.cs b/csharp/src/Google.Protobuf/Reflection/ProtobufFieldAttribute.cs deleted file mode 100644 index a59caea7..00000000 --- a/csharp/src/Google.Protobuf/Reflection/ProtobufFieldAttribute.cs +++ /dev/null @@ -1,58 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2015 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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. -#endregion -using System; - -namespace Google.Protobuf.Reflection -{ - /// - /// Attribute applied to a generated property corresponding to a field in a .proto file. - /// - [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] - public sealed class ProtobufFieldAttribute : Attribute - { - /// - /// The field number in the original .proto file. - /// - public int Number { get; set; } - - /// - /// The field name in the original .proto file. - /// - public string Name { get; set; } - - public ProtobufFieldAttribute(int number, string name) - { - this.Number = number; - this.Name = name; - } - } -} diff --git a/csharp/src/Google.Protobuf/Reflection/ProtobufOneofAttribute.cs b/csharp/src/Google.Protobuf/Reflection/ProtobufOneofAttribute.cs deleted file mode 100644 index 74ad8c53..00000000 --- a/csharp/src/Google.Protobuf/Reflection/ProtobufOneofAttribute.cs +++ /dev/null @@ -1,52 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2015 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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. -#endregion -using System; - -namespace Google.Protobuf.Reflection -{ - /// - /// Attribute applied to the "case" property or "clear" method corresponding to a oneof in a .proto file. - /// - [AttributeUsage(AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = false)] - public sealed class ProtobufOneofAttribute : Attribute - { - /// - /// The oneof name in the original .proto file. - /// - public string Name { get; set; } - - public ProtobufOneofAttribute(string name) - { - this.Name = name; - } - } -} diff --git a/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs b/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs index ec222dc1..5b3cbb36 100644 --- a/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs +++ b/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs @@ -102,25 +102,6 @@ namespace Google.Protobuf.Reflection Expression castTarget = Expression.Convert(targetParameter, method.DeclaringType); Expression call = Expression.Call(castTarget, method); return Expression.Lambda>(call, targetParameter).Compile(); - } - - /// - /// Returns the next type from an iterator of types, unless the iterator is a null reference, - /// in which case null is returned. - /// - internal static Type GetNextType(IEnumerator generatedTypeIterator) - { - if (generatedTypeIterator == null) - { - return null; - } - if (!generatedTypeIterator.MoveNext()) - { - // This parameter name corresponds to any public method supplying the generated types to start with. - throw new ArgumentException("More generated types left over after consuming all expected ones", "generatedTypes"); - } - return generatedTypeIterator.Current; - } - + } } } \ No newline at end of file diff --git a/src/google/protobuf/compiler/csharp/csharp_field_base.cc b/src/google/protobuf/compiler/csharp/csharp_field_base.cc index d327e267..2459d457 100644 --- a/src/google/protobuf/compiler/csharp/csharp_field_base.cc +++ b/src/google/protobuf/compiler/csharp/csharp_field_base.cc @@ -74,7 +74,6 @@ void FieldGeneratorBase::SetCommonFieldVariables( (*variables)["property_name"] = property_name(); (*variables)["type_name"] = type_name(); - (*variables)["original_name"] = descriptor_->name(); (*variables)["name"] = name(); (*variables)["descriptor_name"] = descriptor_->name(); (*variables)["default_value"] = default_value(); @@ -431,10 +430,6 @@ std::string FieldGeneratorBase::capitalized_type_name() { } } -std::string FieldGeneratorBase::field_ordinal() { - return SimpleItoa(fieldOrdinal_); -} - } // namespace csharp } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/csharp/csharp_field_base.h b/src/google/protobuf/compiler/csharp/csharp_field_base.h index 4761dc49..d83543bd 100644 --- a/src/google/protobuf/compiler/csharp/csharp_field_base.h +++ b/src/google/protobuf/compiler/csharp/csharp_field_base.h @@ -85,7 +85,6 @@ class FieldGeneratorBase : public SourceGeneratorBase { std::string default_value(const FieldDescriptor* descriptor); std::string number(); std::string capitalized_type_name(); - std::string field_ordinal(); private: void SetCommonFieldVariables(map* variables); diff --git a/src/google/protobuf/compiler/csharp/csharp_map_field.cc b/src/google/protobuf/compiler/csharp/csharp_map_field.cc index bdbfd92b..cba24a59 100644 --- a/src/google/protobuf/compiler/csharp/csharp_map_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_map_field.cc @@ -79,7 +79,6 @@ void MapFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, - "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ pbc::MapField<$key_type_name$, $value_type_name$> $property_name$ {\n" " get { return $name$_; }\n" "}\n"); diff --git a/src/google/protobuf/compiler/csharp/csharp_message.cc b/src/google/protobuf/compiler/csharp/csharp_message.cc index 42fd5065..6a22a336 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message.cc +++ b/src/google/protobuf/compiler/csharp/csharp_message.cc @@ -115,19 +115,6 @@ void MessageGenerator::Generate(io::Printer* printer) { vars, "private static readonly pb::MessageParser<$class_name$> _parser = new pb::MessageParser<$class_name$>(() => new $class_name$());\n" "public static pb::MessageParser<$class_name$> Parser { get { return _parser; } }\n\n"); - printer->Print( - "private static readonly string[] _fieldNames = " - "new string[] { $slash$$field_names$$slash$ };\n", - "field_names", JoinStrings(field_names(), "\", \""), - "slash", field_names().size() > 0 ? "\"" : ""); - std::vector tags; - for (int i = 0; i < field_names().size(); i++) { - uint32 tag = FixedMakeTag(descriptor_->FindFieldByName(field_names()[i])); - tags.push_back(SimpleItoa(tag)); - } - printer->Print( - "private static readonly uint[] _fieldTags = new uint[] { $tags$ };\n", - "tags", JoinStrings(tags, ", ")); // Access the message descriptor via the relevant file descriptor or containing message descriptor. if (!descriptor_->containing_type()) { diff --git a/src/google/protobuf/compiler/csharp/csharp_message_field.cc b/src/google/protobuf/compiler/csharp/csharp_message_field.cc index c2b6ff76..d2c3a88b 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_message_field.cc @@ -64,7 +64,6 @@ void MessageFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, - "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ $type_name$ $property_name$ {\n" " get { return $name$_; }\n" " set {\n" @@ -159,7 +158,6 @@ void MessageOneofFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, - "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ $type_name$ $property_name$ {\n" " get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : null; }\n" " set {\n" diff --git a/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc b/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc index 2c9338bd..4454ef02 100644 --- a/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc @@ -71,7 +71,6 @@ void PrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, - "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ $type_name$ $property_name$ {\n" " get { return $name$_; }\n" " set {\n" @@ -175,7 +174,6 @@ void PrimitiveOneofFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, - "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ $type_name$ $property_name$ {\n" " get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : $default_value$; }\n" " set {\n" diff --git a/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc b/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc index 60d06154..c1b29e2b 100644 --- a/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc @@ -65,7 +65,6 @@ void RepeatedEnumFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, - "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ pbc::RepeatedField<$type_name$> $property_name$ {\n" " get { return $name$_; }\n" "}\n"); diff --git a/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc b/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc index 921798b0..d939fc79 100644 --- a/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc @@ -78,7 +78,6 @@ void RepeatedMessageFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, - "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ pbc::RepeatedField<$type_name$> $property_name$ {\n" " get { return $name$_; }\n" "}\n"); diff --git a/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc b/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc index bcfb9936..5b5d9b3d 100644 --- a/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc @@ -65,7 +65,6 @@ void RepeatedPrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, - "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ pbc::RepeatedField<$type_name$> $property_name$ {\n" " get { return $name$_; }\n" "}\n"); diff --git a/src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc b/src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc index 3f39250e..6eb1547e 100644 --- a/src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc +++ b/src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc @@ -36,6 +36,7 @@ #include #include #include +#include #include @@ -168,10 +169,10 @@ void UmbrellaClassGenerator::WriteDescriptor(io::Printer* printer) { printer->Print("\"$base64$\", \n", "base64", base64.substr(0, 60)); base64 = base64.substr(60); } - printer->Outdent(); printer->Print("\"$base64$\"));\n", "base64", base64); printer->Outdent(); printer->Outdent(); + printer->Outdent(); // ----------------------------------------------------------------- // Invoke InternalBuildGeneratedFileFrom() to build the file. @@ -184,34 +185,108 @@ void UmbrellaClassGenerator::WriteDescriptor(io::Printer* printer) { "full_umbrella_class_name", GetFullUmbrellaClassName(file_->dependency(i))); } - // Specify all the generated types (messages and enums), recursively, as an array. printer->Print("},\n" - " new global::System.Type[] { "); - for (int i = 0; i < file_->message_type_count(); i++) { - WriteTypeLiterals(file_->message_type(i), printer); + " new pbr::GeneratedCodeInfo("); + // Specify all the generated code information, recursively. + if (file_->enum_type_count() > 0) { + printer->Print("new[] {"); + for (int i = 0; i < file_->enum_type_count(); i++) { + printer->Print("typeof($type_name$), ", "type_name", GetClassName(file_->enum_type(i))); + } + printer->Print("}, "); + } + else { + printer->Print("null, "); + } + if (file_->message_type_count() > 0) { + printer->Print("new pbr::GeneratedCodeInfo[] {\n"); + printer->Indent(); + printer->Indent(); + printer->Indent(); + for (int i = 0; i < file_->message_type_count(); i++) { + WriteGeneratedCodeInfo(file_->message_type(i), printer, i == file_->message_type_count() - 1); + } + printer->Outdent(); + printer->Print("\n}));\n"); + printer->Outdent(); + printer->Outdent(); } - for (int i = 0; i < file_->enum_type_count(); i++) { - printer->Print("typeof($type_name$), ", "type_name", GetClassName(file_->enum_type(i))); + else { + printer->Print("null));\n"); } - printer->Print("});\n"); printer->Outdent(); printer->Print("}\n"); printer->Print("#endregion\n\n"); } -void UmbrellaClassGenerator::WriteTypeLiterals(const Descriptor* descriptor, io::Printer* printer) { - if (IsMapEntryMessage(descriptor)) { - printer->Print("null, "); - return; - } - printer->Print("typeof($type_name$), ", "type_name", GetClassName(descriptor)); - for (int i = 0; i < descriptor->nested_type_count(); i++) { - WriteTypeLiterals(descriptor->nested_type(i), printer); - } - for (int i = 0; i < descriptor->enum_type_count(); i++) { - printer->Print("typeof($type_name$), ", "type_name", GetClassName(descriptor->enum_type(i))); - } +// Write out the generated code for a particular message. This consists of the CLR type, property names +// corresponding to fields, names corresponding to oneofs, nested enums, and nested types. Each array part +// can be specified as null if it would be empty, to make the generated code somewhat simpler to read. +// We write a line break at the end of each generated code info, so that in the final file we'll see all +// the types, pre-ordered depth first, one per line. The indentation will be slightly unusual, +// in that it will look like a single array when it's actually constructing a tree, but it'll be easy to +// read even with multiple levels of nesting. +// The "last" parameter indicates whether this message descriptor is the last one being printed in this immediate +// context. It governs whether or not a trailing comma and newline is written after the constructor, effectively +// just controlling the formatting in the generated code. +void UmbrellaClassGenerator::WriteGeneratedCodeInfo(const Descriptor* descriptor, io::Printer* printer, bool last) { + if (IsMapEntryMessage(descriptor)) { + printer->Print("null, "); + return; + } + // Generated message type + printer->Print("new pbr::GeneratedCodeInfo(typeof($type_name$), ", "type_name", GetClassName(descriptor)); + + // Fields + if (descriptor->field_count() > 0) { + std::vector fields; + for (int i = 0; i < descriptor->field_count(); i++) { + fields.push_back(GetPropertyName(descriptor->field(i))); + } + printer->Print("new[]{ \"$fields$\" }, ", "fields", JoinStrings(fields, "\", \"")); + } + else { + printer->Print("null, "); + } + + // Oneofs + if (descriptor->oneof_decl_count() > 0) { + std::vector oneofs; + for (int i = 0; i < descriptor->oneof_decl_count(); i++) { + oneofs.push_back(UnderscoresToCamelCase(descriptor->oneof_decl(i)->name(), true)); + } + printer->Print("new[]{ \"$oneofs$\" }, ", "oneofs", JoinStrings(oneofs, "\", \"")); + } + else { + printer->Print("null, "); + } + + // Nested enums + if (descriptor->enum_type_count() > 0) { + std::vector enums; + for (int i = 0; i < descriptor->enum_type_count(); i++) { + enums.push_back(GetClassName(descriptor->enum_type(i))); + } + printer->Print("new[]{ typeof($enums$) }, ", "enums", JoinStrings(enums, "), typeof(")); + } + else { + printer->Print("null, "); + } + + // Nested types + if (descriptor->nested_type_count() > 0) { + // Need to specify array type explicitly here, as all elements may be null. + printer->Print("new pbr::GeneratedCodeInfo[] { "); + for (int i = 0; i < descriptor->nested_type_count(); i++) { + WriteGeneratedCodeInfo(descriptor->nested_type(i), printer, i == descriptor->nested_type_count() - 1); + } + printer->Print("}"); + } + else { + printer->Print("null"); + } + printer->Print(last ? ")" : "),\n"); } } // namespace csharp diff --git a/src/google/protobuf/compiler/csharp/csharp_umbrella_class.h b/src/google/protobuf/compiler/csharp/csharp_umbrella_class.h index db1092a9..b8bd2133 100644 --- a/src/google/protobuf/compiler/csharp/csharp_umbrella_class.h +++ b/src/google/protobuf/compiler/csharp/csharp_umbrella_class.h @@ -57,7 +57,7 @@ class UmbrellaClassGenerator : public SourceGeneratorBase { void WriteIntroduction(io::Printer* printer); void WriteDescriptor(io::Printer* printer); - void WriteTypeLiterals(const Descriptor* descriptor, io::Printer* printer); + void WriteGeneratedCodeInfo(const Descriptor* descriptor, io::Printer* printer, bool last); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UmbrellaClassGenerator); }; diff --git a/src/google/protobuf/compiler/csharp/csharp_wrapper_field.cc b/src/google/protobuf/compiler/csharp/csharp_wrapper_field.cc index d6cd0a10..75ef5e50 100644 --- a/src/google/protobuf/compiler/csharp/csharp_wrapper_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_wrapper_field.cc @@ -73,7 +73,6 @@ void WrapperFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, - "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ $type_name$ $property_name$ {\n" " get { return $name$_; }\n" " set {\n" @@ -170,7 +169,6 @@ void WrapperOneofFieldGenerator::GenerateMembers(io::Printer* printer) { AddDeprecatedFlag(printer); printer->Print( variables_, - "[pbr::ProtobufField($number$, \"$original_name$\")]\n" "$access_level$ $type_name$ $property_name$ {\n" " get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : ($type_name$) null; }\n" " set {\n" -- cgit v1.2.3 From 0dbd5ec80d33ea2a37f5362a24fd72b2c5f51aaa Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Thu, 23 Jul 2015 15:31:34 +0100 Subject: First attempt at using profile 259 for Google.Protobuf. This requires .NET 4.5, and there are a few compatibility changes required around reflection. Creating a PR from this to see how our CI systems handle it. Will want to add more documentation, validation and probably tests before merging. This is in aid of issue #590. --- csharp/src/AddressBook/AddressBook.csproj | 7 ++- csharp/src/AddressBook/app.config | 2 +- .../Google.Protobuf.JsonDump.csproj | 9 ++- csharp/src/Google.Protobuf.JsonDump/app.config | 2 +- csharp/src/Google.Protobuf/Collections/MapField.cs | 3 +- .../Google.Protobuf/Collections/RepeatedField.cs | 10 ++-- .../Compatibility/PropertyInfoExtensions.cs | 49 ++++++++++++++++ .../Compatibility/TypeExtensions.cs | 65 ++++++++++++++++++++++ csharp/src/Google.Protobuf/Google.Protobuf.csproj | 7 ++- .../Reflection/FieldAccessorBase.cs | 1 + .../Google.Protobuf/Reflection/FieldDescriptor.cs | 1 + .../Google.Protobuf/Reflection/OneofAccessor.cs | 1 + .../Google.Protobuf/Reflection/OneofDescriptor.cs | 3 +- .../Reflection/SingleFieldAccessor.cs | 1 + 14 files changed, 146 insertions(+), 15 deletions(-) create mode 100644 csharp/src/Google.Protobuf/Compatibility/PropertyInfoExtensions.cs create mode 100644 csharp/src/Google.Protobuf/Compatibility/TypeExtensions.cs (limited to 'csharp/src/Google.Protobuf/Google.Protobuf.csproj') diff --git a/csharp/src/AddressBook/AddressBook.csproj b/csharp/src/AddressBook/AddressBook.csproj index bae43b40..8f8ca7e2 100644 --- a/csharp/src/AddressBook/AddressBook.csproj +++ b/csharp/src/AddressBook/AddressBook.csproj @@ -10,10 +10,11 @@ Properties Google.Protobuf.Examples.AddressBook AddressBook - v4.0 + v4.5 512 Google.Protobuf.Examples.AddressBook.Program - Client + + true @@ -26,6 +27,7 @@ 4 true Off + false pdbonly @@ -37,6 +39,7 @@ 4 true Off + false diff --git a/csharp/src/AddressBook/app.config b/csharp/src/AddressBook/app.config index 19fac17a..a80813af 100644 --- a/csharp/src/AddressBook/app.config +++ b/csharp/src/AddressBook/app.config @@ -1,3 +1,3 @@ - + diff --git a/csharp/src/Google.Protobuf.JsonDump/Google.Protobuf.JsonDump.csproj b/csharp/src/Google.Protobuf.JsonDump/Google.Protobuf.JsonDump.csproj index c3c9d277..a040cda5 100644 --- a/csharp/src/Google.Protobuf.JsonDump/Google.Protobuf.JsonDump.csproj +++ b/csharp/src/Google.Protobuf.JsonDump/Google.Protobuf.JsonDump.csproj @@ -1,4 +1,4 @@ - + CLIENTPROFILE @@ -12,9 +12,10 @@ Properties Google.Protobuf.JsonDump Google.Protobuf.JsonDump - v4.0 + v4.5 512 - Client + + true @@ -27,6 +28,7 @@ 4 true Off + false pdbonly @@ -38,6 +40,7 @@ 4 true Off + false diff --git a/csharp/src/Google.Protobuf.JsonDump/app.config b/csharp/src/Google.Protobuf.JsonDump/app.config index e2a5a187..51278a45 100644 --- a/csharp/src/Google.Protobuf.JsonDump/app.config +++ b/csharp/src/Google.Protobuf.JsonDump/app.config @@ -1,3 +1,3 @@ - + diff --git a/csharp/src/Google.Protobuf/Collections/MapField.cs b/csharp/src/Google.Protobuf/Collections/MapField.cs index 9ca5104d..68f2f1cc 100644 --- a/csharp/src/Google.Protobuf/Collections/MapField.cs +++ b/csharp/src/Google.Protobuf/Collections/MapField.cs @@ -35,6 +35,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.Linq; +using Google.Protobuf.Compatibility; namespace Google.Protobuf.Collections { @@ -74,7 +75,7 @@ namespace Google.Protobuf.Collections /// Whether null values are permitted in the map or not. public MapField(bool allowNullValues) { - if (allowNullValues && typeof(TValue).IsValueType && Nullable.GetUnderlyingType(typeof(TValue)) == null) + if (allowNullValues && typeof(TValue).IsValueType() && Nullable.GetUnderlyingType(typeof(TValue)) == null) { throw new ArgumentException("allowNullValues", "Non-nullable value types do not support null values"); } diff --git a/csharp/src/Google.Protobuf/Collections/RepeatedField.cs b/csharp/src/Google.Protobuf/Collections/RepeatedField.cs index 9bab41ea..ccd1a9bb 100644 --- a/csharp/src/Google.Protobuf/Collections/RepeatedField.cs +++ b/csharp/src/Google.Protobuf/Collections/RepeatedField.cs @@ -29,10 +29,12 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion - + +using Google.Protobuf.Reflection; using System; using System.Collections; using System.Collections.Generic; +using Google.Protobuf.Compatibility; namespace Google.Protobuf.Collections { @@ -88,7 +90,7 @@ namespace Google.Protobuf.Collections uint tag = input.LastTag; var reader = codec.ValueReader; // Value types can be packed or not. - if (typeof(T).IsValueType && WireFormat.GetTagWireType(tag) == WireFormat.WireType.LengthDelimited) + if (typeof(T).IsValueType() && WireFormat.GetTagWireType(tag) == WireFormat.WireType.LengthDelimited) { int length = input.ReadLength(); if (length > 0) @@ -119,7 +121,7 @@ namespace Google.Protobuf.Collections return 0; } uint tag = codec.Tag; - if (typeof(T).IsValueType && WireFormat.GetTagWireType(tag) == WireFormat.WireType.LengthDelimited) + if (typeof(T).IsValueType() && WireFormat.GetTagWireType(tag) == WireFormat.WireType.LengthDelimited) { int dataSize = CalculatePackedDataSize(codec); return CodedOutputStream.ComputeRawVarint32Size(tag) + @@ -165,7 +167,7 @@ namespace Google.Protobuf.Collections } var writer = codec.ValueWriter; var tag = codec.Tag; - if (typeof(T).IsValueType && WireFormat.GetTagWireType(tag) == WireFormat.WireType.LengthDelimited) + if (typeof(T).IsValueType() && WireFormat.GetTagWireType(tag) == WireFormat.WireType.LengthDelimited) { // Packed primitive type uint size = (uint)CalculatePackedDataSize(codec); diff --git a/csharp/src/Google.Protobuf/Compatibility/PropertyInfoExtensions.cs b/csharp/src/Google.Protobuf/Compatibility/PropertyInfoExtensions.cs new file mode 100644 index 00000000..934424f8 --- /dev/null +++ b/csharp/src/Google.Protobuf/Compatibility/PropertyInfoExtensions.cs @@ -0,0 +1,49 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2015 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// 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. +#endregion + +using System.Reflection; + +namespace Google.Protobuf.Compatibility +{ + internal static class PropertyInfoExtensions + { + internal static MethodInfo GetGetMethod(this PropertyInfo target) + { + return target.GetMethod; + } + + internal static MethodInfo GetSetMethod(this PropertyInfo target) + { + return target.SetMethod; + } + } +} diff --git a/csharp/src/Google.Protobuf/Compatibility/TypeExtensions.cs b/csharp/src/Google.Protobuf/Compatibility/TypeExtensions.cs new file mode 100644 index 00000000..fbfb47fa --- /dev/null +++ b/csharp/src/Google.Protobuf/Compatibility/TypeExtensions.cs @@ -0,0 +1,65 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2015 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// 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. +#endregion + +using System; +using System.Reflection; + +namespace Google.Protobuf.Compatibility +{ + /// + /// Provides extension methods on Type that just proxy to TypeInfo. + /// These are used to support the new type system from .NET 4.5, without + /// having calls to GetTypeInfo all over the place. + /// + internal static class TypeExtensions + { + internal static bool IsValueType(this Type target) + { + return target.GetTypeInfo().IsValueType; + } + + internal static bool IsAssignableFrom(this Type target, Type c) + { + return target.GetTypeInfo().IsAssignableFrom(c.GetTypeInfo()); + } + + internal static PropertyInfo GetProperty(this Type target, string name) + { + return target.GetTypeInfo().GetDeclaredProperty(name); + } + + internal static MethodInfo GetMethod(this Type target, string name) + { + return target.GetTypeInfo().GetDeclaredMethod(name); + } + } +} diff --git a/csharp/src/Google.Protobuf/Google.Protobuf.csproj b/csharp/src/Google.Protobuf/Google.Protobuf.csproj index 29320ca8..b1f20816 100644 --- a/csharp/src/Google.Protobuf/Google.Protobuf.csproj +++ b/csharp/src/Google.Protobuf/Google.Protobuf.csproj @@ -11,8 +11,8 @@ Google.Protobuf Google.Protobuf {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - Profile92 - v4.0 + Profile259 + v4.5 512 true ..\..\keys\Google.Protobuf.snk @@ -60,6 +60,8 @@ + + @@ -111,6 +113,7 @@ +