gnodet-bot commented on code in PR #13582: URL: https://github.com/apache/maven/pull/13582#discussion_r4324386486
########## maven-embedder/src/test/java/org/apache/maven/cli/internal/BootstrapCoreExtensionManagerTest.java: ########## @@ -0,0 +1,71 @@ +/* + * 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.apache.maven.cli.internal; + +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.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Guards the JSR330 visibility filtering bypass: it must apply only when the TCCL is (a child of) a core extension realm. + */ +class BootstrapCoreExtensionManagerTest { + private final ClassLoader origTccl = Thread.currentThread().getContextClassLoader(); + + private ClassWorld world; + + @BeforeEach + void setUp() { + world = new ClassWorld(); + } + + @AfterEach + void tearDown() { + Thread.currentThread().setContextClassLoader(origTccl); + } + + @Test + void plainTcclIsNotCoreExtensionRealm() { + assertFalse(BootstrapCoreExtensionManager.isContextRealmCoreExtensionRealm()); + } + + @Test + void coreExtensionRealmTccl() throws Exception { + Thread.currentThread().setContextClassLoader(world.newRealm("coreExtension>g:a:1", null)); + assertTrue(BootstrapCoreExtensionManager.isContextRealmCoreExtensionRealm()); + } + + @Test + void childOfCoreExtensionRealmTccl() throws Exception { + Thread.currentThread() + .setContextClassLoader( + new java.net.URLClassLoader(new java.net.URL[0], world.newRealm("coreExtension>g:a:1", null))); + assertTrue(BootstrapCoreExtensionManager.isContextRealmCoreExtensionRealm()); + } Review Comment: 💡 **Suggestion:** The `childOfCoreExtensionRealmTccl` test covers a `URLClassLoader` wrapping a core extension realm, exercising the parent-chain walk via `getParent()`. Consider adding a sibling test where the TCCL is a `ClassRealm` child of the core extension realm (e.g. `world.newRealm("coreExtension>g:a:1>child", coreExtRealm)`), since the production loop checks `instanceof ClassRealm` explicitly — this would directly exercise that branch. ########## maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycles.java: ########## @@ -132,33 +131,19 @@ public Map<String, Lifecycle> getPhaseToLifecycleMap() { public List<Lifecycle> getLifeCycles() { // ensure canonical order of standard lifecycles - Map<String, Lifecycle> lifecycles = new LinkedHashMap<>(); - - // filter by visibility (plexus vs sisu diff; "realms" are plexus thing) - // in some tests container is not injected - if (this.plexusContainer != null) { - for (String name : this.lifecycles.keySet()) { - if (!isEnabled(name)) { - continue; - } - try { - lifecycles.put(name, plexusContainer.lookup(Lifecycle.class, name)); - } catch (ComponentLookupException e) { - // skip it - } - } - } else { - lifecycles.putAll(this.lifecycles); - } + // take "snapshot": will filter by visibility + Map<String, Lifecycle> lifecycles = new LinkedHashMap<>(this.lifecycles); + // apply enabled + lifecycles.entrySet().removeIf(e -> !isEnabled(e.getKey())); LinkedHashSet<String> lifecycleNames = new LinkedHashSet<>(getStandardLifecycles()); lifecycleNames.addAll(lifecycles.keySet()); ArrayList<Lifecycle> result = new ArrayList<>(); for (String name : lifecycleNames) { Lifecycle lifecycle = lifecycles.get(name); - if (lifecycle.getId() == null) { - throw new NullPointerException("A lifecycle must have an id."); + if (lifecycle == null || lifecycle.getId() == null) { Review Comment: 💡 **Minor:** The `lifecycle == null` check is defensive dead code — `lifecycleNames` is built from `lifecycles.keySet()`, so a lookup is guaranteed to return non-null. A short comment like `// defensive — lifecycleNames ⊆ lifecycles.keySet()` would make the intent clear for future readers. -- 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]
