diff options
Diffstat (limited to 'csharp/src/Google.Protobuf')
19 files changed, 299 insertions, 153 deletions
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 @@ <Compile Include="Reflection\EnumDescriptor.cs" />
<Compile Include="Reflection\EnumValueDescriptor.cs" />
<Compile Include="Reflection\FieldAccessorBase.cs" />
- <Compile Include="Reflection\FieldAccessorTable.cs" />
<Compile Include="Reflection\FieldDescriptor.cs" />
<Compile Include="Reflection\FieldType.cs" />
<Compile Include="Reflection\FileDescriptor.cs" />
@@ -91,6 +90,8 @@ <Compile Include="Reflection\OneofDescriptor.cs" />
<Compile Include="Reflection\PackageDescriptor.cs" />
<Compile Include="Reflection\PartialClasses.cs" />
+ <Compile Include="Reflection\ProtobufOneofAttribute.cs" />
+ <Compile Include="Reflection\ProtobufFieldAttribute.cs" />
<Compile Include="Reflection\ReflectionUtil.cs" />
<Compile Include="Reflection\RepeatedFieldAccessor.cs" />
<Compile Include="Reflection\ServiceDescriptor.cs" />
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 @@ -40,15 +40,6 @@ namespace Google.Protobuf // TODO(jonskeet): Split these interfaces into separate files when we're happy with them.
/// <summary>
- /// Reflection support for accessing field values.
- /// </summary>
- public interface IReflectedMessage : IMessage
- {
- FieldAccessorTable Fields { get; }
- // TODO(jonskeet): Descriptor? Or a single property which has "all you need for reflection"?
- }
-
- /// <summary>
/// Interface for a Protocol Buffers message, supporting
/// basic operations required for serialization.
/// </summary>
@@ -73,6 +64,13 @@ namespace Google.Protobuf /// <returns>The number of bytes required to write this message
/// to a coded output stream.</returns>
int CalculateSize();
+
+ /// <summary>
+ /// 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.
+ /// </summary>
+ MessageDescriptor Descriptor { get; }
}
/// <summary>
@@ -81,7 +79,7 @@ namespace Google.Protobuf /// the implementation class.
/// </summary>
/// <typeparam name="T">The message type.</typeparam>
- public interface IMessage<T> : IReflectedMessage, IEquatable<T>, IDeepCloneable<T>, IFreezable where T : IMessage<T>
+ public interface IMessage<T> : IMessage, IEquatable<T>, IDeepCloneable<T>, IFreezable where T : IMessage<T>
{
/// <summary>
/// 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. /// </summary> - internal static IList<TOutput> ConvertAndMakeReadOnly<TInput, TOutput>(IList<TInput> input, - IndexedConverter<TInput, TOutput> - converter) + internal static IList<TOutput> ConvertAndMakeReadOnly<TInput, TOutput> + (IList<TInput> input, IndexedConverter<TInput, TOutput> 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<EnumValueDescriptor> 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 /// </summary> public override string Name { get { return proto.Name; } } + /// <summary> + /// The generated type for this enum, or <c>null</c> if the descriptor does not represent a generated type. + /// </summary> + public Type GeneratedType { get { return generatedType; } } + /// <value> /// If this is a nested type, get the outer descriptor, otherwise null. /// </value> 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<object, object> 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 -{ - /// <summary> - /// Provides access to fields in generated messages via reflection. - /// </summary> - public sealed class FieldAccessorTable - { - private readonly ReadOnlyCollection<IFieldAccessor> accessors; - private readonly ReadOnlyCollection<OneofAccessor> oneofs; - private readonly MessageDescriptor descriptor; - - /// <summary> - /// Constructs a FieldAccessorTable for a particular message class. - /// Only one FieldAccessorTable should be constructed per class. - /// </summary> - /// <param name="type">The CLR type for the message.</param> - /// <param name="descriptor">The type's descriptor</param> - /// <param name="propertyNames">The Pascal-case names of all the field-based properties in the message.</param> - 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<IFieldAccessor>(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<OneofAccessor>(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. - /// <summary> - /// Returns all of the field accessors for the message type. - /// </summary> - public ReadOnlyCollection<IFieldAccessor> Accessors { get { return accessors; } } - - public ReadOnlyCollection<OneofAccessor> 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; } } /// <summary> /// 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<ProtobufFieldAttribute>().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>((FileDescriptor[]) dependencies.Clone()); + IEnumerator<Type> generatedTypeIterator = generatedTypes == null ? null : ((IEnumerable<Type>)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"); + } } /// <summary> @@ -265,7 +272,7 @@ namespace Google.Protobuf.Reflection /// <exception cref="DescriptorValidationException">If <paramref name="proto"/> 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.</exception> - 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 } } + /// <summary> + /// Creates an instance for generated code. + /// </summary> + /// <remarks> + /// The <paramref name="generatedTypes"/> 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. + /// </remarks> 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 /// </summary> 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<EnumDescriptor> enumTypes; private readonly IList<FieldDescriptor> fields; private readonly IList<OneofDescriptor> oneofs; + // CLR representation of the type described by this descriptor, if any. + private readonly Type generatedType; + private IDictionary<int, IFieldAccessor> fieldAccessorsByFieldNumber; - internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex) + internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex, IEnumerator<Type> 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); } + + /// <summary> + /// Returns the total number of nested types and enums, recursively. + /// </summary> + private int CountTotalGeneratedTypes() + { + return nestedTypes.Sum(nested => nested.CountTotalGeneratedTypes()) + enumTypes.Count; + } /// <summary> /// The brief name of the descriptor's target. @@ -94,6 +108,11 @@ namespace Google.Protobuf.Reflection internal DescriptorProto Proto { get { return proto; } } /// <summary> + /// The generated type for this message, or <c>null</c> if the descriptor does not represent a generated type. + /// </summary> + public Type GeneratedType { get { return generatedType; } } + + /// <summary> /// Returns whether this message is one of the "well known types" which may have runtime/protoc support. /// </summary> internal bool IsWellKnownType @@ -142,6 +161,13 @@ namespace Google.Protobuf.Reflection } /// <summary> + /// 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. + /// </summary> + public IDictionary<int, IFieldAccessor> FieldAccessorsByFieldNumber { get { return fieldAccessorsByFieldNumber; } } + + /// <summary> /// Finds a field by field name. /// </summary> /// <param name="name">The unqualified name of the field (e.g. "foo").</param> @@ -192,6 +218,8 @@ namespace Google.Protobuf.Reflection { oneof.CrossLink(); } + + fieldAccessorsByFieldNumber = new ReadOnlyDictionary<int, IFieldAccessor>(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<object> 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<int>(property.GetGetMethod()); + caseDelegate = ReflectionUtil.CreateFuncObjectT<int>(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<FieldDescriptor> 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<FieldDescriptor> Fields { get { return fields; } } + public OneofAccessor Accessor { get { return accessor; } } + internal void CrossLink() { List<FieldDescriptor> fieldCollection = new List<FieldDescriptor>(); @@ -73,6 +77,36 @@ namespace Google.Protobuf.Reflection } } fields = new ReadOnlyCollection<FieldDescriptor>(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<ProtobufOneofAttribute>().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<ProtobufOneofAttribute>().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 +{ + /// <summary> + /// Attribute applied to a generated property corresponding to a field in a .proto file. + /// </summary> + [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] + public sealed class ProtobufFieldAttribute : Attribute + { + /// <summary> + /// The field number in the original .proto file. + /// </summary> + public int Number { get; set; } + + /// <summary> + /// The field name in the original .proto file. + /// </summary> + 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 +{ + /// <summary> + /// Attribute applied to the "case" property or "clear" method corresponding to a oneof in a .proto file. + /// </summary> + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = false)] + public sealed class ProtobufOneofAttribute : Attribute + { + /// <summary> + /// The oneof name in the original .proto file. + /// </summary> + 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<Action<object>>(call, targetParameter).Compile(); } + + /// <summary> + /// Returns the next type from an iterator of types, unless the iterator is a null reference, + /// in which case null is returned. + /// </summary> + internal static Type GetNextType(IEnumerator<Type> 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 /// </summary> 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<object, object> setValueDelegate; private readonly Action<object> 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"); |