You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 9, 2026. It is now read-only.
Repository navigation
Commit 59d86de
Browse filesBrowse the repository at this point in the historyBrowse files
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
// Metadata.xml XPath method reference: path="/api/package[@name='java.code']/interface[@name='IMyInterface']/method[@name='set_Key' and count(parameter)=1 and parameter[1][@type='java.lang.String']]"
// Metadata.xml XPath method reference: path="/api/package[@name='java.code']/interface[@name='IMyInterface']/method[@name='set_Key' and count(parameter)=1 and parameter[1][@type='java.lang.String']]"
0 commit comments