Commit 22d5687
authored
Fixes: #981
Context: 5a0e37e
Add support for `//class/method[@managedOverride = 'none']` and
`//interface/method[@managedOverride = 'reabstract']`.
Setting `@managedOverride` to `none` ensures that the member is not
marked with `virtual` or `override`. This is useful for `sealed`
classes, to avoid a [CS0549 error][0]. Previously, given the Java:
// Java
public final class MyClass {
public void doThing() {/* … */ }
}
The `class-parse` XML would specify that `MyClass.doThing()` was
"virtual" -- `@abstract` is false, `@final` is false:
<class name="MyClass …>
<method
abstract="false"
final="false"
name="doThing"
return="void"
… />
</class>
This would result in the C# binding:
// C#
public sealed partial class MyClass {
public virtual void DoThing() => …
}
which would error out with a CS0549:
error CS0549: 'MyClass.DoThing()' is a new virtual member in sealed type 'MyClass'
This can now be resolved by setting `@managedOverride` to `none`:
<attr path="//class[@name='MyClass']/method[@name='doThing']"
name="managedOverride">none</attr>
which will result in the compilable binding:
// C#
public sealed partial class MyClass {
public void DoThing() => …
}
Setting `@managedOverride` to `reabstract` is part of support for
re-abstracting interface members; see also a65d6fb. Currently in
`src/Java.Base`, we have Java:
// Java
public interface AnnotatedType {
default AnnotatedType getAnnotatedOwnerType() {…}
}
public interface AnnotatedArrayType implements AnnotatedType {
AnnotatedType getAnnotatedOwnerType(); // re-abstract interface default method
}
which results in the C# binding:
// C#
public partial interface IAnnotatedType {
virtual IAnnotatedType? AnnotatedOwnerType {
get => …
}
}
public partial interface IAnnotatedArrayType : IAnnotatedType {
IAnnotatedType? AnnotatedOwnerType { get; } // CS0108
}
which results in a [warning CS0108][1]:
warning CS0108: 'IAnnotatedArrayType.AnnotatedOwnerType' hides inherited member
'IAnnotatedType.AnnotatedOwnerType'. Use the new keyword if hiding was intended.
Fixing this requires two steps. First, we can now set
`//interface/method/@managedOverride` to `reabstract`:
<attr path="//interface[@name='AnnotatedArrayType']/method[@name='getAnnotatedOwnerType']"
name="managedOverride">reabstract</attr>
What we *also* need to do is make the member *explicitly qualified*.
This can be "forced" for *properties* by setting `@propertyName`:
<attr path="//interface[@name='AnnotatedArrayType']/method[@name='getAnnotatedOwnerType']"
name="propertyName">IAnnotatedType.AnnotatedOwnerType</attr>
`@managedOverride` and `@propertyName` work together to emit:
public partial interface IAnnotatedArrayType : IAnnotatedType {
abstract IAnnotatedType? IAnnotatedType.AnnotatedOwnerType {get;}
}
The problem is that this combination probably breaks Android output,
and it can't be used for *method* overrides, e.g. having
`java.io.Closeable.close()` re-abstract `java.lang.AutoCloseable.close()`.
TODO: complete the "interface reabstract" case, possibly via
`//interface/method[@explicitInterface='ManagedInterfaceName']`:
<attr path="//interface[@name='AnnotatedArrayType']/method[@name='getAnnotatedOwnerType']"
name="explicitInterface">IAnnotatedType</attr>
[0]: https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0549
[1]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0108
1 parent 7f1d2d7 commit 22d5687
File tree
5 files changed
+114
-0
lines changed- tests/generator-Tests/Unit-Tests
- tools/generator/SourceWriters
5 files changed
+114
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
147 | 147 | | |
148 | 148 | | |
149 | 149 | | |
| 150 | + | |
| 151 | + | |
| 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 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
150 | 200 | | |
151 | 201 | | |
152 | 202 | | |
| |||
195 | 245 | | |
196 | 246 | | |
197 | 247 | | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
198 | 298 | | |
199 | 299 | | |
200 | 300 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
28 | 32 | | |
29 | 33 | | |
30 | 34 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
23 | 27 | | |
24 | 28 | | |
25 | 29 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
61 | 64 | | |
62 | 65 | | |
63 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
69 | 72 | | |
70 | 73 | | |
71 | 74 | | |
| |||
0 commit comments