Skip to content

Commit

Permalink
Initial nullable annotations of System.Private.DataContractSerialization
Browse files Browse the repository at this point in the history
Contributes to dotnet#2339
  • Loading branch information
eerhardt committed Aug 27, 2020
1 parent d413360 commit 700689b
Show file tree
Hide file tree
Showing 113 changed files with 2,763 additions and 2,547 deletions.
2 changes: 1 addition & 1 deletion src/libraries/Common/src/System/CodeDom/CodeObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class CodeObject
internal class CodeObject
#endif
{
private IDictionary _userData;
private IDictionary? _userData;

public CodeObject() { }

Expand Down
18 changes: 9 additions & 9 deletions src/libraries/Common/src/System/CodeDom/CodeTypeReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public class CodeTypeReference : CodeObject
internal class CodeTypeReference : CodeObject
#endif
{
private string _baseType;
private string? _baseType;
private readonly bool _isInterface;
private CodeTypeReferenceCollection _typeArguments;
private CodeTypeReferenceCollection? _typeArguments;
private bool _needsFixup;

public CodeTypeReference()
Expand All @@ -50,7 +50,7 @@ public CodeTypeReference(Type type)
if (type.IsArray)
{
ArrayRank = type.GetArrayRank();
ArrayElementType = new CodeTypeReference(type.GetElementType());
ArrayElementType = new CodeTypeReference(type.GetElementType()!);
_baseType = null;
}
else
Expand All @@ -73,7 +73,7 @@ public CodeTypeReference(string typeName, CodeTypeReferenceOptions codeTypeRefer
Initialize(typeName, codeTypeReferenceOption);
}

public CodeTypeReference(string typeName)
public CodeTypeReference(string? typeName)
{
Initialize(typeName);
}
Expand All @@ -86,7 +86,7 @@ private void InitializeFromType(Type type)
Type currentType = type;
while (currentType.IsNested)
{
currentType = currentType.DeclaringType;
currentType = currentType.DeclaringType!;
_baseType = currentType.Name + "+" + _baseType;
}

Expand Down Expand Up @@ -116,17 +116,17 @@ private void InitializeFromType(Type type)
}
}

private void Initialize(string typeName)
private void Initialize(string? typeName)
{
Initialize(typeName, Options);
}

private void Initialize(string typeName, CodeTypeReferenceOptions options)
private void Initialize(string? typeName, CodeTypeReferenceOptions options)
{
Options = options;
if (string.IsNullOrEmpty(typeName))
{
typeName = typeof(void).FullName;
typeName = typeof(void).FullName!;
_baseType = typeName;
ArrayRank = 0;
ArrayElementType = null;
Expand Down Expand Up @@ -303,7 +303,7 @@ public CodeTypeReference(CodeTypeReference arrayType, int rank)
ArrayElementType = arrayType;
}

public CodeTypeReference ArrayElementType { get; set; }
public CodeTypeReference? ArrayElementType { get; set; }

public int ArrayRank { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public CodeTypeReferenceCollection(CodeTypeReference[] value)

public CodeTypeReference this[int index]
{
get { return ((CodeTypeReference)(List[index])); }
get { return ((CodeTypeReference)(List[index]!)); }
set { List[index] = value; }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<NoWarn>$(NoWarn);1634;1691;649</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand All @@ -7,6 +7,7 @@
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
<!-- Too much private reflection. Do not bother with trimming -->
<ILLinkTrimAssembly>false</ILLinkTrimAssembly>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<RuntimeSerializationSources>System\Runtime\Serialization</RuntimeSerializationSources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ namespace System.Runtime.Serialization
{
internal static class FastInvokerBuilder
{
public delegate void Setter(ref object obj, object value);
public delegate object Getter(object obj);
public delegate void Setter(ref object obj, object? value);
public delegate object? Getter(object obj);

private delegate void StructSetDelegate<T, TArg>(ref T obj, TArg value);
private delegate TResult StructGetDelegate<T, out TResult>(ref T obj);

private static readonly MethodInfo s_createGetterInternal = typeof(FastInvokerBuilder).GetMethod(nameof(CreateGetterInternal), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
private static readonly MethodInfo s_createSetterInternal = typeof(FastInvokerBuilder).GetMethod(nameof(CreateSetterInternal), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
private static readonly MethodInfo s_make = typeof(FastInvokerBuilder).GetMethod(nameof(Make), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
private static readonly MethodInfo s_createGetterInternal = typeof(FastInvokerBuilder).GetMethod(nameof(CreateGetterInternal), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)!;
private static readonly MethodInfo s_createSetterInternal = typeof(FastInvokerBuilder).GetMethod(nameof(CreateSetterInternal), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)!;
private static readonly MethodInfo s_make = typeof(FastInvokerBuilder).GetMethod(nameof(Make), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)!;

public static Func<object> GetMakeNewInstanceFunc(Type type)
{
Expand All @@ -31,25 +31,23 @@ public static Func<object> GetMakeNewInstanceFunc(Type type)

public static Getter CreateGetter(MemberInfo memberInfo)
{
if (memberInfo is PropertyInfo)
if (memberInfo is PropertyInfo propInfo)
{
var propInfo = (PropertyInfo)memberInfo;
var createGetterGeneric = s_createGetterInternal.MakeGenericMethod(propInfo.DeclaringType, propInfo.PropertyType).CreateDelegate<Func<PropertyInfo, Getter>>();
var createGetterGeneric = s_createGetterInternal.MakeGenericMethod(propInfo.DeclaringType!, propInfo.PropertyType).CreateDelegate<Func<PropertyInfo, Getter>>();
Getter accessor = createGetterGeneric(propInfo);
return accessor;
}
else if (memberInfo is FieldInfo)
else if (memberInfo is FieldInfo fieldInfo)
{
return (obj) =>
{
FieldInfo fieldInfo = (FieldInfo)memberInfo;
var value = fieldInfo.GetValue(obj);
return value;
};
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.InvalidMember, DataContract.GetClrTypeFullName(memberInfo.DeclaringType), memberInfo.Name)));
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.InvalidMember, DataContract.GetClrTypeFullName(memberInfo.DeclaringType!), memberInfo.Name)));
}
}

Expand All @@ -60,7 +58,7 @@ public static Setter CreateSetter(MemberInfo memberInfo)
PropertyInfo propInfo = (PropertyInfo)memberInfo;
if (propInfo.CanWrite)
{
var buildSetAccessorGeneric = s_createSetterInternal.MakeGenericMethod(propInfo.DeclaringType, propInfo.PropertyType).CreateDelegate<Func<PropertyInfo, Setter>>();
var buildSetAccessorGeneric = s_createSetterInternal.MakeGenericMethod(propInfo.DeclaringType!, propInfo.PropertyType).CreateDelegate<Func<PropertyInfo, Setter>>();
Setter accessor = buildSetAccessorGeneric(propInfo);
return accessor;
}
Expand All @@ -72,14 +70,14 @@ public static Setter CreateSetter(MemberInfo memberInfo)
else if (memberInfo is FieldInfo)
{
FieldInfo fieldInfo = (FieldInfo)memberInfo;
return (ref object obj, object val) =>
return (ref object obj, object? val) =>
{
fieldInfo.SetValue(obj, val);
};
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.InvalidMember, DataContract.GetClrTypeFullName(memberInfo.DeclaringType), memberInfo.Name)));
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.InvalidMember, DataContract.GetClrTypeFullName(memberInfo.DeclaringType!), memberInfo.Name)));
}
}

Expand Down Expand Up @@ -111,7 +109,7 @@ private static Getter CreateGetterInternal<DeclaringType, PropertyType>(Property

if (typeof(DeclaringType).IsValueType)
{
var getMethod = propInfo.GetMethod.CreateDelegate<StructGetDelegate<DeclaringType, PropertyType>>();
var getMethod = propInfo.GetMethod!.CreateDelegate<StructGetDelegate<DeclaringType, PropertyType>>();

return (obj) =>
{
Expand All @@ -121,7 +119,7 @@ private static Getter CreateGetterInternal<DeclaringType, PropertyType>(Property
}
else
{
var getMethod = propInfo.GetMethod.CreateDelegate<Func<DeclaringType, PropertyType>>();
var getMethod = propInfo.GetMethod!.CreateDelegate<Func<DeclaringType, PropertyType>>();

return (obj) =>
{
Expand All @@ -136,14 +134,14 @@ private static Setter CreateSetterInternal<DeclaringType, PropertyType>(Property
{
if (propInfo.Name == "Key")
{
return (ref object obj, object val) =>
return (ref object obj, object? val) =>
{
((IKeyValue)obj).Key = val;
};
}
else
{
return (ref object obj, object val) =>
return (ref object obj, object? val) =>
{
((IKeyValue)obj).Value = val;
};
Expand All @@ -152,40 +150,24 @@ private static Setter CreateSetterInternal<DeclaringType, PropertyType>(Property

if (typeof(DeclaringType).IsValueType)
{
var setMethod = propInfo.SetMethod.CreateDelegate<StructSetDelegate<DeclaringType, PropertyType>>();
var setMethod = propInfo.SetMethod!.CreateDelegate<StructSetDelegate<DeclaringType, PropertyType>>();

return (ref object obj, object val) =>
return (ref object obj, object? val) =>
{
var unboxed = (DeclaringType)obj;
setMethod(ref unboxed, (PropertyType)val);
obj = unboxed;
setMethod(ref unboxed, (PropertyType)val!);
obj = unboxed!;
};
}
else
{
var setMethod = propInfo.SetMethod.CreateDelegate<Action<DeclaringType, PropertyType>>();
var setMethod = propInfo.SetMethod!.CreateDelegate<Action<DeclaringType, PropertyType>>();

return (ref object obj, object val) =>
return (ref object obj, object? val) =>
{
setMethod((DeclaringType)obj, (PropertyType)val);
setMethod((DeclaringType)obj, (PropertyType)val!);
};
}
}
}

internal static class CreateDelegateExtension
{
// a generic extension for CreateDelegate
public static T CreateDelegate<T>(this MethodInfo method) where T : class
{
try
{
return method.CreateDelegate(typeof(T)) as T;
}
catch (Exception e)
{
throw new InvalidOperationException(SR.Format(SR.FailedToCreateMethodDelegate, method.Name, method.DeclaringType.FullName), e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ internal class Attributes
DictionaryGlobals.XsiTypeLocalName
};

internal string Id;
internal string Ref;
internal string XsiTypeName;
internal string XsiTypeNamespace;
internal string XsiTypePrefix;
internal string Id = null!; // initialized in Reset
internal string Ref = null!; // initialized in Reset
internal string? XsiTypeName;
internal string? XsiTypeNamespace;
internal string? XsiTypePrefix;
internal bool XsiNil;
internal string ClrAssembly;
internal string ClrType;
internal string? ClrAssembly;
internal string? ClrType;
internal int ArraySZSize;
internal string FactoryTypeName;
internal string FactoryTypeNamespace;
internal string FactoryTypePrefix;
internal string? FactoryTypeName;
internal string? FactoryTypeNamespace;
internal string? FactoryTypePrefix;
internal bool UnrecognizedAttributesFound;

internal void Read(XmlReaderDelegator reader)
Expand Down
Loading

0 comments on commit 700689b

Please sign in to comment.