gnodet-bot commented on code in PR #13636:
URL: https://github.com/apache/maven/pull/13636#discussion_r4483966746
##########
impl/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java:
##########
@@ -823,6 +827,28 @@ private void populateMojoExecutionFields(
}
try {
+ if ("enhanced".equals(configuratorId)) {
Review Comment:
⚠️ **Missing `isV4Api()` guard — V3 plugins with `configurator="enhanced"`
will break.**
`populateMojoExecutionFields` is called from both `loadV3Mojo` (new diff
line 805) and `loadV4Mojo` (new diff line 619). With no `isV4Api()` guard, any
V3 plugin that explicitly declares `<configurator>enhanced</configurator>` in
its descriptor (some community plugins do this) will now be routed to
`V4MojoConfigurator` instead of the Plexus component lookup.
`V4MojoConfigurator` has different semantics — no `ClassRealmConverter`
context, no Plexus type converters — so these V3 plugins will silently
misconfigure or fail at runtime.
Also: now that `EnhancedComponentConfigurator` is deleted, V3 mojos that hit
this branch with `isV4Api() == false` would reach the Plexus lookup and get
`ComponentLookupException` anyway — but that's a clearer failure than silent
misconfiguration.
```suggestion
if ("enhanced".equals(configuratorId) &&
mojoDescriptor.isV4Api()) {
```
##########
impl/maven-core/src/main/java/org/apache/maven/plugin/internal/V4MojoConfigurator.java:
##########
@@ -0,0 +1,445 @@
+/*
+ * 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.plugin.internal;
+
+import java.io.File;
+import java.lang.reflect.Array;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.RecordComponent;
+import java.lang.reflect.Type;
+import java.net.URI;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import org.apache.maven.api.xml.XmlNode;
+import org.apache.maven.plugin.PluginConfigurationException;
+import org.apache.maven.plugin.descriptor.PluginDescriptor;
+
+/** Configures a Maven 4 mojo directly from the public Maven XML model. */
+public final class V4MojoConfigurator {
+ private static final Object NOT_SCALAR = new Object();
+
+ private final MojoExpressionEvaluator evaluator;
+ private final ClassLoader classLoader;
+ private final PluginDescriptor pluginDescriptor;
+
+ public V4MojoConfigurator(
+ MojoExpressionEvaluator evaluator, ClassLoader classLoader,
PluginDescriptor pluginDescriptor) {
+ this.evaluator = evaluator;
+ this.classLoader = classLoader;
+ this.pluginDescriptor = pluginDescriptor;
+ }
+
+ /**
+ * Creates a configurator backed by a Plexus {@link
TypeAwareExpressionEvaluator}.
+ * For use by callers outside the {@code internal} package that cannot
access
+ * {@link PlexusMojoExpressionEvaluatorAdapter} directly (e.g. {@code
maven-testing}).
+ */
+ public V4MojoConfigurator(
+
org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator
evaluator,
+ ClassLoader classLoader,
+ PluginDescriptor pluginDescriptor) {
+ this(new PlexusMojoExpressionEvaluatorAdapter(evaluator), classLoader,
pluginDescriptor);
+ }
+
+ public void configure(Object mojo, XmlNode configuration) throws
PluginConfigurationException {
+ if (configuration == null) {
+ return;
+ }
+ for (XmlNode child : configuration.children()) {
+ try {
+ set(mojo, child.name(), child);
+ } catch (Exception e) {
+ throw new PluginConfigurationException(
+ pluginDescriptor,
+ "Unable to configure '" + child.name() + "' on "
+ + mojo.getClass().getName(),
+ e);
+ }
+ }
+ }
+
+ private void set(Object bean, String name, XmlNode node) throws Exception {
+ if (name.isEmpty()) {
+ throw new NoSuchFieldException("<empty element name>");
+ }
+ String title = Character.toUpperCase(name.charAt(0)) +
name.substring(1);
+ for (Method method : bean.getClass().getMethods()) {
+ if ((method.getName().equals("set" + title) ||
method.getName().equals("add" + title))
+ && method.getParameterCount() == 1) {
+ if (!method.canAccess(bean)) {
+ method.setAccessible(true);
+ }
+ method.invoke(bean, convert(node,
method.getGenericParameterTypes()[0]));
+ return;
+ }
+ }
+ for (Class<?> type = bean.getClass(); type != null; type =
type.getSuperclass()) {
+ try {
+ Field field = type.getDeclaredField(name);
+ field.setAccessible(true);
+ field.set(bean, convert(node, field.getGenericType()));
+ return;
+ } catch (NoSuchFieldException ignored) {
+ // Continue through the class hierarchy.
+ }
+ }
+ throw new NoSuchFieldException(name);
+ }
+
+ private Object convert(XmlNode node, Type target) throws Exception {
+ Class<?> type = rawType(target);
+ if (type == XmlNode.class) {
+ return node;
+ }
+ Object scalar = convertScalar(node, type);
+ if (scalar != NOT_SCALAR) {
+ return scalar;
+ }
+ if (type.isArray()) {
+ List<XmlNode> children = node.children();
+ if (children.isEmpty()) {
+ // No child elements: treat the text value as a
comma-separated list
+ String text = evaluateNodeAsString(node);
+ String[] parts = text != null
+ ? java.util.Arrays.stream(text.split(","))
+ .map(String::trim)
+ .filter(s -> !s.isEmpty())
+ .toArray(String[]::new)
+ : new String[0];
+ Object array = Array.newInstance(type.getComponentType(),
parts.length);
+ for (int i = 0; i < parts.length; i++) {
+ Array.set(array, i, convert(XmlNode.newInstance("item",
parts[i]), type.getComponentType()));
+ }
+ return array;
+ }
+ Object array = Array.newInstance(type.getComponentType(),
children.size());
+ for (int i = 0; i < children.size(); i++) {
+ Array.set(array, i, convert(children.get(i),
type.getComponentType()));
+ }
+ return array;
+ }
+ if (Collection.class.isAssignableFrom(type)) {
+ Type itemType = target instanceof ParameterizedType p ?
p.getActualTypeArguments()[0] : Object.class;
+ Collection<Object> values =
+ Set.class.isAssignableFrom(type) ? new
java.util.LinkedHashSet<>() : new ArrayList<>();
+ List<XmlNode> children = node.children();
+ if (children.isEmpty()) {
+ // No child elements: treat the text value as a
comma-separated list
+ String text = evaluateNodeAsString(node);
+ if (text != null) {
+ for (String part : text.split(",")) {
+ String trimmed = part.trim();
+ if (!trimmed.isEmpty()) {
+ values.add(convert(XmlNode.newInstance("item",
trimmed), itemType));
+ }
+ }
+ }
+ } else {
+ for (XmlNode child : children) {
+ values.add(convert(child, itemType));
+ }
+ }
+ return values;
+ }
+ if (Map.class.isAssignableFrom(type)) {
+ Type valueType = target instanceof ParameterizedType p ?
p.getActualTypeArguments()[1] : Object.class;
+ if (Properties.class.isAssignableFrom(type)) {
+ Properties properties = new Properties();
+ for (XmlNode child : node.children()) {
+ // Support Plexus-style:
<property><name>k</name><value>v</value></property>
+ XmlNode nameNode = child.child("name");
+ XmlNode valueNode = child.child("value");
+ if (nameNode != null && valueNode != null) {
+ String key = evaluateNodeAsString(nameNode);
+ Object val = convert(valueNode, valueType);
+ if (key != null && val != null) {
+ properties.put(key, val);
+ }
+ } else if (nameNode == null) {
+ // Simple <key>value</key> style
+ Object val = convert(child, valueType);
+ if (val != null) {
+ properties.put(child.name(), val);
+ }
+ }
+ // nameNode != null && valueNode == null: malformed Plexus
entry — skip it
+ }
+ return properties;
+ }
+ Map<String, Object> values = new LinkedHashMap<>();
+ for (XmlNode child : node.children()) {
+ values.put(child.name(), convert(child, valueType));
+ }
+ return values;
+ }
+ type = resolveImplementation(node, type);
+ String value = evaluateNodeAsString(node);
+ if (value != null && node.children().isEmpty()) {
+ Method setter = findDefaultSetter(type);
+ if (setter != null) {
+ Object instance = instantiate(type);
+ if (!setter.canAccess(instance)) {
+ setter.setAccessible(true);
+ }
+ setter.invoke(
+ instance, convert(XmlNode.newInstance("value", value),
setter.getGenericParameterTypes()[0]));
+ return instance;
+ }
+ }
+ if (type.isRecord()) {
+ return instantiateRecord(node, type);
+ }
+ Object instance = instantiate(type);
+ for (XmlNode child : node.children()) {
+ set(instance, child.name(), child);
+ }
+ return instance;
+ }
+
+ private Object convertScalar(XmlNode node, Class<?> type) throws Exception
{
+ if (type == String.class) {
+ return evaluateNode(node, type);
+ }
+ if (type == Object.class) {
+ // For Object-typed parameters: if the node is a leaf (no
children), evaluate as String.
+ // If it has children, fall through to bean/map/collection
conversion so that
+ // structured XML children are not silently dropped.
+ if (node.children().isEmpty()) {
+ return evaluateNode(node, String.class);
+ }
+ return NOT_SCALAR;
Review Comment:
⚠️ **`Object.class` with structured children crashes —
`NoSuchFieldException` from the bean path.**
When `type == Object.class` and the node has children, `NOT_SCALAR` is
returned here. Back in `convert()`, the control falls through all type-specific
branches (array, Collection, Map — none match `Object.class`) and reaches:
```java
Object instance = instantiate(Object.class); // → new Object()
for (XmlNode child : node.children()) {
set(instance, child.name(), child); // → NoSuchFieldException
(Object has no fields)
}
```
This `NoSuchFieldException` is wrapped in a `PluginConfigurationException`
and thrown to the user. This is a regression — the previous behavior returned
`null` (silent data loss), which was bad, but this is a hard crash for any
`Object`-typed parameter with structured XML children.
The fix: when `Object.class` has children, convert to `Map<String, Object>`
(consistent with how the configurator handles `Map.class` without type
parameters).
```suggestion
if (type == Object.class) {
if (node.children().isEmpty()) {
return evaluateNode(node, String.class);
}
// Object-typed parameter with children: convert to Map<String,
Object>
// so structured XML children are preserved rather than crashing
the bean path.
Map<String, Object> map = new LinkedHashMap<>();
for (XmlNode child : node.children()) {
map.put(child.name(), convert(child, Object.class));
}
return map;
}
```
--
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]