Commit 0045292
[generator] Support Buffer.duplicate() binding in API-34 (#1086)
Context: dotnet/android#7796
Context: 22d5687
Context: 5a0e37e
Imagine the following covariant-return-type -using construct, which
is new in API-34 `android.jar`:
// Java
public abstract class Buffer {
public abstract Buffer duplicate ();
}
public abstract class CharBuffer extends Buffer {
public abstract CharBuffer duplicate ();
}
By default this is bound as:
// C#
[Register(…)]
public abstract partial class Buffer {
[Register(…)]
public abstract Buffer Duplicate ();
}
[Register(…)]
public abstract partial class CharBuffer : Buffer {
[Register(…)]
public abstract CharBuffer Duplicate ();
}
Alas, this results in [error CS0533][0]:
error CS0533: 'CharBuffer.Duplicate()' hides inherited abstract member 'Buffer.Duplicate()'
C# supports this semantic if we use [`abstract override`][1], similar
to [reabstraction of Default Interface Methods][2] in 22d5687:
public abstract class CharBuffer : Buffer {
public override abstract CharBuffer Duplicate ();
}
Theoretically, we can tell `generator` how to fix this via the
`//attr[@name='managedOverride']` metadata property (5a0e37e):
<attr path="/api/package[@name='java.nio']/class[@name='CharBuffer']/method[@name='duplicate']"
name="managedOverride">override</attr>
However, `//attr[@name='managedOverride']` was not written to support
updating`abstract` methods.
Fix that oversight so that using `//attr[@name='managedOverride']`
can be used to emit `override abstract`:
[Register(…)]
public abstract partial class CharBuffer : Buffer {
[Register(…)]
public override abstract CharBuffer Duplicate ();
}
~~ Method Invokers ~~
This gets us halfway there, but there is another issue: method invokers
for both the class and base class methods are *also* emitted into the
`*Invoker` subclass:
partial class BufferInvoker : CharBuffer {
public override sealed unsafe Buffer Duplicate() {…}
// so far so good…
}
partial class CharBufferInvoker : CharBuffer {
[Register ("duplicate", "()Ljava/nio/Buffer;", "")]
public override sealed unsafe Buffer Duplicate() {…}
[Register ("duplicate", "()Ljava/nio/CharBuffer;", "")]
public override sealed unsafe CharBuffer Duplicate() {…}
// uh oh
}
This results in the following error:
error CS0111: Type 'CharBufferInvoker' already defines a member called 'Duplicate' with the same parameter types
error CS0508: 'CharBufferInvoker.Duplicate()': return type must be 'CharBuffer' to match overridden member 'CharBuffer.Duplicate()'
This perhaps(?) could be something `generator` could detect and
prevent, but the feasibility and cost is unknown and expected to be
high. Since this is a very rare case, we will fix it with metadata,
however we have never had metadata to apply to invokers before.
We have decided to support `//attr[@name='skipInvokerMethods']`
metadata on the `class` that the invoker class would be created for:
<attr path="/api/package[@name='java.nio']/class[@name='CharBuffer']"
name="skipInvokerMethods">java/nio/Buffer.duplicate()Ljava/nio/Buffer;</attr>
The value is a comma, space, and newline-separated list of
"JNI signature-like" values consisting of:
1. The simplified JNI name of the declaring class that contains the
the invoker method to skip, e.g. `java/nio/Buffer`
2. `.`
3. The Java name of the invoker method to skip, e.g. `duplicate`.
4. The JNI method signature of the method to skip, e.g.
`()Ljava/nio/Buffer;`
The above `java/nio/Buffer.duplicate()Ljava/nio/Buffer;` value tells
`generator` to skip generating the `Buffer Buffer.duplicate ()` invoker
method when generating the `CharBufferInvoker` type. Multiple invokers
to skip can be specified as a comma or space separated list.
[0]: https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0533
[1]: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes#1467-abstract-methods
[2]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods#reabstraction
(cherry picked from commit 73ebad2)1 parent 4a79bea commit 0045292
File tree
6 files changed
+75
-7
lines changed- src/Xamarin.SourceWriter/Models
- tests/generator-Tests/Unit-Tests
- tools/generator
- Java.Interop.Tools.Generator.Importers
- Java.Interop.Tools.Generator.ObjectModel
- SourceWriters
6 files changed
+75
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
91 | | - | |
| 91 | + | |
| 92 | + | |
92 | 93 | | |
93 | 94 | | |
94 | 95 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
153 | 177 | | |
154 | 178 | | |
155 | 179 | | |
| |||
298 | 322 | | |
299 | 323 | | |
300 | 324 | | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
301 | 355 | | |
302 | 356 | | |
303 | 357 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
119 | 123 | | |
120 | 124 | | |
121 | 125 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
339 | 340 | | |
340 | 341 | | |
341 | 342 | | |
| 343 | + | |
| 344 | + | |
342 | 345 | | |
343 | 346 | | |
344 | 347 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
47 | 50 | | |
48 | 51 | | |
49 | 52 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
| 67 | + | |
68 | 68 | | |
69 | 69 | | |
70 | | - | |
| 70 | + | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
| 73 | + | |
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | | - | |
| 77 | + | |
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | | - | |
| 81 | + | |
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
| 103 | + | |
104 | 104 | | |
105 | 105 | | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
106 | 109 | | |
107 | 110 | | |
108 | 111 | | |
| |||
0 commit comments