gnodet commented on code in PR #11029:
URL: https://github.com/apache/maven/pull/11029#discussion_r3502304978


##########
impl/maven-core/src/main/java/org/apache/maven/classrealm/DefaultClassRealmManager.java:
##########
@@ -364,4 +373,65 @@ private static Object getId(ClassLoader classLoader) {
         }
         return classLoader;
     }
+
+    private static final String MODULE_ACCESS_DESCRIPTOR = 
"META-INF/maven/module-access";
+
+    private void applyModuleAccessDescriptors(ClassRealm classRealm) {

Review Comment:
   Good question. The direction is your second option: 
`META-INF/maven/module-access` opens/exports packages **from** a named module 
in the boot/runtime layer **to** the plugin's classloader (which lives in the 
unnamed module).
   
   A concrete example: JLine ships as named modules (`org.jline.terminal`, 
`org.jline.reader`, etc.) in Maven's `lib/modules/` directory. A plugin running 
on the classpath (unnamed module) that needs to interact with JLine's SPI would 
include:
   
   ```
   # META-INF/maven/module-access
   add-exports org.jline.terminal/org.jline.terminal.spi=ALL-UNNAMED
   add-opens org.jline.terminal/org.jline.terminal.impl=ALL-UNNAMED
   ```
   
   This is semantically identical to passing `--add-exports 
org.jline.terminal/org.jline.terminal.spi=ALL-UNNAMED` on the JVM command line 
— it's the standard JPMS mechanism for granting the unnamed module access to 
packages from named modules. We just read it from a descriptor instead of 
requiring JVM flags.
   
   As for why the standard `module-info` mechanism doesn't work here:
   - The plugin doesn't own the module it needs access to (e.g., 
`org.jline.terminal`), so it can't add `exports` to someone else's module 
descriptor
   - `qualified exports` in `module-info.java` require knowing the target 
module name at compile time, but the plugin is on the classpath (unnamed 
module, no name to export to)
   - The only standard way to do this at runtime is `--add-exports` / 
`Module.addExports()` via a `Controller` — which is exactly what the 
implementation calls under the hood
   
   You're right that this lets class A decide what's visible inside class B's 
module. That's inherent to the problem: plugins need access to packages that 
named modules don't export to `ALL-UNNAMED` by default. The same tension exists 
with `--add-exports` on the JVM command line. The `module-access` descriptor at 
least makes this explicit and declarative — Maven logs a debug message for each 
directive applied, so it's auditable.
   
   For modular plugins (the new `<modular>true</modular>` path), this is less 
of an issue because the plugin itself is a named module and can use proper 
`requires` directives in its own `module-info.java`. The `module-access` 
mechanism is primarily needed for classic (classpath-based) plugins that need 
to interact with modularized libraries.



##########
impl/maven-classworlds/src/test/java/org/codehaus/plexus/classworlds/realm/ClassRealmImplTest.java:
##########
@@ -0,0 +1,482 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.codehaus.plexus.classworlds.realm;
+
+/*
+ * Copyright 2001-2006 Codehaus Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.codehaus.plexus.classworlds.AbstractClassWorldsTestCase;
+import org.codehaus.plexus.classworlds.ClassWorld;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.fail;
+
+class ClassRealmImplTest extends AbstractClassWorldsTestCase {
+    private ClassWorld world;
+
+    @BeforeEach
+    public void setUp() {
+        this.world = new ClassWorld();
+    }
+
+    @AfterEach
+    public void tearDown() {
+        this.world = null;
+    }
+
+    @Test
+    void testNewRealm() throws Exception {
+        ClassRealm realm = this.world.newRealm("foo");
+
+        assertNotNull(realm);
+
+        assertSame(this.world, realm.getWorld());
+
+        assertEquals("foo", realm.getId());
+    }
+
+    @Test
+    void testLocateSourceRealmNoImports() {
+        ClassRealm realm = new ClassRealm(this.world, "foo", null);
+
+        assertSame(null, realm.getImportClassLoader("com.werken.Stuff"));
+    }
+
+    @Test
+    void testLocateSourceRealmSimpleImport() throws Exception {
+        ClassRealm mainRealm = this.world.newRealm("main");
+
+        ClassRealm werkflowRealm = this.world.newRealm("werkflow");
+
+        mainRealm.importFrom("werkflow", "com.werken.werkflow");
+
+        assertSame(werkflowRealm, 
mainRealm.getImportClassLoader("com.werken.werkflow.WerkflowEngine"));
+

Review Comment:
   Agreed — these came from the original plexus-classworlds fork. Will clean up.



-- 
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]

Reply via email to