Commit 01d0684
authored
[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-forgiving1 parent c19794e commit 01d0684
File tree
99 files changed
+3071
-158
lines changed- src
- Java.Interop.Tools.JavaCallableWrappers
- Java.Interop.Tools.TypeNameMappings/Java.Interop.Tools.TypeNameMappings
- Java.Interop
- Java.Interop
- tests/generator-Tests/Unit-Tests
- CodeGeneratorExpectedResults
- Common
- JavaInterop1
- XAJavaInterop1-NRT
- XAJavaInterop1
- XamarinAndroid
- tools/generator
- Java.Interop.Tools.Generator.CodeGeneration
- Java.Interop.Tools.Generator.Importers
- Java.Interop.Tools.Generator.ObjectModel
- Symbols
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
99 files changed
+3071
-158
lines changedLines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
| |||
23 | 25 | | |
24 | 26 | | |
25 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
26 | 31 | | |
27 | 32 | | |
28 | 33 | | |
| |||
Lines changed: 47 additions & 43 deletions
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
161 | | - | |
| 161 | + | |
162 | 162 | | |
163 | 163 | | |
164 | 164 | | |
| |||
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
172 | | - | |
| 172 | + | |
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
| |||
Lines changed: 3 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
203 | 204 | | |
204 | 205 | | |
205 | 206 | | |
206 | | - | |
| 207 | + | |
207 | 208 | | |
208 | 209 | | |
209 | 210 | | |
| |||
213 | 214 | | |
214 | 215 | | |
215 | 216 | | |
216 | | - | |
| 217 | + | |
217 | 218 | | |
218 | 219 | | |
219 | 220 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
158 | | - | |
| 158 | + | |
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
| |||
Lines changed: 16 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
199 | 199 | | |
200 | 200 | | |
201 | 201 | | |
202 | | - | |
| 202 | + | |
203 | 203 | | |
204 | 204 | | |
205 | 205 | | |
| |||
261 | 261 | | |
262 | 262 | | |
263 | 263 | | |
264 | | - | |
| 264 | + | |
265 | 265 | | |
266 | 266 | | |
267 | 267 | | |
| |||
396 | 396 | | |
397 | 397 | | |
398 | 398 | | |
| 399 | + | |
399 | 400 | | |
| 401 | + | |
400 | 402 | | |
401 | 403 | | |
402 | 404 | | |
| |||
473 | 475 | | |
474 | 476 | | |
475 | 477 | | |
| 478 | + | |
476 | 479 | | |
| 480 | + | |
477 | 481 | | |
478 | 482 | | |
479 | 483 | | |
| |||
607 | 611 | | |
608 | 612 | | |
609 | 613 | | |
610 | | - | |
| 614 | + | |
611 | 615 | | |
612 | 616 | | |
613 | 617 | | |
614 | 618 | | |
615 | | - | |
| 619 | + | |
616 | 620 | | |
617 | 621 | | |
618 | 622 | | |
| |||
621 | 625 | | |
622 | 626 | | |
623 | 627 | | |
624 | | - | |
| 628 | + | |
625 | 629 | | |
626 | 630 | | |
627 | 631 | | |
628 | 632 | | |
629 | 633 | | |
630 | 634 | | |
631 | 635 | | |
632 | | - | |
| 636 | + | |
633 | 637 | | |
634 | 638 | | |
635 | 639 | | |
| |||
694 | 698 | | |
695 | 699 | | |
696 | 700 | | |
697 | | - | |
| 701 | + | |
698 | 702 | | |
699 | 703 | | |
700 | 704 | | |
701 | 705 | | |
702 | | - | |
| 706 | + | |
703 | 707 | | |
704 | 708 | | |
705 | 709 | | |
| |||
720 | 724 | | |
721 | 725 | | |
722 | 726 | | |
723 | | - | |
| 727 | + | |
724 | 728 | | |
725 | 729 | | |
726 | 730 | | |
727 | 731 | | |
728 | | - | |
| 732 | + | |
729 | 733 | | |
730 | 734 | | |
731 | 735 | | |
| |||
748 | 752 | | |
749 | 753 | | |
750 | 754 | | |
751 | | - | |
| 755 | + | |
752 | 756 | | |
753 | 757 | | |
754 | 758 | | |
| |||
765 | 769 | | |
766 | 770 | | |
767 | 771 | | |
768 | | - | |
| 772 | + | |
769 | 773 | | |
770 | 774 | | |
771 | 775 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | | - | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| |||
0 commit comments