gnodet-bot commented on code in PR #13636: URL: https://github.com/apache/maven/pull/13636#discussion_r4351103106
########## impl/maven-core/src/main/java/org/apache/maven/plugin/internal/V4MojoConfigurator.java: ########## @@ -0,0 +1,382 @@ +/* + * 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; +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; +import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator; + +/** 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 ExpressionEvaluator evaluator; + private final ClassLoader classLoader; + private final PluginDescriptor pluginDescriptor; + + public V4MojoConfigurator( + ExpressionEvaluator evaluator, ClassLoader classLoader, PluginDescriptor pluginDescriptor) { + this.evaluator = evaluator; + this.classLoader = classLoader; + this.pluginDescriptor = 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 { + 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(); + 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<>(); + for (XmlNode child : node.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()) { + properties.put(child.name(), convert(child, valueType)); Review Comment: ⚠️ **`Properties.put(key, null)` throws `NullPointerException` when an expression is unresolved.** `Properties` is backed by `Hashtable`, which rejects null values with NPE — unlike `LinkedHashMap`, which accepts them silently. When `convert(child, valueType)` returns `null` (unresolved expression, no `default-value`), line 133 will NPE at runtime for any mojo `Properties` parameter with an unresolved element. The `testCollectionsAndMaps` test doesn't catch this because the mock evaluator always returns the string — the null path is not exercised. ```suggestion Properties properties = new Properties(); for (XmlNode child : node.children()) { Object val = convert(child, valueType); if (val != null) { properties.put(child.name(), val); } } ``` -- 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]
