From 68380f0f6681c72a5d4ab5e69abc9670e9d99838 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Thu, 30 Jul 2015 13:03:45 +0100 Subject: [PATCH 1/3] Rename ThrowHelper to Preconditions and make it public - we'll want to use it from the generated code soon. Additionally, change it to return the value passed, and make it generic with a class constraint. A separate method doesn't have the class constraint, for more unusual scenarios. --- Makefile.am | 2 +- .../Google.Protobuf/Collections/MapField.cs | 16 +-- .../Google.Protobuf/Google.Protobuf.csproj | 2 +- csharp/src/Google.Protobuf/JsonFormatter.cs | 2 +- .../src/Google.Protobuf/MessageExtensions.cs | 28 ++-- csharp/src/Google.Protobuf/MessageParser.cs | 4 +- .../{ThrowHelper.cs => Preconditions.cs} | 125 ++++++++++-------- 7 files changed, 100 insertions(+), 79 deletions(-) rename csharp/src/Google.Protobuf/{ThrowHelper.cs => Preconditions.cs} (66%) diff --git a/Makefile.am b/Makefile.am index 3c0cea5ac5..b5868b88f5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -107,6 +107,7 @@ csharp_EXTRA_DIST= \ csharp/src/Google.Protobuf/LimitedInputStream.cs \ csharp/src/Google.Protobuf/MessageExtensions.cs \ csharp/src/Google.Protobuf/MessageParser.cs \ + csharp/src/Google.Protobuf/Preconditions.cs \ csharp/src/Google.Protobuf/Properties/AssemblyInfo.cs \ csharp/src/Google.Protobuf/Reflection/DescriptorBase.cs \ csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs \ @@ -133,7 +134,6 @@ csharp_EXTRA_DIST= \ csharp/src/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs \ csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs \ csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs \ - csharp/src/Google.Protobuf/ThrowHelper.cs \ csharp/src/Google.Protobuf/WellKnownTypes/Any.cs \ csharp/src/Google.Protobuf/WellKnownTypes/Api.cs \ csharp/src/Google.Protobuf/WellKnownTypes/Duration.cs \ diff --git a/csharp/src/Google.Protobuf/Collections/MapField.cs b/csharp/src/Google.Protobuf/Collections/MapField.cs index 9f460e1e13..fc94fd5cd9 100644 --- a/csharp/src/Google.Protobuf/Collections/MapField.cs +++ b/csharp/src/Google.Protobuf/Collections/MapField.cs @@ -112,13 +112,13 @@ namespace Google.Protobuf.Collections public bool ContainsKey(TKey key) { - ThrowHelper.ThrowIfNull(key, "key"); + Preconditions.CheckNotNullUnconstrained(key, "key"); return map.ContainsKey(key); } public bool Remove(TKey key) { - ThrowHelper.ThrowIfNull(key, "key"); + Preconditions.CheckNotNullUnconstrained(key, "key"); LinkedListNode> node; if (map.TryGetValue(key, out node)) { @@ -151,7 +151,7 @@ namespace Google.Protobuf.Collections { get { - ThrowHelper.ThrowIfNull(key, "key"); + Preconditions.CheckNotNullUnconstrained(key, "key"); TValue value; if (TryGetValue(key, out value)) { @@ -161,11 +161,11 @@ namespace Google.Protobuf.Collections } set { - ThrowHelper.ThrowIfNull(key, "key"); + Preconditions.CheckNotNullUnconstrained(key, "key"); // value == null check here is redundant, but avoids boxing. if (value == null && !allowNullValues) { - ThrowHelper.ThrowIfNull(value, "value"); + Preconditions.CheckNotNullUnconstrained(value, "value"); } LinkedListNode> node; var pair = new KeyValuePair(key, value); @@ -187,7 +187,7 @@ namespace Google.Protobuf.Collections public void Add(IDictionary entries) { - ThrowHelper.ThrowIfNull(entries, "entries"); + Preconditions.CheckNotNull(entries, "entries"); foreach (var pair in entries) { Add(pair.Key, pair.Value); @@ -374,7 +374,7 @@ namespace Google.Protobuf.Collections void IDictionary.Remove(object key) { - ThrowHelper.ThrowIfNull(key, "key"); + Preconditions.CheckNotNull(key, "key"); if (!(key is TKey)) { return; @@ -403,7 +403,7 @@ namespace Google.Protobuf.Collections { get { - ThrowHelper.ThrowIfNull(key, "key"); + Preconditions.CheckNotNull(key, "key"); if (!(key is TKey)) { return null; diff --git a/csharp/src/Google.Protobuf/Google.Protobuf.csproj b/csharp/src/Google.Protobuf/Google.Protobuf.csproj index b141e4342c..7de4f51b6c 100644 --- a/csharp/src/Google.Protobuf/Google.Protobuf.csproj +++ b/csharp/src/Google.Protobuf/Google.Protobuf.csproj @@ -96,7 +96,7 @@ - + diff --git a/csharp/src/Google.Protobuf/JsonFormatter.cs b/csharp/src/Google.Protobuf/JsonFormatter.cs index f624b09016..438af4df1b 100644 --- a/csharp/src/Google.Protobuf/JsonFormatter.cs +++ b/csharp/src/Google.Protobuf/JsonFormatter.cs @@ -120,7 +120,7 @@ namespace Google.Protobuf public string Format(IMessage message) { - ThrowHelper.ThrowIfNull(message, "message"); + Preconditions.CheckNotNull(message, "message"); StringBuilder builder = new StringBuilder(); // TODO(jonskeet): Handle well-known types here. // Our reflection support needs improving so that we can get at the descriptor diff --git a/csharp/src/Google.Protobuf/MessageExtensions.cs b/csharp/src/Google.Protobuf/MessageExtensions.cs index ee2863d12a..7bd79930fe 100644 --- a/csharp/src/Google.Protobuf/MessageExtensions.cs +++ b/csharp/src/Google.Protobuf/MessageExtensions.cs @@ -41,8 +41,8 @@ namespace Google.Protobuf { public static void MergeFrom(this IMessage message, byte[] data) { - ThrowHelper.ThrowIfNull(message, "message"); - ThrowHelper.ThrowIfNull(data, "data"); + Preconditions.CheckNotNull(message, "message"); + Preconditions.CheckNotNull(data, "data"); CodedInputStream input = CodedInputStream.CreateInstance(data); message.MergeFrom(input); input.CheckLastTagWas(0); @@ -50,8 +50,8 @@ namespace Google.Protobuf public static void MergeFrom(this IMessage message, ByteString data) { - ThrowHelper.ThrowIfNull(message, "message"); - ThrowHelper.ThrowIfNull(data, "data"); + Preconditions.CheckNotNull(message, "message"); + Preconditions.CheckNotNull(data, "data"); CodedInputStream input = data.CreateCodedInput(); message.MergeFrom(input); input.CheckLastTagWas(0); @@ -59,8 +59,8 @@ namespace Google.Protobuf public static void MergeFrom(this IMessage message, Stream input) { - ThrowHelper.ThrowIfNull(message, "message"); - ThrowHelper.ThrowIfNull(input, "input"); + Preconditions.CheckNotNull(message, "message"); + Preconditions.CheckNotNull(input, "input"); CodedInputStream codedInput = CodedInputStream.CreateInstance(input); message.MergeFrom(codedInput); codedInput.CheckLastTagWas(0); @@ -68,8 +68,8 @@ namespace Google.Protobuf public static void MergeDelimitedFrom(this IMessage message, Stream input) { - ThrowHelper.ThrowIfNull(message, "message"); - ThrowHelper.ThrowIfNull(input, "input"); + Preconditions.CheckNotNull(message, "message"); + Preconditions.CheckNotNull(input, "input"); int size = (int) CodedInputStream.ReadRawVarint32(input); Stream limitedStream = new LimitedInputStream(input, size); message.MergeFrom(limitedStream); @@ -77,7 +77,7 @@ namespace Google.Protobuf public static byte[] ToByteArray(this IMessage message) { - ThrowHelper.ThrowIfNull(message, "message"); + Preconditions.CheckNotNull(message, "message"); byte[] result = new byte[message.CalculateSize()]; CodedOutputStream output = CodedOutputStream.CreateInstance(result); message.WriteTo(output); @@ -87,8 +87,8 @@ namespace Google.Protobuf public static void WriteTo(this IMessage message, Stream output) { - ThrowHelper.ThrowIfNull(message, "message"); - ThrowHelper.ThrowIfNull(output, "output"); + Preconditions.CheckNotNull(message, "message"); + Preconditions.CheckNotNull(output, "output"); CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output); message.WriteTo(codedOutput); codedOutput.Flush(); @@ -96,8 +96,8 @@ namespace Google.Protobuf public static void WriteDelimitedTo(this IMessage message, Stream output) { - ThrowHelper.ThrowIfNull(message, "message"); - ThrowHelper.ThrowIfNull(output, "output"); + Preconditions.CheckNotNull(message, "message"); + Preconditions.CheckNotNull(output, "output"); CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output); codedOutput.WriteRawVarint32((uint)message.CalculateSize()); message.WriteTo(codedOutput); @@ -106,7 +106,7 @@ namespace Google.Protobuf public static ByteString ToByteString(this IMessage message) { - ThrowHelper.ThrowIfNull(message, "message"); + Preconditions.CheckNotNull(message, "message"); return ByteString.AttachBytes(message.ToByteArray()); } } diff --git a/csharp/src/Google.Protobuf/MessageParser.cs b/csharp/src/Google.Protobuf/MessageParser.cs index 5407de06e2..bfa63ae586 100644 --- a/csharp/src/Google.Protobuf/MessageParser.cs +++ b/csharp/src/Google.Protobuf/MessageParser.cs @@ -84,7 +84,7 @@ namespace Google.Protobuf /// The newly parsed message. public T ParseFrom(byte[] data) { - ThrowHelper.ThrowIfNull(data, "data"); + Preconditions.CheckNotNull(data, "data"); T message = factory(); message.MergeFrom(data); return message; @@ -92,7 +92,7 @@ namespace Google.Protobuf public T ParseFrom(ByteString data) { - ThrowHelper.ThrowIfNull(data, "data"); + Preconditions.CheckNotNull(data, "data"); T message = factory(); message.MergeFrom(data); return message; diff --git a/csharp/src/Google.Protobuf/ThrowHelper.cs b/csharp/src/Google.Protobuf/Preconditions.cs similarity index 66% rename from csharp/src/Google.Protobuf/ThrowHelper.cs rename to csharp/src/Google.Protobuf/Preconditions.cs index 62d9ea602c..ff3bd0f146 100644 --- a/csharp/src/Google.Protobuf/ThrowHelper.cs +++ b/csharp/src/Google.Protobuf/Preconditions.cs @@ -1,53 +1,74 @@ -#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; - -namespace Google.Protobuf -{ - /// - /// Helper methods for throwing exceptions - /// - internal static class ThrowHelper - { - /// - /// Throws an ArgumentNullException if the given value is null. - /// - internal static void ThrowIfNull(object value, string name) - { - if (value == null) - { - throw new ArgumentNullException(name); - } - } - } +#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; + +namespace Google.Protobuf +{ + /// + /// Helper methods for throwing exceptions + /// + public static class Preconditions + { + /// + /// Throws an ArgumentNullException if the given value is null, otherwise + /// return the value to the caller. + /// + public static T CheckNotNull(T value, string name) where T : class + { + if (value == null) + { + throw new ArgumentNullException(name); + } + return value; + } + + /// + /// Throws an ArgumentNullException if the given value is null, otherwise + /// return the value to the caller. + /// + /// + /// This is equivalent to but without the type parameter + /// constraint. In most cases, the constraint is useful to prevent you from calling CheckNotNull + /// with a value type - but it gets in the way if either you want to use it with a nullable + /// value type, or you want to use it with an unconstrained type parameter. + /// + internal static T CheckNotNullUnconstrained(T value, string name) + { + if (value == null) + { + throw new ArgumentNullException(name); + } + return value; + } + } } \ No newline at end of file From 3980cf9df1ca9c66718f1fe9817011134f87bbcd Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Thu, 30 Jul 2015 13:35:42 +0100 Subject: [PATCH 2/3] Prohibit null values for string/bytes fields in generated code. --- .../src/Google.Protobuf.Test/GeneratedMessageTest.cs | 10 ++++++++++ .../protobuf/compiler/csharp/csharp_field_base.cc | 11 ----------- .../compiler/csharp/csharp_primitive_field.cc | 4 ++-- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs b/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs index dfaac48cd0..140b878c15 100644 --- a/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs +++ b/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs @@ -111,6 +111,16 @@ namespace Google.Protobuf Assert.IsNull(message.OneofNestedMessage); } + [Test] + public void NullStringAndBytesRejected() + { + var message = new TestAllTypes(); + Assert.Throws(() => message.SingleString = null); + Assert.Throws(() => message.OneofString = null); + Assert.Throws(() => message.SingleBytes = null); + Assert.Throws(() => message.OneofBytes = null); + } + [Test] public void RoundTrip_Empty() { diff --git a/src/google/protobuf/compiler/csharp/csharp_field_base.cc b/src/google/protobuf/compiler/csharp/csharp_field_base.cc index 2459d457ed..7e3bbeef51 100644 --- a/src/google/protobuf/compiler/csharp/csharp_field_base.cc +++ b/src/google/protobuf/compiler/csharp/csharp_field_base.cc @@ -127,17 +127,6 @@ void FieldGeneratorBase::AddDeprecatedFlag(io::Printer* printer) { } } -void FieldGeneratorBase::AddNullCheck(io::Printer* printer) { - AddNullCheck(printer, "value"); -} - -void FieldGeneratorBase::AddNullCheck(io::Printer* printer, const std::string& name) { - if (is_nullable_type()) { - printer->Print(" pb::ThrowHelper.ThrowIfNull($name$, \"$name$\");\n", - "name", name); - } -} - void FieldGeneratorBase::AddPublicMemberAttributes(io::Printer* printer) { AddDeprecatedFlag(printer); } diff --git a/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc b/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc index 80f4a92d02..fc043ec06c 100644 --- a/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc @@ -81,7 +81,7 @@ void PrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) { } else { printer->Print( variables_, - " $name$_ = value ?? $default_value$;\n"); + " $name$_ = pb::Preconditions.CheckNotNull(value, \"value\");\n"); } printer->Print( " }\n" @@ -183,7 +183,7 @@ void PrimitiveOneofFieldGenerator::GenerateMembers(io::Printer* printer) { } else { printer->Print( variables_, - " $oneof_name$_ = value ?? $default_value$;\n"); + " $oneof_name$_ = pb::Preconditions.CheckNotNull(value, \"value\");\n"); } printer->Print( variables_, From d18cc0845606c32e01e044ae5411c7053b1c73ec Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Thu, 30 Jul 2015 13:50:02 +0100 Subject: [PATCH 3/3] Generated code for previous commit. --- csharp/src/AddressBook/Addressbook.cs | 6 +-- .../TestProtos/UnittestProto3.cs | 22 ++++---- .../Reflection/DescriptorProtoFile.cs | 52 +++++++++---------- .../src/Google.Protobuf/WellKnownTypes/Any.cs | 4 +- .../src/Google.Protobuf/WellKnownTypes/Api.cs | 10 ++-- .../WellKnownTypes/SourceContext.cs | 2 +- .../Google.Protobuf/WellKnownTypes/Struct.cs | 2 +- .../Google.Protobuf/WellKnownTypes/Type.cs | 12 ++--- .../WellKnownTypes/Wrappers.cs | 4 +- 9 files changed, 57 insertions(+), 57 deletions(-) diff --git a/csharp/src/AddressBook/Addressbook.cs b/csharp/src/AddressBook/Addressbook.cs index 92f983e7c7..25752e20bf 100644 --- a/csharp/src/AddressBook/Addressbook.cs +++ b/csharp/src/AddressBook/Addressbook.cs @@ -76,7 +76,7 @@ namespace Google.Protobuf.Examples.AddressBook { public string Name { get { return name_; } set { - name_ = value ?? ""; + name_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -94,7 +94,7 @@ namespace Google.Protobuf.Examples.AddressBook { public string Email { get { return email_; } set { - email_ = value ?? ""; + email_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -257,7 +257,7 @@ namespace Google.Protobuf.Examples.AddressBook { public string Number { get { return number_; } set { - number_ = value ?? ""; + number_ = pb::Preconditions.CheckNotNull(value, "value"); } } diff --git a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestProto3.cs b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestProto3.cs index 1f2e9229af..e360ac15a7 100644 --- a/csharp/src/Google.Protobuf.Test/TestProtos/UnittestProto3.cs +++ b/csharp/src/Google.Protobuf.Test/TestProtos/UnittestProto3.cs @@ -426,7 +426,7 @@ namespace Google.Protobuf.TestProtos { public string SingleString { get { return singleString_; } set { - singleString_ = value ?? ""; + singleString_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -435,7 +435,7 @@ namespace Google.Protobuf.TestProtos { public pb::ByteString SingleBytes { get { return singleBytes_; } set { - singleBytes_ = value ?? pb::ByteString.Empty; + singleBytes_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -700,7 +700,7 @@ namespace Google.Protobuf.TestProtos { public string OneofString { get { return oneofFieldCase_ == OneofFieldOneofCase.OneofString ? (string) oneofField_ : ""; } set { - oneofField_ = value ?? ""; + oneofField_ = pb::Preconditions.CheckNotNull(value, "value"); oneofFieldCase_ = OneofFieldOneofCase.OneofString; } } @@ -709,7 +709,7 @@ namespace Google.Protobuf.TestProtos { public pb::ByteString OneofBytes { get { return oneofFieldCase_ == OneofFieldOneofCase.OneofBytes ? (pb::ByteString) oneofField_ : pb::ByteString.Empty; } set { - oneofField_ = value ?? pb::ByteString.Empty; + oneofField_ = pb::Preconditions.CheckNotNull(value, "value"); oneofFieldCase_ = OneofFieldOneofCase.OneofBytes; } } @@ -2703,7 +2703,7 @@ namespace Google.Protobuf.TestProtos { public string StringField { get { return stringField_; } set { - stringField_ = value ?? ""; + stringField_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -2954,7 +2954,7 @@ namespace Google.Protobuf.TestProtos { public string MyString { get { return myString_; } set { - myString_ = value ?? ""; + myString_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -3385,7 +3385,7 @@ namespace Google.Protobuf.TestProtos { public string Data { get { return data_; } set { - data_ = value ?? ""; + data_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -3589,7 +3589,7 @@ namespace Google.Protobuf.TestProtos { public pb::ByteString Data { get { return data_; } set { - data_ = value ?? pb::ByteString.Empty; + data_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -3695,7 +3695,7 @@ namespace Google.Protobuf.TestProtos { public pb::ByteString Data { get { return data_; } set { - data_ = value ?? pb::ByteString.Empty; + data_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -4350,7 +4350,7 @@ namespace Google.Protobuf.TestProtos { public string FooString { get { return fooCase_ == FooOneofCase.FooString ? (string) foo_ : ""; } set { - foo_ = value ?? ""; + foo_ = pb::Preconditions.CheckNotNull(value, "value"); fooCase_ = FooOneofCase.FooString; } } @@ -5409,7 +5409,7 @@ namespace Google.Protobuf.TestProtos { public string A { get { return a_; } set { - a_ = value ?? ""; + a_ = pb::Preconditions.CheckNotNull(value, "value"); } } diff --git a/csharp/src/Google.Protobuf/Reflection/DescriptorProtoFile.cs b/csharp/src/Google.Protobuf/Reflection/DescriptorProtoFile.cs index 213863e20c..e40472cbc4 100644 --- a/csharp/src/Google.Protobuf/Reflection/DescriptorProtoFile.cs +++ b/csharp/src/Google.Protobuf/Reflection/DescriptorProtoFile.cs @@ -301,7 +301,7 @@ namespace Google.Protobuf.Reflection { internal string Name { get { return name_; } set { - name_ = value ?? ""; + name_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -310,7 +310,7 @@ namespace Google.Protobuf.Reflection { internal string Package { get { return package_; } set { - package_ = value ?? ""; + package_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -393,7 +393,7 @@ namespace Google.Protobuf.Reflection { internal string Syntax { get { return syntax_; } set { - syntax_ = value ?? ""; + syntax_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -649,7 +649,7 @@ namespace Google.Protobuf.Reflection { internal string Name { get { return name_; } set { - name_ = value ?? ""; + name_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -1200,7 +1200,7 @@ namespace Google.Protobuf.Reflection { internal string Name { get { return name_; } set { - name_ = value ?? ""; + name_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -1236,7 +1236,7 @@ namespace Google.Protobuf.Reflection { internal string TypeName { get { return typeName_; } set { - typeName_ = value ?? ""; + typeName_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -1245,7 +1245,7 @@ namespace Google.Protobuf.Reflection { internal string Extendee { get { return extendee_; } set { - extendee_ = value ?? ""; + extendee_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -1254,7 +1254,7 @@ namespace Google.Protobuf.Reflection { internal string DefaultValue { get { return defaultValue_; } set { - defaultValue_ = value ?? ""; + defaultValue_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -1545,7 +1545,7 @@ namespace Google.Protobuf.Reflection { internal string Name { get { return name_; } set { - name_ = value ?? ""; + name_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -1653,7 +1653,7 @@ namespace Google.Protobuf.Reflection { internal string Name { get { return name_; } set { - name_ = value ?? ""; + name_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -1809,7 +1809,7 @@ namespace Google.Protobuf.Reflection { internal string Name { get { return name_; } set { - name_ = value ?? ""; + name_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -1973,7 +1973,7 @@ namespace Google.Protobuf.Reflection { internal string Name { get { return name_; } set { - name_ = value ?? ""; + name_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -2132,7 +2132,7 @@ namespace Google.Protobuf.Reflection { internal string Name { get { return name_; } set { - name_ = value ?? ""; + name_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -2141,7 +2141,7 @@ namespace Google.Protobuf.Reflection { internal string InputType { get { return inputType_; } set { - inputType_ = value ?? ""; + inputType_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -2150,7 +2150,7 @@ namespace Google.Protobuf.Reflection { internal string OutputType { get { return outputType_; } set { - outputType_ = value ?? ""; + outputType_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -2383,7 +2383,7 @@ namespace Google.Protobuf.Reflection { internal string JavaPackage { get { return javaPackage_; } set { - javaPackage_ = value ?? ""; + javaPackage_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -2392,7 +2392,7 @@ namespace Google.Protobuf.Reflection { internal string JavaOuterClassname { get { return javaOuterClassname_; } set { - javaOuterClassname_ = value ?? ""; + javaOuterClassname_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -2437,7 +2437,7 @@ namespace Google.Protobuf.Reflection { internal string GoPackage { get { return goPackage_; } set { - goPackage_ = value ?? ""; + goPackage_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -2491,7 +2491,7 @@ namespace Google.Protobuf.Reflection { internal string ObjcClassPrefix { get { return objcClassPrefix_; } set { - objcClassPrefix_ = value ?? ""; + objcClassPrefix_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -2500,7 +2500,7 @@ namespace Google.Protobuf.Reflection { internal string CsharpNamespace { get { return csharpNamespace_; } set { - csharpNamespace_ = value ?? ""; + csharpNamespace_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -3853,7 +3853,7 @@ namespace Google.Protobuf.Reflection { internal string IdentifierValue { get { return identifierValue_; } set { - identifierValue_ = value ?? ""; + identifierValue_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -3889,7 +3889,7 @@ namespace Google.Protobuf.Reflection { internal pb::ByteString StringValue { get { return stringValue_; } set { - stringValue_ = value ?? pb::ByteString.Empty; + stringValue_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -3898,7 +3898,7 @@ namespace Google.Protobuf.Reflection { internal string AggregateValue { get { return aggregateValue_; } set { - aggregateValue_ = value ?? ""; + aggregateValue_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -4095,7 +4095,7 @@ namespace Google.Protobuf.Reflection { internal string NamePart_ { get { return namePart_; } set { - namePart_ = value ?? ""; + namePart_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -4350,7 +4350,7 @@ namespace Google.Protobuf.Reflection { internal string LeadingComments { get { return leadingComments_; } set { - leadingComments_ = value ?? ""; + leadingComments_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -4359,7 +4359,7 @@ namespace Google.Protobuf.Reflection { internal string TrailingComments { get { return trailingComments_; } set { - trailingComments_ = value ?? ""; + trailingComments_ = pb::Preconditions.CheckNotNull(value, "value"); } } diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/Any.cs b/csharp/src/Google.Protobuf/WellKnownTypes/Any.cs index 86c8552fef..9339587161 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/Any.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/Any.cs @@ -71,7 +71,7 @@ namespace Google.Protobuf.WellKnownTypes { public string TypeUrl { get { return typeUrl_; } set { - typeUrl_ = value ?? ""; + typeUrl_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -80,7 +80,7 @@ namespace Google.Protobuf.WellKnownTypes { public pb::ByteString Value { get { return value_; } set { - value_ = value ?? pb::ByteString.Empty; + value_ = pb::Preconditions.CheckNotNull(value, "value"); } } diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/Api.cs b/csharp/src/Google.Protobuf/WellKnownTypes/Api.cs index a8d4942a4e..366bba2183 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/Api.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/Api.cs @@ -84,7 +84,7 @@ namespace Google.Protobuf.WellKnownTypes { public string Name { get { return name_; } set { - name_ = value ?? ""; + name_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -109,7 +109,7 @@ namespace Google.Protobuf.WellKnownTypes { public string Version { get { return version_; } set { - version_ = value ?? ""; + version_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -285,7 +285,7 @@ namespace Google.Protobuf.WellKnownTypes { public string Name { get { return name_; } set { - name_ = value ?? ""; + name_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -294,7 +294,7 @@ namespace Google.Protobuf.WellKnownTypes { public string RequestTypeUrl { get { return requestTypeUrl_; } set { - requestTypeUrl_ = value ?? ""; + requestTypeUrl_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -312,7 +312,7 @@ namespace Google.Protobuf.WellKnownTypes { public string ResponseTypeUrl { get { return responseTypeUrl_; } set { - responseTypeUrl_ = value ?? ""; + responseTypeUrl_ = pb::Preconditions.CheckNotNull(value, "value"); } } diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/SourceContext.cs b/csharp/src/Google.Protobuf/WellKnownTypes/SourceContext.cs index c86dbadaa1..04fb6350f6 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/SourceContext.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/SourceContext.cs @@ -71,7 +71,7 @@ namespace Google.Protobuf.WellKnownTypes { public string FileName { get { return fileName_; } set { - fileName_ = value ?? ""; + fileName_ = pb::Preconditions.CheckNotNull(value, "value"); } } diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/Struct.cs b/csharp/src/Google.Protobuf/WellKnownTypes/Struct.cs index a4411dd010..15dd6dcd8e 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/Struct.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/Struct.cs @@ -224,7 +224,7 @@ namespace Google.Protobuf.WellKnownTypes { public string StringValue { get { return kindCase_ == KindOneofCase.StringValue ? (string) kind_ : ""; } set { - kind_ = value ?? ""; + kind_ = pb::Preconditions.CheckNotNull(value, "value"); kindCase_ = KindOneofCase.StringValue; } } diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/Type.cs b/csharp/src/Google.Protobuf/WellKnownTypes/Type.cs index 91ac0f6b10..6414293485 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/Type.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/Type.cs @@ -105,7 +105,7 @@ namespace Google.Protobuf.WellKnownTypes { public string Name { get { return name_; } set { - name_ = value ?? ""; + name_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -327,7 +327,7 @@ namespace Google.Protobuf.WellKnownTypes { public string Name { get { return name_; } set { - name_ = value ?? ""; + name_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -336,7 +336,7 @@ namespace Google.Protobuf.WellKnownTypes { public string TypeUrl { get { return typeUrl_; } set { - typeUrl_ = value ?? ""; + typeUrl_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -610,7 +610,7 @@ namespace Google.Protobuf.WellKnownTypes { public string Name { get { return name_; } set { - name_ = value ?? ""; + name_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -783,7 +783,7 @@ namespace Google.Protobuf.WellKnownTypes { public string Name { get { return name_; } set { - name_ = value ?? ""; + name_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -932,7 +932,7 @@ namespace Google.Protobuf.WellKnownTypes { public string Name { get { return name_; } set { - name_ = value ?? ""; + name_ = pb::Preconditions.CheckNotNull(value, "value"); } } diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/Wrappers.cs b/csharp/src/Google.Protobuf/WellKnownTypes/Wrappers.cs index a5f6b7e8d4..c9dd6ea650 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/Wrappers.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/Wrappers.cs @@ -822,7 +822,7 @@ namespace Google.Protobuf.WellKnownTypes { public string Value { get { return value_; } set { - value_ = value ?? ""; + value_ = pb::Preconditions.CheckNotNull(value, "value"); } } @@ -928,7 +928,7 @@ namespace Google.Protobuf.WellKnownTypes { public pb::ByteString Value { get { return value_; } set { - value_ = value ?? pb::ByteString.Empty; + value_ = pb::Preconditions.CheckNotNull(value, "value"); } }