gnodet-bot commented on code in PR #13494:
URL: https://github.com/apache/maven/pull/13494#discussion_r4614569518
##########
impl/maven-classworlds/src/main/java/org/codehaus/plexus/classworlds/launcher/Configurator.java:
##########
@@ -198,106 +180,147 @@ public void associateRealms() {
}
}
- public void addImportFrom(String relamName, String importSpec) throws
NoSuchRealmException {
- curRealm.importFrom(relamName, importSpec);
+ @Override
+ public void addLayer(String layerName) {
+ curLayerName = layerName;
+ curRealm = null;
+ namedLayerPaths.computeIfAbsent(layerName, k -> new ArrayList<>());
}
+ @Override
+ public void addImportFrom(String realmName, String importSpec) throws
NoSuchRealmException {
+ curRealm.importFrom(realmName, importSpec);
+ }
+
+ @Override
public void addLoadFile(File file) {
try {
curRealm.addURL(file.toURI().toURL());
} catch (MalformedURLException e) {
- // can't really happen... or can it?
+ // can't really happen
}
}
+ @Override
public void addLoadURL(URL url) {
curRealm.addURL(url);
}
+ @Override
public void addRealm(String realmName) throws DuplicateRealmException {
+ curLayerName = null;
curRealm = world.newRealm(realmName, foreignClassLoader);
- // If the 'module' directive failed to create a JPMS layer before any
section was
- // declared, drain the deferred classpath fallback into this first
realm.
- if (!pendingModuleClasspathFallback.isEmpty()) {
- for (Path path : pendingModuleClasspathFallback) {
- try {
- curRealm.addURL(path.toUri().toURL());
- } catch (MalformedURLException e) {
- // ignore
- }
- }
- pendingModuleClasspathFallback.clear();
- }
- // Stash the configured realm for subsequent association processing.
configuredRealms.put(realmName, curRealm);
}
+ @Override
public void addModuleFile(File file) {
- modulePaths.add(file.toPath());
+ if (curLayerName != null) {
Review Comment:
⚠️ **Silent discard:** when `curLayerName == null` (i.e. `addModuleFile` is
called outside a named-layer context), the file is silently dropped. The old
code unconditionally added to `modulePaths`. Callers who relied on the old
behaviour will silently lose their artifact. Consider throwing
`IllegalStateException` or at minimum logging a warning when `curLayerName` is
null, so this configuration error is surfaced rather than silently ignored.
##########
impl/maven-core/src/main/java/org/apache/maven/classrealm/DefaultClassRealmManager.java:
##########
@@ -259,6 +286,113 @@ public ClassRealm createPluginRealm(
return createRealm(getKey(plugin, false), RealmType.Plugin, parent,
parentImports, foreignImports, artifacts);
}
+ @Override
+ public ClassRealm createModularPluginRealm(Plugin plugin, ClassLoader
parent, List<Artifact> artifacts)
+ throws ModuleLayerCreationException {
+ Objects.requireNonNull(plugin, "plugin cannot be null");
+
+ if (parent == null) {
+ parent = PARENT_CLASSLOADER;
+ }
+
+ String realmId = getKey(plugin, false);
+ ClassRealm classRealm = newRealm(realmId);
+ classRealm.setParentClassLoader(parent);
+
+ // Collect all artifact paths for the module finder
+ List<Path> modulePaths = new ArrayList<>();
+ if (artifacts != null) {
+ for (Artifact artifact : artifacts) {
+ if (artifact.getFile() != null &&
!isProvidedArtifact(artifact, true)) {
+ modulePaths.add(artifact.getFile().toPath());
+ } else if (logger.isDebugEnabled() && artifact.getFile() !=
null) {
+ logger.debug(" Excluded from module path: {}",
getId(artifact));
+ }
+ }
+ }
+
+ if (modulePaths.isEmpty()) {
+ throw new ModuleLayerCreationException("Plugin " + plugin.getId()
+ + " declares <modular>true</modular> but has no artifacts"
+ " to place on the module path");
+ }
+
+ try {
+ ModuleFinder finder = ModuleFinder.of(modulePaths.toArray(new
Path[0]));
+ Set<String> moduleNames = finder.findAll().stream()
+ .map(ref -> ref.descriptor().name())
+ .collect(Collectors.toSet());
+
+ if (moduleNames.isEmpty()) {
+ throw new ModuleLayerCreationException(
+ "Plugin " + plugin.getId() + " declares
<modular>true</modular> but none of its"
+ + " artifacts contain module descriptors
(module-info.class)."
+ + " Ensure the plugin JAR is compiled with a
module-info.java.");
+ }
+
+ // Parent layer: use the "api" layer from ClassWorld so plugins can
+ // require maven-api-* modules. Infrastructure modules (e.g.
JLine) live
+ // in a sibling "infra" layer that is NOT a JPMS parent here.
+ if (!(world instanceof ClassWorld)) {
Review Comment:
⚠️ **Tautology:** `world` is declared `private final ClassWorld world` —
this `instanceof ClassWorld` check is always `true`. Same pattern at line 356
for `classRealm instanceof ClassRealm`. Both are concrete types, not
interfaces, so the guard never fires. If the intent is to future-proof against
proxy wrapping, add a comment explaining that; otherwise the check is
misleading.
##########
impl/maven-classworlds/src/main/java/org/codehaus/plexus/classworlds/launcher/Configurator.java:
##########
@@ -319,28 +342,99 @@ private void createModuleLayer() {
}
}
- private void addModulePathsToClasspath() {
+ private void applyRealmParentLayers() {
+ for (Map.Entry<String, String> entry : realmParentLayers.entrySet()) {
+ String realmId = entry.getKey();
+ String layerName = entry.getValue();
+ ClassLoader layerLoader = layerClassLoaders.get(layerName);
+ if (layerLoader != null) {
+ ClassRealm realm = configuredRealms.get(realmId);
+ if (realm != null) {
+ realm.setParentClassLoader(layerLoader);
+ }
+ }
+ }
+ }
+
+ private void applyExportedImports() throws NoSuchRealmException {
+ for (String[] pair : realmExportedImports) {
+ String targetRealmId = pair[0];
+ String sourceRealmId = pair[1];
+ ClassRealm targetRealm = configuredRealms.get(targetRealmId);
+ ClassRealm sourceRealm = configuredRealms.get(sourceRealmId);
+ if (targetRealm == null) {
+ throw new NoSuchRealmException(world, targetRealmId);
+ }
+ if (sourceRealm == null) {
+ throw new NoSuchRealmException(world, sourceRealmId);
+ }
+ // Scan the source realm for META-INF/maven/extension.xml exported
packages
+ discoverAndImportExported(targetRealm, sourceRealm);
+ }
+ }
+
+ private void discoverAndImportExported(ClassRealm targetRealm, ClassRealm
sourceRealm) {
+ try {
+ var urls =
sourceRealm.getResources("META-INF/maven/extension.xml");
+ XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
+
xmlFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
+ while (urls.hasMoreElements()) {
+ var url = urls.nextElement();
+ try (var is = url.openStream()) {
+ XMLStreamReader reader =
xmlFactory.createXMLStreamReader(is);
Review Comment:
💡 **Resource leak:** `XMLStreamReader` is created inside a
`try-with-resources` for the `InputStream`, but `reader` itself is not
auto-closed. The `InputStream` will be closed when the `try` exits, which often
suffices in practice, but `XMLStreamReader` is `AutoCloseable` and should be
wrapped in its own `try-with-resources`:
```suggestion
try (XMLStreamReader reader =
xmlFactory.createXMLStreamReader(is)) {
```
--
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]