Skip to content

Commit

Permalink
Annotate System.Reflection.Context for nullability
Browse files Browse the repository at this point in the history
The impact here is almost entirely internal; the ref only gains two question marks.
  • Loading branch information
stephentoub committed Jul 1, 2021
1 parent f6cc321 commit 4adca1b
Show file tree
Hide file tree
Showing 41 changed files with 331 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public abstract partial class CustomReflectionContext : System.Reflection.Reflec
protected CustomReflectionContext() { }
protected CustomReflectionContext(System.Reflection.ReflectionContext source) { }
protected virtual System.Collections.Generic.IEnumerable<System.Reflection.PropertyInfo> AddProperties(System.Type type) { throw null; }
protected System.Reflection.PropertyInfo CreateProperty(System.Type propertyType, string name, System.Func<object, object>? getter, System.Action<object, object>? setter) { throw null; }
protected System.Reflection.PropertyInfo CreateProperty(System.Type propertyType, string name, System.Func<object, object>? getter, System.Action<object, object>? setter, System.Collections.Generic.IEnumerable<System.Attribute>? propertyCustomAttributes, System.Collections.Generic.IEnumerable<System.Attribute>? getterCustomAttributes, System.Collections.Generic.IEnumerable<System.Attribute>? setterCustomAttributes) { throw null; }
protected System.Reflection.PropertyInfo CreateProperty(System.Type propertyType, string name, System.Func<object, object?>? getter, System.Action<object, object?>? setter) { throw null; }
protected System.Reflection.PropertyInfo CreateProperty(System.Type propertyType, string name, System.Func<object, object?>? getter, System.Action<object, object?>? setter, System.Collections.Generic.IEnumerable<System.Attribute>? propertyCustomAttributes, System.Collections.Generic.IEnumerable<System.Attribute>? getterCustomAttributes, System.Collections.Generic.IEnumerable<System.Attribute>? setterCustomAttributes) { throw null; }
protected virtual System.Collections.Generic.IEnumerable<object> GetCustomAttributes(System.Reflection.MemberInfo member, System.Collections.Generic.IEnumerable<object> declaredAttributes) { throw null; }
protected virtual System.Collections.Generic.IEnumerable<object> GetCustomAttributes(System.Reflection.ParameterInfo parameter, System.Collections.Generic.IEnumerable<object> declaredAttributes) { throw null; }
public override System.Reflection.Assembly MapAssembly(System.Reflection.Assembly assembly) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
<PropertyGroup>
<!-- S.R.Context has a lot nullable warnings that need to be addressed: https://github.com/dotnet/runtime/issues/54596. -->
<Nullable Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETCoreApp'">annotations</Nullable>
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetFramework)' == 'netstandard2.0'">SR.PlatformNotSupported_ReflectionContext</GeneratePlatformNotSupportedAssemblyMessage>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static T[] Empty<T>()
return Array.Empty<T>();
}

public static bool CompareArrays<T>(T[] left, T[] right)
public static bool CompareArrays<T>(T[] left, T[] right) where T : notnull
{
if (left.Length != right.Length)
return false;
Expand All @@ -26,7 +26,7 @@ public static bool CompareArrays<T>(T[] left, T[] right)
return true;
}

public static int GetArrayHashCode<T>(T[] array)
public static int GetArrayHashCode<T>(T[] array) where T : notnull
{
int hashcode = 0;
foreach (T t in array)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static object[] GetCustomAttributes(CustomReflectionContext context, Cust
if (!inherit)
return CollectionServices.IEnumerableToArray(attributes, attributeFilterType);

CustomType baseMember = type.BaseType as CustomType;
CustomType? baseMember = type.BaseType as CustomType;

if (baseMember == null)
return CollectionServices.IEnumerableToArray(attributes, attributeFilterType);
Expand Down Expand Up @@ -55,7 +55,7 @@ public static object[] GetCustomAttributes(CustomReflectionContext context, Cust
if (!inherit)
return CollectionServices.IEnumerableToArray(attributes, attributeFilterType);

CustomMethodInfo baseMember = method.GetBaseDefinition() as CustomMethodInfo;
CustomMethodInfo? baseMember = method.GetBaseDefinition() as CustomMethodInfo;

if (baseMember == null || baseMember.Equals(method))
return CollectionServices.IEnumerableToArray(attributes, attributeFilterType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection.Context.Projection;
using System.Reflection.Context.Virtual;

namespace System.Reflection.Context.Custom
{
internal sealed class CustomType : ProjectingType
{
private IEnumerable<PropertyInfo> _newProperties;
private IEnumerable<PropertyInfo>? _newProperties;

public CustomType(Type template, CustomReflectionContext context)
: base(template, context.Projector)
Expand All @@ -37,9 +38,9 @@ public override bool IsDefined(Type attributeType, bool inherit)
return AttributeUtils.IsDefined(this, attributeType, inherit);
}

public override bool IsInstanceOfType(object o)
public override bool IsInstanceOfType([NotNullWhen(true)] object? o)
{
Type objectType = ReflectionContext.GetTypeForObject(o);
Type objectType = ReflectionContext.GetTypeForObject(o!);
return IsAssignableFrom(objectType);
}

Expand All @@ -64,7 +65,7 @@ public override PropertyInfo[] GetProperties(BindingFlags bindingAttr)
// adding new properties declared on base types
if (!getDeclaredOnly)
{
CustomType baseType = BaseType as CustomType;
CustomType? baseType = BaseType as CustomType;
while (baseType != null)
{
IEnumerable<PropertyInfo> newProperties = baseType.NewProperties;
Expand All @@ -81,9 +82,9 @@ public override PropertyInfo[] GetProperties(BindingFlags bindingAttr)
return results.ToArray();
}

protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
protected override PropertyInfo? GetPropertyImpl(string name, BindingFlags bindingAttr, Binder? binder, Type? returnType, Type[]? types, ParameterModifier[]? modifiers)
{
PropertyInfo property = base.GetPropertyImpl(name, bindingAttr, binder, returnType, types, modifiers);
PropertyInfo? property = base.GetPropertyImpl(name, bindingAttr, binder, returnType, types, modifiers);

bool getIgnoreCase = (bindingAttr & BindingFlags.IgnoreCase) == BindingFlags.IgnoreCase;
bool getDeclaredOnly = (bindingAttr & BindingFlags.DeclaredOnly) == BindingFlags.DeclaredOnly;
Expand Down Expand Up @@ -111,7 +112,7 @@ protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindin

StringComparison comparison = getIgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;

CustomType type = this;
CustomType? type = this;
foreach (PropertyInfo newDeclaredProperty in type.NewProperties)
{
if (string.Equals(newDeclaredProperty.Name, name, comparison))
Expand Down Expand Up @@ -163,7 +164,7 @@ public override MethodInfo[] GetMethods(BindingFlags bindingAttr)
// adding new methods declared on base types
if (!getDeclaredOnly)
{
CustomType baseType = BaseType as CustomType;
CustomType? baseType = BaseType as CustomType;
while (baseType != null)
{
// We shouldn't add a base type method directly on a subtype.
Expand All @@ -181,9 +182,9 @@ public override MethodInfo[] GetMethods(BindingFlags bindingAttr)
return results.ToArray();
}

protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
protected override MethodInfo? GetMethodImpl(string name, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[]? types, ParameterModifier[]? modifiers)
{
MethodInfo method = base.GetMethodImpl(name, bindingAttr, binder, callConvention, types, modifiers);
MethodInfo? method = base.GetMethodImpl(name, bindingAttr, binder, callConvention, types, modifiers);

bool getIgnoreCase = (bindingAttr & BindingFlags.IgnoreCase) == BindingFlags.IgnoreCase;
bool getDeclaredOnly = (bindingAttr & BindingFlags.DeclaredOnly) == BindingFlags.DeclaredOnly;
Expand Down Expand Up @@ -226,7 +227,7 @@ protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAtt
{
if (string.Equals(newDeclaredProperty.Name, targetPropertyName, comparison))
{
MethodInfo accessor = getPropertyGetter ? newDeclaredProperty.GetGetMethod() : newDeclaredProperty.GetSetMethod();
MethodInfo? accessor = getPropertyGetter ? newDeclaredProperty.GetGetMethod() : newDeclaredProperty.GetSetMethod();
if (accessor != null)
matchingMethods.Add(accessor);
}
Expand All @@ -235,7 +236,7 @@ protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAtt
// adding new methods declared on base types
if (!getDeclaredOnly)
{
CustomType baseType = BaseType as CustomType;
CustomType? baseType = BaseType as CustomType;

while (baseType != null)
{
Expand All @@ -247,7 +248,7 @@ protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAtt
{
PropertyInfo inheritedProperty = new InheritedPropertyInfo(newBaseProperty, this);

MethodInfo accessor = getPropertyGetter ? inheritedProperty.GetGetMethod() : inheritedProperty.GetSetMethod();
MethodInfo? accessor = getPropertyGetter ? inheritedProperty.GetGetMethod() : inheritedProperty.GetSetMethod();
if (accessor != null)
matchingMethods.Add(accessor);
}
Expand Down Expand Up @@ -278,7 +279,7 @@ protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAtt
if (binder == null)
binder = Type.DefaultBinder;

return (MethodInfo)binder.SelectMethod(bindingAttr, matchingMethods.ToArray(), types, modifiers);
return (MethodInfo?)binder.SelectMethod(bindingAttr, matchingMethods.ToArray(), types, modifiers);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection.Context.Custom;
using System.Reflection.Context.Projection;

Expand Down Expand Up @@ -45,7 +46,8 @@ public Assembly ProjectAssemblyIfNeeded(Assembly value)
return value;
}

public override TypeInfo ProjectType(Type value)
[return: NotNullIfNotNull("value")]
public override TypeInfo? ProjectType(Type? value)
{
if (value == null)
return null;
Expand All @@ -55,7 +57,8 @@ public override TypeInfo ProjectType(Type value)
return new CustomType(value, ReflectionContext);
}

public override Assembly ProjectAssembly(Assembly value)
[return: NotNullIfNotNull("value")]
public override Assembly? ProjectAssembly(Assembly? value)
{
if (value == null)
return null;
Expand All @@ -65,7 +68,8 @@ public override Assembly ProjectAssembly(Assembly value)
return new CustomAssembly(value, ReflectionContext);
}

public override Module ProjectModule(Module value)
[return: NotNullIfNotNull("value")]
public override Module? ProjectModule(Module? value)
{
if (value == null)
return null;
Expand All @@ -75,7 +79,8 @@ public override Module ProjectModule(Module value)
return new CustomModule(value, ReflectionContext);
}

public override FieldInfo ProjectField(FieldInfo value)
[return: NotNullIfNotNull("value")]
public override FieldInfo? ProjectField(FieldInfo? value)
{
if (value == null)
return null;
Expand All @@ -85,7 +90,8 @@ public override FieldInfo ProjectField(FieldInfo value)
return new CustomFieldInfo(value, ReflectionContext);
}

public override EventInfo ProjectEvent(EventInfo value)
[return: NotNullIfNotNull("value")]
public override EventInfo? ProjectEvent(EventInfo? value)
{
if (value == null)
return null;
Expand All @@ -95,7 +101,8 @@ public override EventInfo ProjectEvent(EventInfo value)
return new CustomEventInfo(value, ReflectionContext);
}

public override ConstructorInfo ProjectConstructor(ConstructorInfo value)
[return: NotNullIfNotNull("value")]
public override ConstructorInfo? ProjectConstructor(ConstructorInfo? value)
{
if (value == null)
return null;
Expand All @@ -105,7 +112,8 @@ public override ConstructorInfo ProjectConstructor(ConstructorInfo value)
return new CustomConstructorInfo(value, ReflectionContext);
}

public override MethodInfo ProjectMethod(MethodInfo value)
[return: NotNullIfNotNull("value")]
public override MethodInfo? ProjectMethod(MethodInfo? value)
{
if (value == null)
return null;
Expand All @@ -115,23 +123,25 @@ public override MethodInfo ProjectMethod(MethodInfo value)
return new CustomMethodInfo(value, ReflectionContext);
}

public override MethodBase ProjectMethodBase(MethodBase value)
[return: NotNullIfNotNull("value")]
public override MethodBase? ProjectMethodBase(MethodBase? value)
{
if (value == null)
return null;

MethodInfo method = value as MethodInfo;
MethodInfo? method = value as MethodInfo;
if (method != null)
return ProjectMethod(method);

ConstructorInfo constructor = value as ConstructorInfo;
ConstructorInfo? constructor = value as ConstructorInfo;
if (constructor != null)
return ProjectConstructor(constructor);

throw new InvalidOperationException(SR.Format(SR.InvalidOperation_InvalidMethodType, value.GetType()));
}

public override PropertyInfo ProjectProperty(PropertyInfo value)
[return: NotNullIfNotNull("value")]
public override PropertyInfo? ProjectProperty(PropertyInfo? value)
{
if (value == null)
return null;
Expand All @@ -141,7 +151,8 @@ public override PropertyInfo ProjectProperty(PropertyInfo value)
return new CustomPropertyInfo(value, ReflectionContext);
}

public override ParameterInfo ProjectParameter(ParameterInfo value)
[return: NotNullIfNotNull("value")]
public override ParameterInfo? ProjectParameter(ParameterInfo? value)
{
if (value == null)
return null;
Expand All @@ -151,7 +162,8 @@ public override ParameterInfo ProjectParameter(ParameterInfo value)
return new CustomParameterInfo(value, ReflectionContext);
}

public override MethodBody ProjectMethodBody(MethodBody value)
[return: NotNullIfNotNull("value")]
public override MethodBody? ProjectMethodBody(MethodBody? value)
{
if (value == null)
return null;
Expand All @@ -161,7 +173,8 @@ public override MethodBody ProjectMethodBody(MethodBody value)
return new ProjectingMethodBody(value, this);
}

public override LocalVariableInfo ProjectLocalVariable(LocalVariableInfo value)
[return: NotNullIfNotNull("value")]
public override LocalVariableInfo? ProjectLocalVariable(LocalVariableInfo? value)
{
if (value == null)
return null;
Expand All @@ -171,7 +184,8 @@ public override LocalVariableInfo ProjectLocalVariable(LocalVariableInfo value)
return new ProjectingLocalVariableInfo(value, this);
}

public override ExceptionHandlingClause ProjectExceptionHandlingClause(ExceptionHandlingClause value)
[return: NotNullIfNotNull("value")]
public override ExceptionHandlingClause? ProjectExceptionHandlingClause(ExceptionHandlingClause? value)
{
if (value == null)
return null;
Expand All @@ -181,7 +195,8 @@ public override ExceptionHandlingClause ProjectExceptionHandlingClause(Exception
return new ProjectingExceptionHandlingClause(value, this);
}

public override CustomAttributeData ProjectCustomAttributeData(CustomAttributeData value)
[return: NotNullIfNotNull("value")]
public override CustomAttributeData? ProjectCustomAttributeData(CustomAttributeData? value)
{
if (value == null)
return null;
Expand All @@ -191,7 +206,8 @@ public override CustomAttributeData ProjectCustomAttributeData(CustomAttributeDa
return new ProjectingCustomAttributeData(value, this);
}

public override ManifestResourceInfo ProjectManifestResource(ManifestResourceInfo value)
[return: NotNullIfNotNull("value")]
public override ManifestResourceInfo? ProjectManifestResource(ManifestResourceInfo? value)
{
if (value == null)
return null;
Expand All @@ -201,12 +217,13 @@ public override ManifestResourceInfo ProjectManifestResource(ManifestResourceInf
return new ProjectingManifestResourceInfo(value, this);
}

public override MemberInfo ProjectMember(MemberInfo value)
[return: NotNullIfNotNull("value")]
public override MemberInfo? ProjectMember(MemberInfo? value)
{
if (value == null)
return null;

MemberInfo output = null;
MemberInfo? output;
switch (value.MemberType)
{
case MemberTypes.TypeInfo:
Expand Down Expand Up @@ -243,7 +260,7 @@ public override MemberInfo ProjectMember(MemberInfo value)

public override CustomAttributeTypedArgument ProjectTypedArgument(CustomAttributeTypedArgument value)
{
Type argumentType = ProjectType(value.ArgumentType);
Type? argumentType = ProjectType(value.ArgumentType);

return new CustomAttributeTypedArgument(argumentType, value.Value);
}
Expand Down
Loading

0 comments on commit 4adca1b

Please sign in to comment.