Commit 0c90cf5
authored
[generator] Add support for <ns-replace> metadata. (#939)
Fixes: #727
Today, when `generator` creates a .NET `namespace` from a
Java `package`, it applies a simple PascalCase transformation to make
the namespace match established .NET naming standards. For example.
`package android.database` becomes `namespace Android.Database`.
However there are a few scenarios that this is not a good fit for:
1. Word phrases where upper-casing only the first letter is not
desirable, e.g. `package androidx` to `namespace AndroidX`.
2. When Java package names are longer than the desired C# namespaces;
`com.` is a common Java package prefix, but isn't common in C#:
`package com.google.android.material.animation` should become
`namespace Google.Android.Material.Animation`.
Both of these scenarios can require many repeated `metadata` lines
[to fix][0]:
<attr path="/api/package[@name='androidx.core.accessibilityservice']" name="managedName">AndroidX.Core.AccessibilityService</attr>
<attr path="/api/package[@name='androidx.core.app']" name="managedName">AndroidX.Core.App</attr>
…
Improve support for this scenario by adding a support for a new
`<ns-replace/>` element to `Metadata.xml` transform files.
(We may also add support for an `@(AndroidNamespaceReplacement)` item
group as suggested in #727 in the future.)
<metadata>
<ns-replace source='Androidx' replacement='Xamarin.AndroidX' />
<ns-replace source='.Androidx.' replacement='Xamarin.AndroidX' />
<ns-replace source='Com' replacement='' />
<ns-replace source='Com.Google.' replacement='Google' />
<ns-replace source='.Compose' replacement='ComposeUI' />
</metadata>
The `//ns-replace/@source` attribute is a Java [PackageName][1] to
replace. Note: the Java PackageName grammar is:
> *PackageName*:
> - *Identifier*
> - *PackageName* `.` *Identifier*
The `//ns-replace` values *do not* override the [managedName][2]
attribute if already specified within metadata.
The `//ns-replace/@source` attribute:
* Specifies a "match"; when it matches, instances of `@source` are
replaced with `@replacement`.
* Is interpreted in a *case-insensitive* manner; `Androidx` matches
`androidx` and `AndroidX` and `ANDROIDX`.
* Only matches on full [Identifier][3]s and [PackageName][1]s;
<ns-replace source='Com' replacement='' />
Matches `Com.Google.Library`, but not `Common.Google.Library` or
`Google.Imaging.Dicom`.
* Multiple "dotted" Identifiers may be used, and all may match
*anywhere*, *in order*, in the Java package name.
<ns-replace source='Com.Google' replacement='Google' />
will match `com.google.library` and `example.com.google`, but
won't match `Common.Google` or `Com.Googles`.
* *May start with* a `.`, which means that `//ns-replace/@source`
is only matched at the *end* of a Java package name:
<ns-replace source='.Compose' replacement='ComposeUI' />
will match `com.google.androidx.compose`,
but not `com.google.androidx.compose.writer`.
* *May end with* a `.`, which means that `//ns-replace/@source` is
only matched at the *beginning* of the package name:
<ns-replace source='Androidx.' replacement='Xamarin.AndroidX' />
matches `androidx.core`, but not `com.square.okhttp.androidx`.
* May both begin and end with a `.`, which means that only exact
(case insensitive) Java package names are matched:
<ns-replace source='.Androidx.' replacement='Xamarin.AndroidX' />
matches `androidx`, but not `com.google.androidx.core`.
Additionally, duplicate `//ns-replace/@source` values are *ignored*.
`//ns-replace` elements are processed "in order" for a given
`Metadata.xml` file, but the ordering of `generator --fixup` files /
`@(TransformFile)`s is unspecified.
Multiple replacements may affect a single namespace:
<ns-replace source='Androidx' replacement='Xamarin.AndroidX' />
<ns-replace source='View' replacement='Views' />
changes `Androidx.View` to `Xamarin.AndroidX.Views`.
[0]: https://github.com/xamarin/AndroidX/blob/f97553ff428f9b6ea754f173567d220048245a16/source/androidx.core/core/Transforms/Metadata.Namespaces.xml#L10-L34
[1]: https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.5
[2]: https://docs.microsoft.com/en-us/xamarin/android/platform/binding-java-library/customizing-bindings/java-bindings-metadata#managedname
[3]: https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-Identifier1 parent 4cbc07a commit 0c90cf5
File tree
36 files changed
+538
-13
lines changed- src
- Java.Interop.Localization
- xlf
- Java.Interop.Tools.Generator
- Extensions
- Metadata
- Utilities
- tests
- Java.Interop.Tools.Generator-Tests/Metadata
- generator-Tests
- Integration-Tests
- Unit-Tests
- expected.ji/Core_ClassParse
- expected.xaji/Core_ClassParse
- expected/Core_ClassParse
- tools/generator
- Java.Interop.Tools.Generator.Importers
36 files changed
+538
-13
lines changedSome generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
286 | 286 | | |
287 | 287 | | |
288 | 288 | | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
289 | 293 | | |
290 | 294 | | |
291 | 295 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
210 | 215 | | |
211 | 216 | | |
212 | 217 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
210 | 215 | | |
211 | 216 | | |
212 | 217 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
210 | 215 | | |
211 | 216 | | |
212 | 217 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
210 | 215 | | |
211 | 216 | | |
212 | 217 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
210 | 215 | | |
211 | 216 | | |
212 | 217 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
210 | 215 | | |
211 | 216 | | |
212 | 217 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
210 | 215 | | |
211 | 216 | | |
212 | 217 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
210 | 215 | | |
211 | 216 | | |
212 | 217 | | |
| |||
0 commit comments