diff --git a/dd-java-agent/instrumentation/maven/maven-3.2.1/src/main/java/datadog/trace/instrumentation/maven3/MavenUtils.java b/dd-java-agent/instrumentation/maven/maven-3.2.1/src/main/java/datadog/trace/instrumentation/maven3/MavenUtils.java index b08b6bffa39..c2ed31af284 100644 --- a/dd-java-agent/instrumentation/maven/maven-3.2.1/src/main/java/datadog/trace/instrumentation/maven3/MavenUtils.java +++ b/dd-java-agent/instrumentation/maven/maven-3.2.1/src/main/java/datadog/trace/instrumentation/maven3/MavenUtils.java @@ -351,7 +351,7 @@ public static List getClasspath(MavenSession session, MojoExecution mojoEx MethodHandles methodHandles = new MethodHandles(pluginRealm); MethodHandle generateTestClasspathMethod = - findMethod(methodHandles, mojo.getClass(), "generateTestClasspath"); + findMethod(methodHandles, mojo.getClass(), "generateTestClasspath", true); if (generateTestClasspathMethod == null) { LOGGER.debug( "Could not find generateTestClasspathMethod method in {} class", @@ -485,13 +485,32 @@ private static String getEffectiveJvm(MojoExecution mojoExecution, Mojo mojo) { private static MethodHandle findMethod( MethodHandles methodHandles, Class mojoClass, String methodName) { - do { - MethodHandle getEffectiveJvm = methodHandles.method(mojoClass, methodName); - if (getEffectiveJvm != null) { - return getEffectiveJvm; + return findMethod(methodHandles, mojoClass, methodName, false); + } + + private static MethodHandle findMethod( + MethodHandles methodHandles, Class mojoClass, String methodName, boolean acceptVarargs) { + for (Class clazz = mojoClass; clazz != null; clazz = clazz.getSuperclass()) { + MethodHandle handle = methodHandles.method(clazz, methodName); + if (handle != null) { + return handle; + } + } + if (!acceptVarargs) { + return null; + } + // fallback to a varargs method of the same name, which can also be invoked without arguments: + // our MethodHandles#invoke delegates to MethodHandle#invokeWithArguments, which collects the + // missing trailing varargs into an empty array. Necessary for: + // - AbstractSurefireMojo#generateTestClasspath() in >= 3.6.0-M1: added "ArtifactFilter..." + // param + for (Class clazz = mojoClass; clazz != null; clazz = clazz.getSuperclass()) { + MethodHandle handle = + methodHandles.method(clazz, m -> m.getName().equals(methodName) && m.isVarArgs()); + if (handle != null) { + return handle; } - mojoClass = mojoClass.getSuperclass(); - } while (mojoClass != null); + } return null; }