Skip to content

Commit 82fa2b2

Browse files
authored
remove NotNull and ItemNotNull attributes (#17)
* remove NotNull and ItemNotNull attributes * fix nullable warnings * fix nullable warning / add message to exceptions.
1 parent 1befa6c commit 82fa2b2

File tree

12 files changed

+167
-145
lines changed

12 files changed

+167
-145
lines changed

Packages/com.quabug.any-processor/CodeGen/AnyProcessor.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,7 @@ private bool ProcessImpl()
8080
var modified = false;
8181
modified = ProcessEachAttribute(Assembly, ProcessAssembly) || modified;
8282
modified = ProcessEachAttribute(Module, ProcessModule) || modified;
83-
var explicitTypeProcessor = Assembly.GetAttributesOf<ExplicitAnyProcessorTypeAttribute>().Any();
84-
foreach (var type in Module.GetAllTypes())
85-
{
86-
modified = ProcessEachAttribute(type, ProcessType) || modified;
87-
if (type.HasGenericParameters) modified = ProcessEachAttribute(type.GenericParameters, ProcessGenericParameter) || modified;
88-
if (explicitTypeProcessor && !type.GetAttributesOf<AnyProcessorTypeAttribute>().Any()) continue;
89-
90-
if (type.HasMethods) modified = ProcessEachAttribute(type.Methods, ProcessMethod) || modified;
91-
if (type.HasProperties) modified = ProcessEachAttribute(type.Properties, ProcessProperty) || modified;
92-
if (type.HasFields) modified = ProcessEachAttribute(type.Fields, ProcessField) || modified;
93-
}
83+
modified = ProcessModuleTypes() || modified;
9484
return modified;
9585
}
9686
finally
@@ -99,6 +89,23 @@ private bool ProcessImpl()
9989
}
10090
}
10191

92+
private bool ProcessModuleTypes()
93+
{
94+
var modified = false;
95+
var explicitTypeProcessor = Assembly.GetAttributesOf<ExplicitAnyProcessorTypeAttribute>().Any();
96+
foreach (var type in Module.GetAllTypes())
97+
{
98+
modified = ProcessEachAttribute(type, ProcessType) || modified;
99+
if (type.HasGenericParameters) modified = ProcessEachAttribute(type.GenericParameters, ProcessGenericParameter) || modified;
100+
if (explicitTypeProcessor && !type.GetAttributesOf<AnyProcessorTypeAttribute>().Any()) continue;
101+
102+
if (type.HasMethods) modified = ProcessEachAttribute(type.Methods, ProcessMethod) || modified;
103+
if (type.HasProperties) modified = ProcessEachAttribute(type.Properties, ProcessProperty) || modified;
104+
if (type.HasFields) modified = ProcessEachAttribute(type.Fields, ProcessField) || modified;
105+
}
106+
return modified;
107+
}
108+
102109
private bool ProcessEachAttribute<TAttributeProvider>(TAttributeProvider value, Func<TAttributeProvider, CustomAttribute, bool> processor)
103110
where TAttributeProvider : ICustomAttributeProvider
104111
{

Packages/com.quabug.any-processor/CodeGen/Extension/EnumerableExtension.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using System.Collections.Generic;
3+
using JetBrains.Annotations;
34

45
namespace AnyProcessor.CodeGen
56
{
67
public static class EnumerableExtension
78
{
9+
[ItemNotNull]
810
public static IEnumerable<T> Yield<T>(this T value)
911
{
1012
yield return value;

Packages/com.quabug.any-processor/CodeGen/Extension/Extensions.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,31 +148,30 @@ public static TypeDefinition CreateNestedStaticPrivateClass(this TypeDefinition
148148
return nestedType;
149149
}
150150

151-
public static IEnumerable<CustomAttribute> GetAttributesOf<T>([NotNull] this ICustomAttributeProvider provider) where T : Attribute
151+
public static IEnumerable<CustomAttribute> GetAttributesOf<T>(this ICustomAttributeProvider provider) where T : Attribute
152152
{
153153
return provider.GetAttributesOf(typeof(T));
154154
}
155155

156-
public static IEnumerable<CustomAttribute> GetAttributesOf([NotNull] this ICustomAttributeProvider provider, Type type)
156+
public static IEnumerable<CustomAttribute> GetAttributesOf(this ICustomAttributeProvider provider, Type type)
157157
{
158158
return provider.HasCustomAttributes
159159
? provider.CustomAttributes.Where(attribute => attribute.AttributeType.FullName == type.FullName)
160160
: Enumerable.Empty<CustomAttribute>()
161161
;
162162
}
163163

164-
public static string GetBackingFieldName([NotNull] this PropertyDefinition property)
164+
public static string GetBackingFieldName(this PropertyDefinition property)
165165
{
166166
return property.Name.GetBackingFieldName();
167167
}
168168

169-
public static string GetBackingFieldName([NotNull] this string propertyName)
169+
public static string GetBackingFieldName(this string propertyName)
170170
{
171171
return $"<{propertyName}>k__BackingField";
172172
}
173173

174-
[NotNull]
175-
public static FieldDefinition CreateOrReplaceBackingField([NotNull] this PropertyDefinition property, [NotNull] TypeReference fieldType)
174+
public static FieldDefinition CreateOrReplaceBackingField(this PropertyDefinition property, TypeReference fieldType)
176175
{
177176
var backingFieldName = property.GetBackingFieldName();
178177
var backingField = property.DeclaringType.Fields.FirstOrDefault(field => field.Name == backingFieldName)
@@ -182,8 +181,7 @@ public static FieldDefinition CreateOrReplaceBackingField([NotNull] this Propert
182181
return backingField;
183182
}
184183

185-
[NotNull]
186-
public static FieldDefinition CreateSerializeReferenceField([NotNull] this PropertyDefinition property, [NotNull] TypeReference fieldType)
184+
public static FieldDefinition CreateSerializeReferenceField(this PropertyDefinition property, TypeReference fieldType)
187185
{
188186
//.field private class AnySerialize.Tests.TestMonoBehavior/__generic_serialize_reference_GenericInterface__/IBase _GenericInterface
189187
// .custom instance void [UnityEngine.CoreModule]UnityEngine.SerializeReference::.ctor()
@@ -197,7 +195,7 @@ public static FieldDefinition CreateSerializeReferenceField([NotNull] this Prope
197195
return serializedField;
198196
}
199197

200-
public static void ReplacePropertyGetterByFieldMethod([NotNull] this PropertyDefinition property, [NotNull] FieldDefinition serializedField, [NotNull] string fieldMethodName)
198+
public static void ReplacePropertyGetterByFieldMethod(this PropertyDefinition property, FieldDefinition serializedField, string fieldMethodName)
201199
{
202200
// before
203201
// IL_0000: ldarg.0 // this
@@ -219,7 +217,7 @@ public static void ReplacePropertyGetterByFieldMethod([NotNull] this PropertyDef
219217
instructions.Add(Instruction.Create(OpCodes.Ret));
220218
}
221219

222-
public static void ReplacePropertySetterByFieldMethod([NotNull] this PropertyDefinition property, [NotNull] FieldDefinition serializedField, [NotNull] string fieldMethodName)
220+
public static void ReplacePropertySetterByFieldMethod(this PropertyDefinition property, FieldDefinition serializedField, string fieldMethodName)
223221
{
224222
// before
225223
// IL_0000: ldarg.0 // this

Packages/com.quabug.any-processor/CodeGen/Extension/ReflectionExtension.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ namespace AnyProcessor.CodeGen
1010
{
1111
public static class ReflectionExtension
1212
{
13-
[Pure, NotNull]
14-
public static string ToReadableName([NotNull] this Type type)
13+
[Pure]
14+
public static string ToReadableName(this Type type)
1515
{
1616
return type.IsGenericType ? Regex.Replace(type.ToString(), @"(\w+)`\d+\[(.*)\]", "$1<$2>") : type.ToString();
1717
}
1818

19-
[Pure, NotNull, ItemNotNull]
20-
public static IEnumerable<Type> GetSelfAndAllBases([NotNull] this Type type)
19+
[Pure]
20+
public static IEnumerable<Type> GetSelfAndAllBases(this Type type)
2121
{
2222
do
2323
{
@@ -26,8 +26,8 @@ public static IEnumerable<Type> GetSelfAndAllBases([NotNull] this Type type)
2626
} while (type != null);
2727
}
2828

29-
[Pure, NotNull]
30-
public static Type ToReflectionType([NotNull] this TypeReference cecilType)
29+
[Pure]
30+
public static Type ToReflectionType(this TypeReference cecilType)
3131
{
3232
return cecilType switch
3333
{
@@ -46,8 +46,8 @@ public static Type ToReflectionType([NotNull] this TypeReference cecilType)
4646
};
4747
}
4848

49-
[Pure, NotNull]
50-
public static TypeReference ImportType([NotNull] this ModuleDefinition module, [NotNull] Type type, ILPostProcessorLogger logger = null)
49+
[Pure]
50+
public static TypeReference ImportType(this ModuleDefinition module, Type type, ILPostProcessorLogger logger = null)
5151
{
5252
if (type.IsArray) return module.ImportType(type.GetElementType(), logger).MakeArrayType();
5353
if (type.IsByRef) return module.ImportType(type.GetElementType(), logger).MakeByReferenceType();

Packages/com.quabug.any-processor/CodeGen/Extension/TypeEqualsExtension.cs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ namespace AnyProcessor.CodeGen
88
public static class TypeEqualsExtension
99
{
1010
[Pure]
11-
public static bool TypeEquals([NotNull] this TypeReference lhs, [NotNull] TypeReference rhs)
11+
public static bool TypeEquals(this TypeReference? lhs, TypeReference? rhs)
1212
{
13-
if (lhs == null || rhs == null) throw new ArgumentNullException();
13+
if (lhs == null) throw new ArgumentNullException(nameof(lhs));
14+
if (rhs == null) throw new ArgumentNullException(nameof(rhs));
1415
if (ReferenceEquals(lhs, rhs)) return true;
1516
return (lhs, rhs) switch
1617
{
@@ -42,63 +43,70 @@ public static bool TypeEquals([NotNull] this TypeReference lhs, [NotNull] TypeRe
4243
}
4344

4445
[Pure]
45-
public static bool TypeEquals([NotNull] this TypeDefinition lhs, [NotNull] TypeDefinition rhs)
46+
public static bool TypeEquals(this TypeDefinition? lhs, TypeDefinition? rhs)
4647
{
47-
if (lhs == null || rhs == null) throw new ArgumentNullException();
48-
return lhs.MetadataToken == rhs.MetadataToken && lhs.Module.Name == rhs.Module.Name;
48+
if (lhs == null) throw new ArgumentNullException(nameof(lhs));
49+
if (rhs == null) throw new ArgumentNullException(nameof(rhs));
50+
return lhs.MetadataToken == rhs.MetadataToken && lhs.Module?.Name == rhs.Module?.Name;
4951
}
5052

5153
[Pure]
52-
public static bool TypeEquals([NotNull] this GenericParameter lhs, [NotNull] GenericParameter rhs)
54+
public static bool TypeEquals(this GenericParameter? lhs, GenericParameter? rhs)
5355
{
54-
if (lhs == null || rhs == null) throw new ArgumentNullException();
56+
if (lhs == null) throw new ArgumentNullException(nameof(lhs));
57+
if (rhs == null) throw new ArgumentNullException(nameof(rhs));
5558
return lhs.IsContravariant == rhs.IsContravariant && lhs.IsCovariant == rhs.IsCovariant && lhs.IsNonVariant == rhs.IsNonVariant
56-
&& lhs.HasConstraints == rhs.HasConstraints && lhs.Constraints.Count == rhs.Constraints.Count
59+
&& lhs.HasConstraints == rhs.HasConstraints && lhs.Constraints!.Count == rhs.Constraints!.Count
5760
&& lhs.Constraints.Zip(rhs.Constraints, (l, r) => (l, r)).All(t => TypeEquals(t.l, t.r));
5861
}
5962

6063
[Pure]
61-
public static bool TypeEquals([NotNull] this GenericParameterConstraint lhs, [NotNull] GenericParameterConstraint rhs)
64+
public static bool TypeEquals(this GenericParameterConstraint? lhs, GenericParameterConstraint? rhs)
6265
{
63-
if (lhs == null || rhs == null) throw new ArgumentNullException();
66+
if (lhs == null) throw new ArgumentNullException(nameof(lhs));
67+
if (rhs == null) throw new ArgumentNullException(nameof(rhs));
6468
return TypeEquals(lhs.ConstraintType, rhs.ConstraintType);
6569
}
6670

6771
[Pure]
68-
public static bool TypeEquals([NotNull] this TypeSpecification lhs, [NotNull] TypeSpecification rhs)
72+
public static bool TypeEquals(this TypeSpecification? lhs, TypeSpecification? rhs)
6973
{
70-
if (lhs == null || rhs == null) throw new ArgumentNullException();
74+
if (lhs == null) throw new ArgumentNullException(nameof(lhs));
75+
if (rhs == null) throw new ArgumentNullException(nameof(rhs));
7176
return TypeEquals(lhs.ElementType, rhs.ElementType);
7277
}
7378

7479
[Pure]
75-
public static bool TypeEquals([NotNull] this GenericInstanceType lhs, [NotNull] GenericInstanceType rhs)
80+
public static bool TypeEquals(this GenericInstanceType? lhs, GenericInstanceType? rhs)
7681
{
77-
if (lhs == null || rhs == null) throw new ArgumentNullException();
82+
if (lhs == null) throw new ArgumentNullException(nameof(lhs));
83+
if (rhs == null) throw new ArgumentNullException(nameof(rhs));
7884
if (lhs.HasGenericArguments != rhs.HasGenericArguments) return false;
79-
if (lhs.GenericArguments.Count != rhs.GenericArguments.Count) return false;
85+
if (lhs.GenericArguments!.Count != rhs.GenericArguments!.Count) return false;
8086
if (!TypeEquals(lhs.Resolve(), rhs.Resolve())) return false;
8187
return lhs.GenericArguments.Zip(rhs.GenericArguments, (l, r) => (l, r)).All(t => TypeEquals(t.l, t.r));
8288
}
8389

8490
[Pure]
85-
public static bool TypeEquals([NotNull] this OptionalModifierType lhs, [NotNull] OptionalModifierType rhs)
91+
public static bool TypeEquals(this OptionalModifierType? lhs, OptionalModifierType? rhs)
8692
{
87-
if (lhs == null || rhs == null) throw new ArgumentNullException();
93+
if (lhs == null) throw new ArgumentNullException(nameof(lhs));
94+
if (rhs == null) throw new ArgumentNullException(nameof(rhs));
8895
return TypeEquals(lhs.ModifierType, rhs.ModifierType) && TypeEquals(lhs.ElementType, rhs.ElementType);
8996
}
9097

9198
[Pure]
92-
public static bool TypeEquals([NotNull] this RequiredModifierType lhs, [NotNull] RequiredModifierType rhs)
99+
public static bool TypeEquals(this RequiredModifierType? lhs, RequiredModifierType? rhs)
93100
{
94-
if (lhs == null || rhs == null) throw new ArgumentNullException();
101+
if (lhs == null) throw new ArgumentNullException(nameof(lhs));
102+
if (rhs == null) throw new ArgumentNullException(nameof(rhs));
95103
return TypeEquals(lhs.ModifierType, rhs.ModifierType) && TypeEquals(lhs.ElementType, rhs.ElementType);
96104
}
97105

98106
[Pure]
99-
public static bool TypeEquals([NotNull] this FunctionPointerType lhs, [NotNull] FunctionPointerType rhs)
107+
public static bool TypeEquals(this FunctionPointerType? lhs, FunctionPointerType? rhs)
100108
{
101-
throw new NotSupportedException();
109+
throw new NotSupportedException($"compare between two {nameof(FunctionPointerType)}");
102110
}
103111
}
104112
}

0 commit comments

Comments
 (0)