ffang commented on PR #3816:
URL: https://github.com/apache/cxf/pull/3816#issuecomment-7383715211
> ## Review: CXF-9285 – Use precompiled namespace prefix mappers instead of
generating them
> Thanks, this is a nice cleanup. Replacing ~400 lines of hand-written ASM
with two compiled classes is much easier to maintain, and it fixes several real
bugs along the way:
>
> * `GeneratedNamespaceClassLoader` looked up
`org.apache.cxf.jaxb.NamespaceMapper`, but the generator only ever defined
`NamespaceMapperRI`, so the load-only (GraalVM) setup silently lost the
namespace prefixes.
> * When no mapper class was available, `ClassLoaderProxyService` /
`DataWriterImpl` logged an NPE at WARNING.
> * The precompiled mapper's `setContextualNamespace` vs. the
`setContextualNamespaceDecls` method that `DataWriterImpl` calls.
>
> I built `cxf-core` from this branch with checkstyle enabled, and
`NamespaceClassCreatorTest` passes (4/4). For marshallers that are neither
GlassFish nor MOXy the behaviour is unchanged: the old generator also always
extended the GlassFish `NamespacePrefixMapper`, and
`JAXBUtils.setNamespaceMapper` only sets the property for those two runtimes.
>
> ### 1. OSGi import of an EclipseLink internal package with a `[5.0,6)`
range
> The generated `cxf-core` manifest now contains:
>
> ```
>
org.eclipse.persistence.internal.oxm.record.namespaces;version="[5.0,6)";resolution:=optional
> ```
>
> Two concerns:
>
> * `EclipseNamespaceMapper` extends `MapNamespacePrefixMapper` from an
EclipseLink **internal** package, so core now has a compile-time dependency on
a non-API class that can change in any release.
> * Because the import is optional, an OSGi deployment with EclipseLink 4.x
(also Jakarta) won't wire it and MOXy prefix mapping is silently lost, with no
error.
>
> MOXy prefix mapping in OSGi may not have worked before either, since the
generated class was defined from core's classloader and core had no EclipseLink
import. Still, could we either widen the range (e.g. `[4.0,6)`, if 4.x is
binary-compatible here) or set it explicitly in `cxf.osgi.import`, and add a
short comment on the dependency explaining why core needs it?
>
> ### 2. The "EclipseLink not present" path is untested and a bit implicit
> `NamespaceClassGenerator.getNamespaceMapper`:
>
> ```
> try {
> return mcls.getName().contains("eclipse") ?
EclipseNamespaceMapper.class : NamespaceMapper.class;
> } catch (LinkageError ex) {
> ```
>
> This relies on `EclipseNamespaceMapper.class` throwing
`NoClassDefFoundError` when the class is resolved. That's correct JVM
behaviour, but it isn't obvious to a reader, and no test runs without
EclipseLink on the classpath. Suggest splitting it into an `if` so only the
Eclipse branch is inside the `try`, with a comment saying the superclass may be
missing. A test using an isolating classloader would be nice to have, but not a
blocker.
>
> ### 3. The MOXy assertion in `XMLStreamDataWriterTest` is weak
> ```
> } else {
> assertTrue(xstr, xstr.startsWith("<x:greetMe"));
> }
> ```
>
> This doesn't show that the contextual namespace declaration is actually
left out under MOXy, which is the point of
`EclipseNamespaceMapper.getPreDeclaredNamespaceUris`. Could it assert that the
`xmlns:x` declaration is absent? If EclipseLink bug 421463 still prevents that,
a comment saying what we _can_ expect would help.
>
> ### 4. The dependency-management entry applies to every module
> `parent/pom.xml` now manages `org.eclipse.persistence.core` (via
`cxf.eclipselink.version`) for every module. That's harmless today, and reusing
the property in the `eclipse.moxy` profile is good, but it's worth keeping in
mind if anything later brings in a different EclipseLink version.
>
> ### 5. Nits
> * `NamespaceClassGenerator` no longer generates anything but keeps its
name and still extends `ClassGeneratorClassLoader`. That's fine for
compatibility; maybe add a class-level note or `@deprecated` pointing to the
precompiled mappers, or open a follow-up.
> * The javadoc of `GeneratedClassClassLoaderCapture` and
`distribution/.../jaxws_graalvm_dynamic/client/.../Client.java` still describe
capturing or loading generated namespace classes. That is still valid usage,
but the wording is now misleading.
> * `NamespaceMapper.java` still has no newline at the end of the file.
> * `NamespaceClassGenerator` uses
`LogUtils.getL7dLogger(ClassGeneratorClassLoader.class)`. This was already
there, but it might as well use its own class now that the file is being
rewritten.
>
> ### Verdict
> Approve once item 1 (OSGi range / internal-package dependency) has been
checked and answered; items 2–5 are optional polish.
Thanks for the review, @coheigea. Addressed in
0687ec36e6be0f57a9c4020165dc43886da90b3c:
1. **OSGi import / internal package**: the EclipseLink import is now
`org.eclipse.persistence*;resolution:=optional;version="[4,6)"`.
`MapNamespacePrefixMapper` is the same in EclipseLink 4.0.x and 5.0.x
(same constructor, methods and
superclass), and EclipseLink exports its packages at the bundle
version. I added a comment on the dependency in `core/pom.xml` explaining
why it is needed and that it
is an internal class.
2. **EclipseLink not present**: `getNamespaceMapper` now returns
`NamespaceMapper` for non-MOXy marshallers
before the `try`, and only the `EclipseNamespaceMapper` resolution is
guarded, with a comment.
`NamespaceClassCreatorTest#testNoMapperForEclipseLinkMoxyWithoutEclipseLink`
loads the generator with a
classloader that hides EclipseLink and checks that no mapper is returned
for MOXy (and that GlassFish
still gets `NamespaceMapper`).
3. **MOXy assertion**: under MOXy the contextual declaration is still
written on the root element
([bug 421463](https://bugs.eclipse.org/bugs/show_bug.cgi?id=421463)), so
the test can't assert that it
is absent. It now asserts the exact MOXy output, which shows that the
mapped `x` prefix is used; without
the fix, the load-only setup loses it.
4. **Dependency management**: agreed. The property is now shared with the
`eclipse.moxy` profile, so there
is a single EclipseLink version to change.
5. **Nits**: done. `NamespaceClassGenerator` uses its own logger,
`NamespaceMapper.java` ends with a newline,
and the `GeneratedClassClassLoaderCapture` javadoc now says no namespace
class is captured. I kept the
name `NamespaceClassGenerator`, since it is still the default
`NamespaceClassCreator` used by
`ClassLoaderProxyService`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]