Skip to content

Commit

Permalink
[.Localization, .Cecil, .Diagnostics, .Generator] $(Nullable)=enable (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
jpobst committed Dec 17, 2020
1 parent 2f62ffd commit 3f6cf72
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<DefineConstants>INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand All @@ -16,6 +19,10 @@
</Compile>
</ItemGroup>

<ItemGroup>
<Compile Include="..\utils\NullableAttributes.cs" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Resources.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
Expand Down
7 changes: 7 additions & 0 deletions src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<DefineConstants>INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
</PropertyGroup>

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

<ItemGroup>
<Compile Include="..\utils\NullableAttributes.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Java.Interop.Localization\Java.Interop.Localization.csproj" />
<ProjectReference Include="..\Java.Interop.Tools.Diagnostics\Java.Interop.Tools.Diagnostics.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class DirectoryAssemblyResolver : IAssemblyResolver {

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

Dictionary<string, AssemblyDefinition> cache;
Dictionary<string, AssemblyDefinition?> cache;
bool loadDebugSymbols;
Action<TraceLevel, string> logger;

Expand All @@ -71,18 +71,18 @@ public class DirectoryAssemblyResolver : IAssemblyResolver {
};

[Obsolete ("Use DirectoryAssemblyResolver(Action<TraceLevel, string>, bool, ReaderParameters)")]
public DirectoryAssemblyResolver (Action<string, object[]> logWarnings, bool loadDebugSymbols, ReaderParameters loadReaderParameters = null)
public DirectoryAssemblyResolver (Action<string, object[]> logWarnings, bool loadDebugSymbols, ReaderParameters? loadReaderParameters = null)
: this ((TraceLevel level, string value) => logWarnings?.Invoke ("{0}", new[]{value}), loadDebugSymbols, loadReaderParameters)
{
if (logWarnings == null)
throw new ArgumentNullException (nameof (logWarnings));
}

public DirectoryAssemblyResolver (Action<TraceLevel, string> logger, bool loadDebugSymbols, ReaderParameters loadReaderParameters = null)
public DirectoryAssemblyResolver (Action<TraceLevel, string> logger, bool loadDebugSymbols, ReaderParameters? loadReaderParameters = null)
{
if (logger == null)
throw new ArgumentNullException (nameof (logger));
cache = new Dictionary<string, AssemblyDefinition> ();
cache = new Dictionary<string, AssemblyDefinition?> ();
this.loadDebugSymbols = loadDebugSymbols;
this.logger = logger;
SearchDirectories = new List<string> ();
Expand All @@ -100,14 +100,14 @@ protected virtual void Dispose (bool disposing)
if (!disposing || cache == null)
return;
foreach (var e in cache) {
e.Value.Dispose ();
e.Value?.Dispose ();
}
cache = null;
cache.Clear ();
}

public Dictionary<string, AssemblyDefinition> ToResolverCache ()
public Dictionary<string, AssemblyDefinition?> ToResolverCache ()
{
return new Dictionary<string, AssemblyDefinition>(cache);
return new Dictionary<string, AssemblyDefinition?>(cache);
}

public bool AddToCache (AssemblyDefinition assembly)
Expand All @@ -122,12 +122,12 @@ public bool AddToCache (AssemblyDefinition assembly)
return true;
}

public virtual AssemblyDefinition Load (string fileName, bool forceLoad = false)
public virtual AssemblyDefinition? Load (string fileName, bool forceLoad = false)
{
if (!File.Exists (fileName))
return null;

AssemblyDefinition assembly = null;
AssemblyDefinition? assembly = null;
var name = Path.GetFileNameWithoutExtension (fileName);
if (!forceLoad && cache.TryGetValue (name, out assembly))
return assembly;
Expand Down Expand Up @@ -181,7 +181,7 @@ public AssemblyDefinition Resolve (string fullName)
return Resolve (fullName, null);
}

public AssemblyDefinition Resolve (string fullName, ReaderParameters parameters)
public AssemblyDefinition Resolve (string fullName, ReaderParameters? parameters)
{
return Resolve (AssemblyNameReference.Parse (fullName), parameters);
}
Expand All @@ -200,7 +200,7 @@ public string FindAssemblyFile (AssemblyNameReference reference)
{
var name = reference.Name;

string assembly;
string? assembly;
foreach (var dir in SearchDirectories)
if ((assembly = SearchDirectory (name, dir)) != null)
return assembly;
Expand All @@ -216,20 +216,24 @@ public string FindAssemblyFile (AssemblyNameReference reference)
name + ".dll");
}

public AssemblyDefinition Resolve (AssemblyNameReference reference, ReaderParameters parameters)
public AssemblyDefinition Resolve (AssemblyNameReference reference, ReaderParameters? parameters)
{
var name = reference.Name;

AssemblyDefinition assembly;
if (cache.TryGetValue (name, out assembly))
AssemblyDefinition? assembly;
if (cache.TryGetValue (name, out assembly)) {
if (assembly is null)
throw CreateLoadException (reference);

return assembly;
}

string assemblyFile;
AssemblyDefinition candidate = null;
string? assemblyFile;
AssemblyDefinition? candidate = null;
foreach (var dir in SearchDirectories) {
if ((assemblyFile = SearchDirectory (name, dir)) != null) {
var loaded = Load (assemblyFile);
if (Array.Equals (loaded.Name.MetadataToken, reference.MetadataToken))
if (Array.Equals (loaded?.Name.MetadataToken, reference.MetadataToken))
return loaded;
candidate = candidate ?? loaded;
}
Expand All @@ -238,18 +242,23 @@ public AssemblyDefinition Resolve (AssemblyNameReference reference, ReaderParame
if (candidate != null)
return candidate;

throw new System.IO.FileNotFoundException (
throw CreateLoadException (reference);
}

static FileNotFoundException CreateLoadException (AssemblyNameReference reference)
{
return new System.IO.FileNotFoundException (
string.Format ("Could not load assembly '{0}, Version={1}, Culture={2}, PublicKeyToken={3}'. Perhaps it doesn't exist in the Mono for Android profile?",
name,
reference.Version,
string.IsNullOrEmpty (reference.Culture) ? "neutral" : reference.Culture,
reference.Name,
reference.Version,
string.IsNullOrEmpty (reference.Culture) ? "neutral" : reference.Culture,
reference.PublicKeyToken == null
? "null"
: string.Join ("", reference.PublicKeyToken.Select(b => b.ToString ("x2")))),
name + ".dll");
: string.Join ("", reference.PublicKeyToken.Select (b => b.ToString ("x2")))),
reference.Name + ".dll");
}

string SearchDirectory (string name, string directory)
string? SearchDirectory (string name, string directory)
{
if (Path.IsPathRooted (name) && File.Exists (name))
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class MethodDefinitionRocks
public static MethodDefinition GetBaseDefinition (this MethodDefinition method) =>
GetBaseDefinition (method, cache: null);

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

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

public static bool AreParametersCompatibleWith (this Collection<ParameterDefinition> a, Collection<ParameterDefinition> b, TypeDefinitionCache cache)
public static bool AreParametersCompatibleWith (this Collection<ParameterDefinition> a, Collection<ParameterDefinition> b, TypeDefinitionCache? cache)
{
if (a.Count != b.Count)
return false;
Expand All @@ -66,15 +66,15 @@ public static bool AreParametersCompatibleWith (this Collection<ParameterDefinit
return true;
}

static bool IsParameterCompatibleWith (IModifierType a, IModifierType b, TypeDefinitionCache cache)
static bool IsParameterCompatibleWith (IModifierType a, IModifierType b, TypeDefinitionCache? cache)
{
if (!IsParameterCompatibleWith (a.ModifierType, b.ModifierType, cache))
return false;

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

static bool IsParameterCompatibleWith (TypeSpecification a, TypeSpecification b, TypeDefinitionCache cache)
static bool IsParameterCompatibleWith (TypeSpecification a, TypeSpecification b, TypeDefinitionCache? cache)
{
if (a is GenericInstanceType)
return IsParameterCompatibleWith ((GenericInstanceType) a, (GenericInstanceType) b, cache);
Expand All @@ -85,7 +85,7 @@ static bool IsParameterCompatibleWith (TypeSpecification a, TypeSpecification b,
return IsParameterCompatibleWith (a.ElementType, b.ElementType, cache);
}

static bool IsParameterCompatibleWith (GenericInstanceType a, GenericInstanceType b, TypeDefinitionCache cache)
static bool IsParameterCompatibleWith (GenericInstanceType a, GenericInstanceType b, TypeDefinitionCache? cache)
{
if (!IsParameterCompatibleWith (a.ElementType, b.ElementType, cache))
return false;
Expand All @@ -103,7 +103,7 @@ static bool IsParameterCompatibleWith (GenericInstanceType a, GenericInstanceTyp
return true;
}

static bool IsParameterCompatibleWith (TypeReference a, TypeReference b, TypeDefinitionCache cache)
static bool IsParameterCompatibleWith (TypeReference a, TypeReference b, TypeDefinitionCache? cache)
{
if (a is TypeSpecification || b is TypeSpecification) {
if (a.GetType () != b.GetType ())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace Java.Interop.Tools.Cecil {
public static class TypeDefinitionRocks {

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

public static TypeDefinition GetBaseType (this TypeDefinition type, TypeDefinitionCache cache)
public static TypeDefinition? GetBaseType (this TypeDefinition type, TypeDefinitionCache? cache)
{
var bt = type.BaseType;
if (bt == null)
Expand All @@ -25,30 +25,34 @@ public static TypeDefinition GetBaseType (this TypeDefinition type, TypeDefiniti
public static IEnumerable<TypeDefinition> GetTypeAndBaseTypes (this TypeDefinition type) =>
GetTypeAndBaseTypes (type, cache: null);

public static IEnumerable<TypeDefinition> GetTypeAndBaseTypes (this TypeDefinition type, TypeDefinitionCache cache)
public static IEnumerable<TypeDefinition> GetTypeAndBaseTypes (this TypeDefinition type, TypeDefinitionCache? cache)
{
while (type != null) {
yield return type;
type = type.GetBaseType (cache);
TypeDefinition? t = type;

while (t != null) {
yield return t;
t = t.GetBaseType (cache);
}
}

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

public static IEnumerable<TypeDefinition> GetBaseTypes (this TypeDefinition type, TypeDefinitionCache cache)
public static IEnumerable<TypeDefinition> GetBaseTypes (this TypeDefinition type, TypeDefinitionCache? cache)
{
while ((type = type.GetBaseType (cache)) != null) {
yield return type;
TypeDefinition? t = type;

while ((t = t.GetBaseType (cache)) != null) {
yield return t;
}
}

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

public static bool IsAssignableFrom (this TypeReference type, TypeReference c, TypeDefinitionCache cache)
public static bool IsAssignableFrom (this TypeReference type, TypeReference c, TypeDefinitionCache? cache)
{
if (type.FullName == c.FullName)
return true;
Expand All @@ -71,7 +75,7 @@ public static bool IsAssignableFrom (this TypeReference type, TypeReference c, T
public static bool IsSubclassOf (this TypeDefinition type, string typeName) =>
IsSubclassOf (type, typeName, cache: null);

public static bool IsSubclassOf (this TypeDefinition type, string typeName, TypeDefinitionCache cache)
public static bool IsSubclassOf (this TypeDefinition type, string typeName, TypeDefinitionCache? cache)
{
foreach (var t in type.GetTypeAndBaseTypes (cache)) {
if (t.FullName == typeName) {
Expand All @@ -85,7 +89,7 @@ public static bool IsSubclassOf (this TypeDefinition type, string typeName, Type
public static bool ImplementsInterface (this TypeDefinition type, string interfaceName) =>
ImplementsInterface (type, interfaceName, cache: null);

public static bool ImplementsInterface (this TypeDefinition type, string interfaceName, TypeDefinitionCache cache)
public static bool ImplementsInterface (this TypeDefinition type, string interfaceName, TypeDefinitionCache? cache)
{
foreach (var t in type.GetTypeAndBaseTypes (cache)) {
foreach (var i in t.Interfaces) {
Expand All @@ -101,7 +105,7 @@ public static bool ImplementsInterface (this TypeDefinition type, string interfa
public static string GetPartialAssemblyName (this TypeReference type) =>
GetPartialAssemblyName (type, cache: null);

public static string GetPartialAssemblyName (this TypeReference type, TypeDefinitionCache cache)
public static string GetPartialAssemblyName (this TypeReference type, TypeDefinitionCache? cache)
{
TypeDefinition def = cache != null ? cache.Resolve (type) : type.Resolve ();
return (def ?? type).Module.Assembly.Name.Name;
Expand All @@ -111,7 +115,7 @@ public static string GetPartialAssemblyName (this TypeReference type, TypeDefini
public static string GetPartialAssemblyQualifiedName (this TypeReference type) =>
GetPartialAssemblyQualifiedName (type, cache: null);

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

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

public static TypeDefinition GetNestedType (this TypeDefinition type, string name)
public static TypeDefinition? GetNestedType (this TypeDefinition type, string name)
{
if (type == null)
return null;
Expand All @@ -147,7 +151,7 @@ public static TypeDefinition GetNestedType (this TypeDefinition type, string nam
}

// Note: this is not recursive, so it will not find nested types.
public static TypeDefinition FindType (this ModuleDefinition module, string name)
public static TypeDefinition? FindType (this ModuleDefinition module, string name)
{
if (module == null)
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<DefineConstants>INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
</PropertyGroup>

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

<ItemGroup>
<Compile Include="..\utils\NullableAttributes.cs" />
</ItemGroup>

</Project>
Loading

0 comments on commit 3f6cf72

Please sign in to comment.