Commit bc5bcf4
authored
[Java.Base] Begin binding JDK-11 java.base module (dotnet#909)
Context: dotnet#858
What do *I* want? To be able to use our wonderful Java binding
infrastructure against *Desktop Java*, not just Android.
At the same time, I don't want "Android-isms" "leaking" into such a
binding. *Just* Java.Interop, no xamarin-android.
"Take over" the `generator --codegen-target=JavaInterop1` format
so that it *isn't* useful for Xamarin.Android, and is instead usable
for non-Android usage.
This is a work-in-progress, and *far* from complete. For prototype
purposes, this *only* binds:
* `java.lang.Class`
* `java.lang.Math`
* `java.lang.Number`
* `java.lang.Object`
* `java.lang.Throwable`
The `Java.Base` binding is only for .NET 6 and above. I'm not
interested in .NET Standard support at this point in time.
~~ Build Changes ~~
Additionally, this `Java.Base` binding is only for the
`java.base.jmod` file provided by JDK-11. `make prepare` is updated
to look for a JDK-11 installation path.
~~ Test Changes ~~
Update `samples/Hello` so that it (1) works, and (2) instantiates the
`Java.Lang.Object` binding:
dotnet run --project samples/Hello
Update `tests/generator-Tests` so that
`tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/JavaInterop1`
is now used *exclusively* for `generator --codegen-target=JavaInterop1`
output; `generator --codegen-target=XAJavaInterop1` now has its own
separate files in
`tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1`.
(This contributes to much of the commit diff.)
Update `tests/generator-Tests/SupportFiles` so that types in
`Android.*` are excluded when `JAVA_INTEROP1` is `#define`d, and
update the unit test infrastructure so that building for `JavaInterop1`
causes `JAVA_INTEROP1` to be `#define`d. This allows many of the unit
tests to ensure that *some* `JavaInterop1` constructs *compile*.
However, not all unit tests compile under `JavaInterop1`. The new
`BaseGeneratorTest.TryJavaInterop1` property can be set to false to
prevent compiling a test suite using `JavaInterop1`. This will allow
us to slowly implement `JavaInterop1` support over time.
The build logs will also now emit a command-line that can be used to
manually compile unit test code. See e.g.
`bin/TestDebug/TestOutput-generator-Tests.txt`.
~~ API Changes ~~
Update `Java.Interop.JavaObject` so that
`JavaObject.DisposeUnlessReferenced()` is now `virtual`.
Override `DisposeUnlessReferenced()` from the `Java*Array` types
so that if the instance was created via the new
`JniEnvironment.Arrays.CreateMarshal*Array()` methods, the array
instance will be disposed. This is intended for marshaling array
parameters:
public void WithArray(int[] array)
{
const string __id = "withArray.[I";
var native_array = JniEnvironment.Arrays.CreateMarshalInt32Array (array);
try {
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
__args [0] = new JniArgumentValue (native_array);
_members.StaticMethods.InvokeVoidMethod (__id, __args);
} finally {
if (array != null) native_array.DisposeUnlessReferenced ();
}
}
Add `Java.Interop.JavaTypeParametersAttribute(string[] typeParameters)`
from Xamarin.Android.
~~ Bindings vs. Xamarin.Android ~~
Pull in `src/Java.Base/Transforms/map.csv` [from xamarin-android][0],
removing the `android*` types.
Instead of `[Android.Runtime.RegisterAttribute]` on types, use
`[Java.Interop.JniTypeSignatureAttribute]`.
Java arrays are bound as appropriate `IList<T>`, using the
`Java.Interop.Java*Array` types as an intermediary. This should help
reduce marshaling logic & overhead, as if the "source" array is a
`Java*Array`, it doesn't need to be "deep copied". The exception is
C# `params` arrays, which continue to be bound as arrays, and are
marshaled via an appropriate `Java*Array` type.
`java.io.InputStream` isn't bound as `System.IO.Stream`, etc.
"Java.Interop-style" constructors are used (25de1f3), e.g.
// This
DeclaringType (ref JniObjectReference reference, JniObjectReferenceOptions options);
// Not Xamarin.Android-style
DeclaringType (IntPtr handle, JniHandleOwnership transfer);
"Java.Interop-style" wrapper construction is used, e.g.
// This
var wrapper = JniEnvironment.Runtime.ValueManager.GetValue<DeclaringType>(ref h, JniObjectReferenceOptions.CopyAndDispose);
// Not this
var wrapper = Java.Lang.Object.GetObject<DeclaringType>(handle);
~~ TODO: Marshal Methods ~~
Marshal methods are currently skipped. Java-to-managed invocations
are not currently supported.
Xamarin.Android uses Java Callable Wrappers + `Runtime.register()`
to specify which methods to register, via lots of reflection, etc.
Proposal: For Desktop, JCW's shouldn't need to specify all the
methods to register. Instead, use the `jnimarshalmethod-gen`-
originated strategy of `[JniAddNativeMethodRegistrationAttribute]`
within the binding, and then have it use `MarshalMethodBuilder` to
generate the marshal methods. Need to update `MarshalMethodBuilder`
to look for overrides in addition to methods with `[JavaCallable]`,
which in turn may require an equivalent to
`[Android.Runtime.RegisterAttribute(…)]`.
Perhaps `[JniMethodSignatureAttribute(string name, string sig)]`?
In the meantime, `Java.Base` will skip all marshal-method logic
plus runtime method generation. Leave that for later.
~~ TODO: Other Binding Changes? ~~
We should eventually "unify" `java.lang.Object` and `System.Object`.
Consider `java.lang.Class`:
/* partial */ class Class<T> {
public boolean isInstance(java.lang.Object);
public java.lang.Object[] getSigners();
}
If we unify `java.lang.Object` and `System.Object`, we'd have a
binding of:
partial class Class {
public bool IsInstance (object value);
public IList<object> GetSigners();
}
~~ Open Questions ~~
What's up with `java.lang.Class.getAnnotationsByType()`?
During an iteration of this PR, I got:
public unsafe Java.Interop.JavaObjectArray<Java.Lang.Object>? GetAnnotationsByType (Java.Lang.Class? annotationClass)
{
const string __id = "getAnnotationsByType.(Ljava/lang/Class;)[Ljava/lang/annotation/Annotation;";
From `__id` we see that the Java return type is `Annotation[]`, yet
we bind it as an `Object` array? This is because of
[dotnet#669][1].
That said, it's currently "differently *worse*"; I don't know why,
but `__id` is now:
const string __id = "getAnnotationsByType.(Ljava/lang/Class;)[Ljava/lang/Object;";
i.e. the return type is an `Object` array instead of an `Annotation`
array, which is wrong, as per `javap`:
% javap -s java.lang.Class
…
public <A extends java.lang.annotation.Annotation> A getAnnotation(java.lang.Class<A>);
descriptor: (Ljava/lang/Class;)Ljava/lang/annotation/Annotation;
This needs additional investigation.
[0]: https://github.com/xamarin/xamarin-android/blob/99523feab02e8622a3357e9e6a025f5afc44c970/src/Mono.Android/map.csv
[1]: dotnet#6691 parent af91b9c commit bc5bcf4
File tree
462 files changed
+13758
-5050
lines changed- build-tools
- Java.Interop.BootstrapTasks/Java.Interop.BootstrapTasks
- scripts
- samples/Hello
- src
- Java.Base
- Java.Lang
- Transforms
- Java.Interop/Java.Interop
- tests
- TestJVM
- generator-Tests
- Integration-Tests
- SupportFiles
- Unit-Tests
- CodeGeneratorExpectedResults
- Common
- JavaInterop1
- XAJavaInterop1
- expected.ji
- AccessModifiers
- Adapters
- Android.Graphics.Color
- Arrays
- CSharpKeywords
- Constructors
- Core_ClassParse
- Core_Jar2Xml
- GenericArguments
- InterfaceMethodsConflict
- NestedTypes
- NonStaticFields
- NormalMethods
- NormalProperties
- ParameterXPath
- StaticFields
- StaticMethods
- StaticProperties
- Streams
- TestInterface
- java.lang.Enum
- java.lang.Object
- java.util.List
- expected.xaji
- AccessModifiers
- Adapters
- Android.Graphics.Color
- Arrays
- CSharpKeywords
- Constructors
- Core_ClassParse
- Core_Jar2Xml
- GenericArguments
- InterfaceMethodsConflict
- NestedTypes
- NonStaticFields
- NormalMethods
- NormalProperties
- ParameterXPath
- StaticFields
- StaticMethods
- StaticProperties
- Streams
- TestInterface
- java.lang.Enum
- java.lang.Object
- java.util.List
- expected
- Adapters/SupportFiles
- Streams/SupportFiles
- tools/generator
- Java.Interop.Tools.Generator.CodeGeneration
- Java.Interop.Tools.Generator.ObjectModel
- Symbols
- Java.Interop.Tools.Generator.Transformation
- SourceWriters
- Attributes
- Extensions
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
462 files changed
+13758
-5050
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
33 | 37 | | |
34 | 38 | | |
35 | 39 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
| 108 | + | |
| 109 | + | |
108 | 110 | | |
109 | 111 | | |
110 | 112 | | |
| |||
296 | 298 | | |
297 | 299 | | |
298 | 300 | | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
299 | 305 | | |
300 | 306 | | |
301 | 307 | | |
| |||
346 | 352 | | |
347 | 353 | | |
348 | 354 | | |
| 355 | + | |
349 | 356 | | |
350 | 357 | | |
351 | 358 | | |
| |||
Lines changed: 45 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
22 | 24 | | |
23 | 25 | | |
24 | 26 | | |
| |||
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
31 | | - | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| 40 | + | |
39 | 41 | | |
40 | 42 | | |
41 | | - | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
42 | 46 | | |
43 | | - | |
| 47 | + | |
| 48 | + | |
44 | 49 | | |
45 | 50 | | |
46 | 51 | | |
| |||
56 | 61 | | |
57 | 62 | | |
58 | 63 | | |
59 | | - | |
60 | | - | |
61 | 64 | | |
62 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
63 | 70 | | |
64 | 71 | | |
65 | 72 | | |
66 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
67 | 89 | | |
68 | 90 | | |
69 | 91 | | |
| |||
97 | 119 | | |
98 | 120 | | |
99 | 121 | | |
100 | | - | |
101 | 122 | | |
| 123 | + | |
102 | 124 | | |
103 | 125 | | |
104 | | - | |
| 126 | + | |
105 | 127 | | |
106 | | - | |
| 128 | + | |
107 | 129 | | |
108 | | - | |
| 130 | + | |
109 | 131 | | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
121 | 138 | | |
122 | 139 | | |
123 | 140 | | |
124 | | - | |
| 141 | + | |
125 | 142 | | |
126 | | - | |
| 143 | + | |
127 | 144 | | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
| 145 | + | |
132 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
133 | 151 | | |
134 | 152 | | |
135 | 153 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | | - | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
25 | 37 | | |
26 | 38 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
13 | 21 | | |
14 | 22 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
| 7 | + | |
12 | 8 | | |
13 | 9 | | |
14 | 10 | | |
| |||
18 | 14 | | |
19 | 15 | | |
20 | 16 | | |
| 17 | + | |
| 18 | + | |
21 | 19 | | |
22 | 20 | | |
23 | 21 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
8 | | - | |
| 10 | + | |
9 | 11 | | |
10 | | - | |
| 12 | + | |
11 | 13 | | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
17 | 35 | | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
18 | 44 | | |
19 | 45 | | |
20 | 46 | | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
21 | 63 | | |
22 | 64 | | |
23 | 65 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
0 commit comments