Skip to content

Commit 3f6cf72

Browse files
authored
[.Localization, .Cecil, .Diagnostics, .Generator] $(Nullable)=enable (#746)
Update `Java.Interop.Localization.dll`, `Java.Interop.Tools.Cecil.dll`, `Java.Interop.Tools.Diagnostics.dll`, and `Java.Interop.Tools.Generator.dll` so that C# 8 [Nullable Reference Types][0] are used. [0]: https://docs.microsoft.com/dotnet/csharp/nullable-references
1 parent 2f62ffd commit 3f6cf72

File tree

11 files changed

+105
-61
lines changed

11 files changed

+105
-61
lines changed

src/Java.Interop.Localization/Java.Interop.Localization.csproj

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5+
<LangVersion>8.0</LangVersion>
6+
<Nullable>enable</Nullable>
7+
<DefineConstants>INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
58
</PropertyGroup>
69

710
<ItemGroup>
@@ -16,6 +19,10 @@
1619
</Compile>
1720
</ItemGroup>
1821

22+
<ItemGroup>
23+
<Compile Include="..\utils\NullableAttributes.cs" />
24+
</ItemGroup>
25+
1926
<ItemGroup>
2027
<EmbeddedResource Update="Resources.resx">
2128
<Generator>PublicResXFileCodeGenerator</Generator>

src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil.csproj

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5+
<LangVersion>8.0</LangVersion>
6+
<Nullable>enable</Nullable>
7+
<DefineConstants>INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
58
</PropertyGroup>
69

710
<Import Project="..\..\build-tools\scripts\cecil.projitems" />
@@ -10,6 +13,10 @@
1013
<OutputPath>$(ToolOutputFullPath)</OutputPath>
1114
</PropertyGroup>
1215

16+
<ItemGroup>
17+
<Compile Include="..\utils\NullableAttributes.cs" />
18+
</ItemGroup>
19+
1320
<ItemGroup>
1421
<ProjectReference Include="..\Java.Interop.Localization\Java.Interop.Localization.csproj" />
1522
<ProjectReference Include="..\Java.Interop.Tools.Diagnostics\Java.Interop.Tools.Diagnostics.csproj" />

src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs

+34-25
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class DirectoryAssemblyResolver : IAssemblyResolver {
6161

6262
public ICollection<string> SearchDirectories {get; private set;}
6363

64-
Dictionary<string, AssemblyDefinition> cache;
64+
Dictionary<string, AssemblyDefinition?> cache;
6565
bool loadDebugSymbols;
6666
Action<TraceLevel, string> logger;
6767

@@ -71,18 +71,18 @@ public class DirectoryAssemblyResolver : IAssemblyResolver {
7171
};
7272

7373
[Obsolete ("Use DirectoryAssemblyResolver(Action<TraceLevel, string>, bool, ReaderParameters)")]
74-
public DirectoryAssemblyResolver (Action<string, object[]> logWarnings, bool loadDebugSymbols, ReaderParameters loadReaderParameters = null)
74+
public DirectoryAssemblyResolver (Action<string, object[]> logWarnings, bool loadDebugSymbols, ReaderParameters? loadReaderParameters = null)
7575
: this ((TraceLevel level, string value) => logWarnings?.Invoke ("{0}", new[]{value}), loadDebugSymbols, loadReaderParameters)
7676
{
7777
if (logWarnings == null)
7878
throw new ArgumentNullException (nameof (logWarnings));
7979
}
8080

81-
public DirectoryAssemblyResolver (Action<TraceLevel, string> logger, bool loadDebugSymbols, ReaderParameters loadReaderParameters = null)
81+
public DirectoryAssemblyResolver (Action<TraceLevel, string> logger, bool loadDebugSymbols, ReaderParameters? loadReaderParameters = null)
8282
{
8383
if (logger == null)
8484
throw new ArgumentNullException (nameof (logger));
85-
cache = new Dictionary<string, AssemblyDefinition> ();
85+
cache = new Dictionary<string, AssemblyDefinition?> ();
8686
this.loadDebugSymbols = loadDebugSymbols;
8787
this.logger = logger;
8888
SearchDirectories = new List<string> ();
@@ -100,14 +100,14 @@ protected virtual void Dispose (bool disposing)
100100
if (!disposing || cache == null)
101101
return;
102102
foreach (var e in cache) {
103-
e.Value.Dispose ();
103+
e.Value?.Dispose ();
104104
}
105-
cache = null;
105+
cache.Clear ();
106106
}
107107

108-
public Dictionary<string, AssemblyDefinition> ToResolverCache ()
108+
public Dictionary<string, AssemblyDefinition?> ToResolverCache ()
109109
{
110-
return new Dictionary<string, AssemblyDefinition>(cache);
110+
return new Dictionary<string, AssemblyDefinition?>(cache);
111111
}
112112

113113
public bool AddToCache (AssemblyDefinition assembly)
@@ -122,12 +122,12 @@ public bool AddToCache (AssemblyDefinition assembly)
122122
return true;
123123
}
124124

125-
public virtual AssemblyDefinition Load (string fileName, bool forceLoad = false)
125+
public virtual AssemblyDefinition? Load (string fileName, bool forceLoad = false)
126126
{
127127
if (!File.Exists (fileName))
128128
return null;
129129

130-
AssemblyDefinition assembly = null;
130+
AssemblyDefinition? assembly = null;
131131
var name = Path.GetFileNameWithoutExtension (fileName);
132132
if (!forceLoad && cache.TryGetValue (name, out assembly))
133133
return assembly;
@@ -181,7 +181,7 @@ public AssemblyDefinition Resolve (string fullName)
181181
return Resolve (fullName, null);
182182
}
183183

184-
public AssemblyDefinition Resolve (string fullName, ReaderParameters parameters)
184+
public AssemblyDefinition Resolve (string fullName, ReaderParameters? parameters)
185185
{
186186
return Resolve (AssemblyNameReference.Parse (fullName), parameters);
187187
}
@@ -200,7 +200,7 @@ public string FindAssemblyFile (AssemblyNameReference reference)
200200
{
201201
var name = reference.Name;
202202

203-
string assembly;
203+
string? assembly;
204204
foreach (var dir in SearchDirectories)
205205
if ((assembly = SearchDirectory (name, dir)) != null)
206206
return assembly;
@@ -216,20 +216,24 @@ public string FindAssemblyFile (AssemblyNameReference reference)
216216
name + ".dll");
217217
}
218218

219-
public AssemblyDefinition Resolve (AssemblyNameReference reference, ReaderParameters parameters)
219+
public AssemblyDefinition Resolve (AssemblyNameReference reference, ReaderParameters? parameters)
220220
{
221221
var name = reference.Name;
222222

223-
AssemblyDefinition assembly;
224-
if (cache.TryGetValue (name, out assembly))
223+
AssemblyDefinition? assembly;
224+
if (cache.TryGetValue (name, out assembly)) {
225+
if (assembly is null)
226+
throw CreateLoadException (reference);
227+
225228
return assembly;
229+
}
226230

227-
string assemblyFile;
228-
AssemblyDefinition candidate = null;
231+
string? assemblyFile;
232+
AssemblyDefinition? candidate = null;
229233
foreach (var dir in SearchDirectories) {
230234
if ((assemblyFile = SearchDirectory (name, dir)) != null) {
231235
var loaded = Load (assemblyFile);
232-
if (Array.Equals (loaded.Name.MetadataToken, reference.MetadataToken))
236+
if (Array.Equals (loaded?.Name.MetadataToken, reference.MetadataToken))
233237
return loaded;
234238
candidate = candidate ?? loaded;
235239
}
@@ -238,18 +242,23 @@ public AssemblyDefinition Resolve (AssemblyNameReference reference, ReaderParame
238242
if (candidate != null)
239243
return candidate;
240244

241-
throw new System.IO.FileNotFoundException (
245+
throw CreateLoadException (reference);
246+
}
247+
248+
static FileNotFoundException CreateLoadException (AssemblyNameReference reference)
249+
{
250+
return new System.IO.FileNotFoundException (
242251
string.Format ("Could not load assembly '{0}, Version={1}, Culture={2}, PublicKeyToken={3}'. Perhaps it doesn't exist in the Mono for Android profile?",
243-
name,
244-
reference.Version,
245-
string.IsNullOrEmpty (reference.Culture) ? "neutral" : reference.Culture,
252+
reference.Name,
253+
reference.Version,
254+
string.IsNullOrEmpty (reference.Culture) ? "neutral" : reference.Culture,
246255
reference.PublicKeyToken == null
247256
? "null"
248-
: string.Join ("", reference.PublicKeyToken.Select(b => b.ToString ("x2")))),
249-
name + ".dll");
257+
: string.Join ("", reference.PublicKeyToken.Select (b => b.ToString ("x2")))),
258+
reference.Name + ".dll");
250259
}
251260

252-
string SearchDirectory (string name, string directory)
261+
string? SearchDirectory (string name, string directory)
253262
{
254263
if (Path.IsPathRooted (name) && File.Exists (name))
255264
return name;

src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/MethodDefinitionRocks.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static class MethodDefinitionRocks
1313
public static MethodDefinition GetBaseDefinition (this MethodDefinition method) =>
1414
GetBaseDefinition (method, cache: null);
1515

16-
public static MethodDefinition GetBaseDefinition (this MethodDefinition method, TypeDefinitionCache cache)
16+
public static MethodDefinition GetBaseDefinition (this MethodDefinition method, TypeDefinitionCache? cache)
1717
{
1818
if (method.IsStatic || method.IsNewSlot || !method.IsVirtual)
1919
return method;
@@ -35,7 +35,7 @@ public static MethodDefinition GetBaseDefinition (this MethodDefinition method,
3535
public static IEnumerable<MethodDefinition> GetOverriddenMethods (MethodDefinition method, bool inherit) =>
3636
GetOverriddenMethods (method, inherit, cache: null);
3737

38-
public static IEnumerable<MethodDefinition> GetOverriddenMethods (MethodDefinition method, bool inherit, TypeDefinitionCache cache)
38+
public static IEnumerable<MethodDefinition> GetOverriddenMethods (MethodDefinition method, bool inherit, TypeDefinitionCache? cache)
3939
{
4040
yield return method;
4141
if (inherit) {
@@ -51,7 +51,7 @@ public static IEnumerable<MethodDefinition> GetOverriddenMethods (MethodDefiniti
5151
public static bool AreParametersCompatibleWith (this Collection<ParameterDefinition> a, Collection<ParameterDefinition> b) =>
5252
AreParametersCompatibleWith (a, b, cache: null);
5353

54-
public static bool AreParametersCompatibleWith (this Collection<ParameterDefinition> a, Collection<ParameterDefinition> b, TypeDefinitionCache cache)
54+
public static bool AreParametersCompatibleWith (this Collection<ParameterDefinition> a, Collection<ParameterDefinition> b, TypeDefinitionCache? cache)
5555
{
5656
if (a.Count != b.Count)
5757
return false;
@@ -66,15 +66,15 @@ public static bool AreParametersCompatibleWith (this Collection<ParameterDefinit
6666
return true;
6767
}
6868

69-
static bool IsParameterCompatibleWith (IModifierType a, IModifierType b, TypeDefinitionCache cache)
69+
static bool IsParameterCompatibleWith (IModifierType a, IModifierType b, TypeDefinitionCache? cache)
7070
{
7171
if (!IsParameterCompatibleWith (a.ModifierType, b.ModifierType, cache))
7272
return false;
7373

7474
return IsParameterCompatibleWith (a.ElementType, b.ElementType, cache);
7575
}
7676

77-
static bool IsParameterCompatibleWith (TypeSpecification a, TypeSpecification b, TypeDefinitionCache cache)
77+
static bool IsParameterCompatibleWith (TypeSpecification a, TypeSpecification b, TypeDefinitionCache? cache)
7878
{
7979
if (a is GenericInstanceType)
8080
return IsParameterCompatibleWith ((GenericInstanceType) a, (GenericInstanceType) b, cache);
@@ -85,7 +85,7 @@ static bool IsParameterCompatibleWith (TypeSpecification a, TypeSpecification b,
8585
return IsParameterCompatibleWith (a.ElementType, b.ElementType, cache);
8686
}
8787

88-
static bool IsParameterCompatibleWith (GenericInstanceType a, GenericInstanceType b, TypeDefinitionCache cache)
88+
static bool IsParameterCompatibleWith (GenericInstanceType a, GenericInstanceType b, TypeDefinitionCache? cache)
8989
{
9090
if (!IsParameterCompatibleWith (a.ElementType, b.ElementType, cache))
9191
return false;
@@ -103,7 +103,7 @@ static bool IsParameterCompatibleWith (GenericInstanceType a, GenericInstanceTyp
103103
return true;
104104
}
105105

106-
static bool IsParameterCompatibleWith (TypeReference a, TypeReference b, TypeDefinitionCache cache)
106+
static bool IsParameterCompatibleWith (TypeReference a, TypeReference b, TypeDefinitionCache? cache)
107107
{
108108
if (a is TypeSpecification || b is TypeSpecification) {
109109
if (a.GetType () != b.GetType ())

src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/TypeDefinitionRocks.cs

+21-17
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ namespace Java.Interop.Tools.Cecil {
88
public static class TypeDefinitionRocks {
99

1010
[Obsolete ("Use the TypeDefinitionCache overload for better performance.")]
11-
public static TypeDefinition GetBaseType (this TypeDefinition type) =>
11+
public static TypeDefinition? GetBaseType (this TypeDefinition type) =>
1212
GetBaseType (type, cache: null);
1313

14-
public static TypeDefinition GetBaseType (this TypeDefinition type, TypeDefinitionCache cache)
14+
public static TypeDefinition? GetBaseType (this TypeDefinition type, TypeDefinitionCache? cache)
1515
{
1616
var bt = type.BaseType;
1717
if (bt == null)
@@ -25,30 +25,34 @@ public static TypeDefinition GetBaseType (this TypeDefinition type, TypeDefiniti
2525
public static IEnumerable<TypeDefinition> GetTypeAndBaseTypes (this TypeDefinition type) =>
2626
GetTypeAndBaseTypes (type, cache: null);
2727

28-
public static IEnumerable<TypeDefinition> GetTypeAndBaseTypes (this TypeDefinition type, TypeDefinitionCache cache)
28+
public static IEnumerable<TypeDefinition> GetTypeAndBaseTypes (this TypeDefinition type, TypeDefinitionCache? cache)
2929
{
30-
while (type != null) {
31-
yield return type;
32-
type = type.GetBaseType (cache);
30+
TypeDefinition? t = type;
31+
32+
while (t != null) {
33+
yield return t;
34+
t = t.GetBaseType (cache);
3335
}
3436
}
3537

3638
[Obsolete ("Use the TypeDefinitionCache overload for better performance.")]
3739
public static IEnumerable<TypeDefinition> GetBaseTypes (this TypeDefinition type) =>
3840
GetBaseTypes (type, cache: null);
3941

40-
public static IEnumerable<TypeDefinition> GetBaseTypes (this TypeDefinition type, TypeDefinitionCache cache)
42+
public static IEnumerable<TypeDefinition> GetBaseTypes (this TypeDefinition type, TypeDefinitionCache? cache)
4143
{
42-
while ((type = type.GetBaseType (cache)) != null) {
43-
yield return type;
44+
TypeDefinition? t = type;
45+
46+
while ((t = t.GetBaseType (cache)) != null) {
47+
yield return t;
4448
}
4549
}
4650

4751
[Obsolete ("Use the TypeDefinitionCache overload for better performance.")]
4852
public static bool IsAssignableFrom (this TypeReference type, TypeReference c) =>
4953
IsAssignableFrom (type, c, cache: null);
5054

51-
public static bool IsAssignableFrom (this TypeReference type, TypeReference c, TypeDefinitionCache cache)
55+
public static bool IsAssignableFrom (this TypeReference type, TypeReference c, TypeDefinitionCache? cache)
5256
{
5357
if (type.FullName == c.FullName)
5458
return true;
@@ -71,7 +75,7 @@ public static bool IsAssignableFrom (this TypeReference type, TypeReference c, T
7175
public static bool IsSubclassOf (this TypeDefinition type, string typeName) =>
7276
IsSubclassOf (type, typeName, cache: null);
7377

74-
public static bool IsSubclassOf (this TypeDefinition type, string typeName, TypeDefinitionCache cache)
78+
public static bool IsSubclassOf (this TypeDefinition type, string typeName, TypeDefinitionCache? cache)
7579
{
7680
foreach (var t in type.GetTypeAndBaseTypes (cache)) {
7781
if (t.FullName == typeName) {
@@ -85,7 +89,7 @@ public static bool IsSubclassOf (this TypeDefinition type, string typeName, Type
8589
public static bool ImplementsInterface (this TypeDefinition type, string interfaceName) =>
8690
ImplementsInterface (type, interfaceName, cache: null);
8791

88-
public static bool ImplementsInterface (this TypeDefinition type, string interfaceName, TypeDefinitionCache cache)
92+
public static bool ImplementsInterface (this TypeDefinition type, string interfaceName, TypeDefinitionCache? cache)
8993
{
9094
foreach (var t in type.GetTypeAndBaseTypes (cache)) {
9195
foreach (var i in t.Interfaces) {
@@ -101,7 +105,7 @@ public static bool ImplementsInterface (this TypeDefinition type, string interfa
101105
public static string GetPartialAssemblyName (this TypeReference type) =>
102106
GetPartialAssemblyName (type, cache: null);
103107

104-
public static string GetPartialAssemblyName (this TypeReference type, TypeDefinitionCache cache)
108+
public static string GetPartialAssemblyName (this TypeReference type, TypeDefinitionCache? cache)
105109
{
106110
TypeDefinition def = cache != null ? cache.Resolve (type) : type.Resolve ();
107111
return (def ?? type).Module.Assembly.Name.Name;
@@ -111,7 +115,7 @@ public static string GetPartialAssemblyName (this TypeReference type, TypeDefini
111115
public static string GetPartialAssemblyQualifiedName (this TypeReference type) =>
112116
GetPartialAssemblyQualifiedName (type, cache: null);
113117

114-
public static string GetPartialAssemblyQualifiedName (this TypeReference type, TypeDefinitionCache cache)
118+
public static string GetPartialAssemblyQualifiedName (this TypeReference type, TypeDefinitionCache? cache)
115119
{
116120
return string.Format ("{0}, {1}",
117121
// Cecil likes to use '/' as the nested type separator, while
@@ -124,7 +128,7 @@ public static string GetPartialAssemblyQualifiedName (this TypeReference type, T
124128
public static string GetAssemblyQualifiedName (this TypeReference type) =>
125129
GetAssemblyQualifiedName (type, cache: null);
126130

127-
public static string GetAssemblyQualifiedName (this TypeReference type, TypeDefinitionCache cache)
131+
public static string GetAssemblyQualifiedName (this TypeReference type, TypeDefinitionCache? cache)
128132
{
129133
TypeDefinition def = cache != null ? cache.Resolve (type) : type.Resolve ();
130134
return string.Format ("{0}, {1}",
@@ -134,7 +138,7 @@ public static string GetAssemblyQualifiedName (this TypeReference type, TypeDefi
134138
(def ?? type).Module.Assembly.Name.FullName);
135139
}
136140

137-
public static TypeDefinition GetNestedType (this TypeDefinition type, string name)
141+
public static TypeDefinition? GetNestedType (this TypeDefinition type, string name)
138142
{
139143
if (type == null)
140144
return null;
@@ -147,7 +151,7 @@ public static TypeDefinition GetNestedType (this TypeDefinition type, string nam
147151
}
148152

149153
// Note: this is not recursive, so it will not find nested types.
150-
public static TypeDefinition FindType (this ModuleDefinition module, string name)
154+
public static TypeDefinition? FindType (this ModuleDefinition module, string name)
151155
{
152156
if (module == null)
153157
return null;

src/Java.Interop.Tools.Diagnostics/Java.Interop.Tools.Diagnostics.csproj

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5+
<LangVersion>8.0</LangVersion>
6+
<Nullable>enable</Nullable>
7+
<DefineConstants>INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
58
</PropertyGroup>
69

710
<Import Project="..\..\build-tools\scripts\cecil.projitems" />
@@ -10,4 +13,8 @@
1013
<OutputPath>$(ToolOutputFullPath)</OutputPath>
1114
</PropertyGroup>
1215

16+
<ItemGroup>
17+
<Compile Include="..\utils\NullableAttributes.cs" />
18+
</ItemGroup>
19+
1320
</Project>

0 commit comments

Comments
 (0)