-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[generator] Add nullable reference types (NRT) support. (#563)
Fixes: #468 Context: dotnet/android#4227 Add [C#8 nullable reference type][0] (NRT) support to `generator` when given `generator -lang-features=nullable-reference-types`. This uses a variety of Java annotations to infer nullable information (18c29b7) via the `//method/@return-not-null` and `//parameter/@not-null` attribute values within `api.xml` to "forward" nullability information to the generated C# binding. ~~ Goals ~~ `generator` should be able to interpret the nullable annotations provided by an input `.jar` file (via `class-parse`). It should use this information to generate bindings that expose similar nullable annotations on the produced public C# API. For example, this Java: // Java public class Foo { public void bar (@NotNull Object baz, String value) { … } } Should generate this C# API: // C# Binding public class Foo : Java.Lang.Object { public void Bar (Java.Lang.Object baz, string? value) { … } } Additionally, the generated binding code should not produce any additional warnings *on its own*. That is, the internal plumbing code itself should not create warnings. ~~ Non-Goals ~~ There exists cases in our generated plumbing code that do not play nicely with the provability of C#8 nullable reference types. For example, we may generate code like this: int Java.Lang.IComparable.CompareTo (Java.Lang.Object o) { return CompareTo (global::Java.Interop.JavaObjectExtensions.JavaCast<Android.Util.Half>(o)); } Technically `.JavaCast<>()` can return `null`, which cannot be passed to `.CompareTo (object o)` because it does not accept `null`. In these cases we liberally use the [null forgiving operator (`!`)][1] to suppress warnings. It may be desirable to change how this code is structured to be better provably `null`-safe, however this PR does not attempt to make those modification. It is assumed that the code is currently working, so `null` is prevented here via other mechanisms. No functional changes are made to generated code. Additionally, there are cases where Java nullable annotations can create scenarios that will produce warnings in C#, particularly around inheritance. For example: // Java public class Base { public void m (@NotNull Object baz) { … } } public class Derived extends Base { @OverRide public void m (Object baz) { … } } This would produce a C# warning such as: CS8610: Nullability of reference types in type of parameter 'M' doesn't match overridden member. `generator` will not attempt to resolve this error, it is an exercise for the user. This can be accomplished by fixing the Java code or using `metadata` to override the `//@not-null` attribute such as: <attr path="/api/package[@name='blah']/class[@name='Foo2']/method[@name='Bar' and count(parameter)=1 and parameter[1][@type='object']]/parameter" name="not-null">true</attr> ~~ Unit Test Changes ~~ Several of the unit test "expected output" files changed their property type from `java.lang.String` to `string`. This occurred due to a related refactoring of parameter & return type generation code. This change shouldn't be "user visible" because the unit tests don't go through a "complete" pipeline which would involve ensuring that get- and set-method pairs have consistent parameter & return types. [0]: https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references [1]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-forgiving
- Loading branch information
Showing
99 changed files
with
3,071 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 47 additions & 43 deletions
90
...terop.Tools.TypeNameMappings/Java.Interop.Tools.TypeNameMappings/JavaNativeTypeManager.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...nit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1-NRT/WriteCharSequenceEnumerator.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () | ||
{ | ||
return GetEnumerator (); | ||
} | ||
|
||
public System.Collections.Generic.IEnumerator<char> GetEnumerator () | ||
{ | ||
for (int i = 0; i < Length(); i++) | ||
yield return CharAt (i); | ||
} | ||
|
Oops, something went wrong.