This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-plugin-testing.git

commit 6d97c92311510d97ecc0cf9a6277f0ae5c48b9e5
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Tue Nov 10 14:23:11 2026 +0000

    Backport plexus-utils cleanups from apache/maven#13928 to the Maven 3 
harness
    
    Mirrors the parts of apache/maven PR #13928 ("Remove remaining Plexus
    dependencies from maven-testing") that are portable to the Maven 3 line,
    applied to org.apache.maven.api.plugin.testing.MojoExtension:
    
    - Replace XmlStreamReader with InputStreamReader + UTF-8 for the plugin
      descriptor stream and Xpp3DomBuilder.build of POM files
    - Replace plexus-utils ReflectionUtils.getFieldByNameIncludingSuperclasses
      with an equivalent private JDK-only helper
    - Improve field-not-found error messages to include the class name, and
      add the missing null check in getVariableValueFromObject
    
    The remaining PR hunks do not apply to the Maven 3 line: maven-core 3.x
    has no MojoExpressionEvaluator public SPI (mojo configuration is wired
    through the Plexus ExpressionEvaluator / BasicComponentConfigurator, and
    the Xpp3Dom configuration pipeline is inherent to Maven 3), so neither
    the SPI promotion nor the sisu.plexus/guice dependency removal can be
    ported here; guice is referenced directly by MojoExtension and remains a
    transitive compile dependency.
---
 .../maven/api/plugin/testing/MojoExtension.java    | 34 +++++++++++++++++-----
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git 
a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java
 
b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java
index 010caef..bbbc6be 100644
--- 
a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java
+++ 
b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java
@@ -21,6 +21,7 @@ package org.apache.maven.api.plugin.testing;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.StringReader;
 import java.lang.reflect.AccessibleObject;
@@ -28,6 +29,7 @@ import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.net.URL;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -86,8 +88,6 @@ import 
org.codehaus.plexus.component.repository.exception.ComponentLookupExcepti
 import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
 import org.codehaus.plexus.testing.PlexusExtension;
 import org.codehaus.plexus.util.InterpolationFilterReader;
-import org.codehaus.plexus.util.ReflectionUtils;
-import org.codehaus.plexus.util.xml.XmlStreamReader;
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 import org.eclipse.aether.RepositorySystemSession;
@@ -270,7 +270,7 @@ public class MojoExtension extends PlexusExtension 
implements ParameterResolver
         try (InputStream is = Objects.requireNonNull(
                         
classLoader.getResourceAsStream(getPluginDescriptorLocation()),
                         "Unable to find plugin descriptor: " + 
getPluginDescriptorLocation());
-                Reader reader = new BufferedReader(new XmlStreamReader(is));
+                Reader reader = new BufferedReader(new InputStreamReader(is, 
StandardCharsets.UTF_8));
                 InterpolationFilterReader interpolationReader = new 
InterpolationFilterReader(reader, map, "${", "}")) {
 
             PluginDescriptor pluginDescriptor = new 
PluginDescriptorBuilder().build(interpolationReader);
@@ -409,7 +409,7 @@ public class MojoExtension extends PlexusExtension 
implements ParameterResolver
 
         if (pomDom == null) {
             if (pomPath != null && Files.exists(pomPath)) {
-                pomDom = Xpp3DomBuilder.build(new 
XmlStreamReader(pomPath.toFile()));
+                pomDom = Xpp3DomBuilder.build(Files.newBufferedReader(pomPath, 
StandardCharsets.UTF_8));
             } else {
                 pomDom = new Xpp3Dom("");
             }
@@ -686,7 +686,10 @@ public class MojoExtension extends PlexusExtension 
implements ParameterResolver
      */
     @SuppressWarnings("unchecked")
     public static <T> T getVariableValueFromObject(Object object, String 
variable) throws IllegalAccessException {
-        Field field = 
ReflectionUtils.getFieldByNameIncludingSuperclasses(variable, 
object.getClass());
+        Field field = getFieldByNameIncludingSuperclasses(variable, 
object.getClass());
+        Objects.requireNonNull(
+                field,
+                "Field " + variable + " not found in " + 
object.getClass().getName());
         field.setAccessible(true);
         return (T) field.get(object);
     }
@@ -749,12 +752,29 @@ public class MojoExtension extends PlexusExtension 
implements ParameterResolver
      */
     public static void setVariableValueToObject(Object object, String 
variable, Object value)
             throws IllegalAccessException {
-        Field field = 
ReflectionUtils.getFieldByNameIncludingSuperclasses(variable, 
object.getClass());
-        Objects.requireNonNull(field, "Field " + variable + " not found");
+        Field field = getFieldByNameIncludingSuperclasses(variable, 
object.getClass());
+        Objects.requireNonNull(
+                field,
+                "Field " + variable + " not found in " + 
object.getClass().getName());
         field.setAccessible(true);
         field.set(object, value);
     }
 
+    /**
+     * Traverses the class hierarchy from {@code clazz} upward and returns the 
first
+     * declared field matching {@code name}, or {@code null} if none is found.
+     */
+    private static Field getFieldByNameIncludingSuperclasses(String name, 
Class<?> clazz) {
+        for (Class<?> type = clazz; type != null && type != Object.class; type 
= type.getSuperclass()) {
+            try {
+                return type.getDeclaredField(name);
+            } catch (NoSuchFieldException ignored) {
+                // continue
+            }
+        }
+        return null;
+    }
+
     private static class WrapEvaluator implements TypeAwareExpressionEvaluator 
{
 
         private final PlexusContainer container;

Reply via email to